001/** 002 * CaseComponentSerializer.java 003 * jCOLIBRI2 framework. 004 * @author Juan A. Recio-Garc�a. 005 * GAIA - Group for Artificial Intelligence Applications 006 * http://gaia.fdi.ucm.es 007 * 04/11/2007 008 */ 009package es.ucm.fdi.gaia.jcolibri.connector.xmlutils; 010 011import java.io.StringWriter; 012 013import javax.xml.parsers.DocumentBuilder; 014import javax.xml.parsers.DocumentBuilderFactory; 015import javax.xml.transform.Transformer; 016import javax.xml.transform.TransformerFactory; 017import javax.xml.transform.dom.DOMSource; 018import javax.xml.transform.stream.StreamResult; 019 020import org.apache.logging.log4j.LogManager; 021import org.w3c.dom.Document; 022import org.w3c.dom.Element; 023import org.w3c.dom.NamedNodeMap; 024import org.w3c.dom.Node; 025import org.w3c.dom.NodeList; 026 027import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute; 028import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseComponent; 029import es.ucm.fdi.gaia.jcolibri.connector.plaintextutils.PlainTextTypeConverter; 030import es.ucm.fdi.gaia.jcolibri.util.AttributeUtils; 031 032/** 033 * Utility class to serialize CaseComponents into xml files. 034 * This class will be used in future versions to implement the xml connector. 035 * @author Juan A. Recio-Garcia 036 * @version 1.0 037 * 038 */ 039public class CaseComponentSerializer { 040 041 042 public static CaseComponent deserializeCaseComponent(Node node) 043 { 044 try 045 { 046 NamedNodeMap nodemap = node.getAttributes(); 047 String className = nodemap.getNamedItem("Class").getTextContent(); 048 CaseComponent cc = (CaseComponent)Class.forName(className).newInstance(); 049 050 NodeList nl = node.getChildNodes(); 051 for(int i=0; i<nl.getLength(); i++) 052 { 053 Node child = nl.item(i); 054 String nodeName = child.getNodeName(); 055 056 057 if(nodeName.equals("CaseComponent")) 058 { 059 Attribute at = new Attribute(child.getAttributes().getNamedItem("Name").getTextContent(), Class.forName(child.getAttributes().getNamedItem("Class").getTextContent())); 060 AttributeUtils.setValue(at, cc, deserializeCaseComponent(child)); 061 }else 062 { 063 Attribute at = new Attribute(nodeName, cc.getClass()); 064 if(child.getFirstChild() != null) 065 { 066 String value = child.getFirstChild().getTextContent(); 067 Object oValue = PlainTextTypeConverter.convert(value, at.getType()); 068 AttributeUtils.setValue(at, cc, oValue); 069 } 070 } 071 } 072 return cc; 073 } catch (Exception e) 074 { 075 LogManager.getLogger(CaseComponentSerializer.class).error(e); 076 } 077 return null; 078 } 079 080 public static String serializeCaseComponent(CaseComponent casecomponent, String name) 081 { 082 try 083 { 084 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 085 DocumentBuilder db = dbf.newDocumentBuilder(); 086 Document doc = db.newDocument(); 087 088 Element root = serializeCaseComponent(casecomponent,name,doc); 089 090 TransformerFactory tf = TransformerFactory.newInstance(); 091 Transformer trans = tf.newTransformer(); 092 StringWriter sw = new StringWriter(); 093 trans.transform(new DOMSource(root), new StreamResult(sw)); 094 095 096 return sw.toString(); 097 } catch (Exception e) 098 { 099 LogManager.getLogger(CaseComponentSerializer.class).error(e); 100 101 } 102 return null; 103 } 104 105 public static Element serializeCaseComponent(CaseComponent casecomponent, String name, Document doc) 106 { 107 try 108 { 109 Element root = doc.createElement("CaseComponent"); 110 root.setAttribute("Name", name); 111 root.setAttribute("Class", casecomponent.getClass().getCanonicalName()); 112 root.setAttribute("IdAttribute", casecomponent.getIdAttribute().getName()); 113 114 for(Attribute at: AttributeUtils.getAttributes(casecomponent)) 115 { 116 if(CaseComponent.class.isAssignableFrom(at.getType())) 117 root.appendChild(serializeCaseComponent((CaseComponent)at.getValue(casecomponent),at.getName(),doc)); 118 else 119 { 120 Element child = doc.createElement(at.getName()); 121 Object value = at.getValue(casecomponent); 122 if(value!=null) 123 child.appendChild(doc.createTextNode(value.toString())); 124 root.appendChild(child); 125 } 126 } 127 128 return root; 129 } catch (Exception e) 130 { 131 LogManager.getLogger(CaseComponentSerializer.class).error(e); 132 133 } 134 return null; 135 } 136}