package com.c2kernel.persistency.outcome; import java.io.StringReader; import java.io.StringWriter; import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.xml.serialize.Method; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Document; import org.xml.sax.InputSource; import com.c2kernel.common.PersistencyException; import com.c2kernel.entity.C2KLocalObject; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.utils.Logger; public class Outcome implements C2KLocalObject { int mID = -1; String mData; String mSchemaType; int mSchemaVersion; static DocumentBuilder parser; static { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setNamespaceAware(false); try { parser = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { Logger.error(e); } } //id is the eventID public Outcome(int id, String data, String schemaType, int schemaVersion) { mID = id; mData = data; mSchemaType = schemaType; mSchemaVersion = schemaVersion; } public Outcome(String path, String data) throws PersistencyException { // derive all the meta data from the path StringTokenizer tok = new StringTokenizer(path,"/"); if (tok.countTokens() != 3 && !(tok.nextToken().equals("Outcome"))) throw new PersistencyException("Outcome() - Outcome path must have three components: "+path, null); mSchemaType = tok.nextToken(); String verstring = tok.nextToken(); String objId = tok.nextToken(); try { mSchemaVersion = Integer.parseInt(verstring); } catch (NumberFormatException ex) { throw new PersistencyException("Outcome() - Outcome version was an invalid number: "+verstring, null); } try { mID = Integer.parseInt(objId); } catch (NumberFormatException ex) { mID = -1; } mData = data; } public void setID(int ID) { mID = ID; } public int getID() { return mID; } @Override public void setName(String name) { try { mID = Integer.parseInt(name); } catch (NumberFormatException e) { Logger.error("Invalid id set on Outcome:"+name); } } @Override public String getName() { return String.valueOf(mID); } public void setData(String data) { mData = data; } public void setData(Document data) { mData = serialize(data, false); } public String getData() { return mData; } public void setSchemaType(String schemaType) { mSchemaType = schemaType; } public String getSchemaType() { return mSchemaType; } public void setSchemaURL(int schemaVersion) { mSchemaVersion = schemaVersion; } public int getSchemaVersion() { return mSchemaVersion; } public void setSchemaVersion(int schVer) { mSchemaVersion = schVer; } @Override public String getClusterType() { return ClusterStorage.OUTCOME; } // special script API methods /** * Parses the outcome into a DOM tree * @return a DOM Document */ public Document getDOM() { try { synchronized (parser) { return parser.parse(new InputSource(new StringReader(mData))); } } catch (Exception e) { Logger.error(e); return null; } } static public String serialize(Document doc, boolean prettyPrint) { String serializedDoc = null; OutputFormat format = new OutputFormat(Method.XML, null, prettyPrint); StringWriter stringOut = new StringWriter(); XMLSerializer serial = new XMLSerializer(stringOut, format); try { serial.asDOMSerializer(); serial.serialize(doc); } catch (java.io.IOException ex) { Logger.error(ex.toString()); } serializedDoc = stringOut.toString(); return serializedDoc; } }