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     */
009    package jcolibri.connector.xmlutils;
010    
011    import java.io.StringWriter;
012    
013    import javax.xml.parsers.DocumentBuilder;
014    import javax.xml.parsers.DocumentBuilderFactory;
015    import javax.xml.transform.Transformer;
016    import javax.xml.transform.TransformerFactory;
017    import javax.xml.transform.dom.DOMSource;
018    import javax.xml.transform.stream.StreamResult;
019    
020    import jcolibri.cbrcore.Attribute;
021    import jcolibri.cbrcore.CaseComponent;
022    import jcolibri.connector.plaintextutils.PlainTextTypeConverter;
023    import jcolibri.util.AttributeUtils;
024    
025    import org.w3c.dom.Document;
026    import org.w3c.dom.Element;
027    import org.w3c.dom.NamedNodeMap;
028    import org.w3c.dom.Node;
029    import org.w3c.dom.NodeList;
030    
031    /**
032     * Utility class to serialize CaseComponents into xml files.
033     * This class will be used in future versions to implement the xml connector.
034     * @author Juan A. Recio-Garcia
035     * @version 1.0
036     *
037     */
038    public class CaseComponentSerializer
039    {
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                    org.apache.commons.logging.LogFactory.getLog(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                org.apache.commons.logging.LogFactory.getLog(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                org.apache.commons.logging.LogFactory.getLog(CaseComponentSerializer.class).error(e);
132                
133            }
134            return null;
135        }
136    }