es.ucm.fdi.gaia.jcolibri.connector.xmlutils.QuerySerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jCOLIBRI Show documentation
Show all versions of jCOLIBRI Show documentation
jCOLIBRI is a java framework for the development of Case-Based Reasoning systems.
/**
* QuerySerializer.java
* jCOLIBRI2 framework.
* @author Juan A. Recio-Garc�a.
* GAIA - Group for Artificial Intelligence Applications
* http://gaia.fdi.ucm.es
* 04/11/2007
*/
package es.ucm.fdi.gaia.jcolibri.connector.xmlutils;
import java.io.FileWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery;
import es.ucm.fdi.gaia.jcolibri.util.FileIO;
/**
* Utility class to serialize queries into xml files.
* @author Juan A. Recio-Garcia
* @version 1.0
*
*/
public class QuerySerializer
{
public static void serializeQuery(CBRQuery query, String filename)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element root = doc.createElement("CBRQuery");
String id = "null";
Object idObject = query.getID();
if(idObject != null)
id = idObject.toString();
root.setAttribute("Id", id);
root.appendChild(CaseComponentSerializer.serializeCaseComponent(query.getDescription(),"Description",doc));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
FileWriter fw = new FileWriter(filename);
trans.transform(new DOMSource(root), new StreamResult(fw));
} catch (Exception e)
{
LogManager.getLogger(CaseComponentSerializer.class).error(e);
}
}
public static CBRQuery deserializeQuery(String filename)
{
CBRQuery query = new CBRQuery();
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(FileIO.openFile(filename));
Node node = doc.getElementsByTagName("CaseComponent").item(0);
query.setDescription(CaseComponentSerializer.deserializeCaseComponent(node));
} catch (Exception e)
{
LogManager.getLogger(CaseComponentSerializer.class).error(e);
}
return query;
}
}