package com.c2kernel.persistency.outcome; import java.io.StringReader; import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; import org.xml.sax.InputSource; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.common.PersistencyException; import com.c2kernel.entity.C2KLocalObject; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.utils.LocalObjectLoader; import com.c2kernel.utils.Logger; public class Outcome implements C2KLocalObject { int mID = -1; String mData; String mSchemaType; int mSchemaVersion; Document dom; static DocumentBuilder parser; static DOMImplementationLS impl; static { // Set up parser DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setNamespaceAware(false); try { parser = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { Logger.error(e); Logger.die("Cannot function without XML parser"); } // Set up serialiser try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS)registry.getDOMImplementation("LS"); } catch (Exception e) { Logger.error(e); Logger.die("Cannot function without XML serialiser"); } } //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; dom = null; } public void setData(Document data) { mData = serialize(data, false); dom = data; } public String getData() { return mData; } public Schema getSchema() throws ObjectNotFoundException { return LocalObjectLoader.getSchema(mSchemaType, mSchemaVersion); } public void setSchemaType(String schemaType) { mSchemaType = schemaType; } public String getSchemaType() { return mSchemaType; } 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() { if (dom == null) try { synchronized (parser) { if (mData!=null) dom = parser.parse(new InputSource(new StringReader(mData))); else dom = parser.newDocument(); } } catch (Exception e) { Logger.error(e); return null; } return dom; } public String getField(String name) { if (getDOM() == null) return null; if (getDOM().getDocumentElement() == null) return null; NodeList elements = getDOM().getDocumentElement().getElementsByTagName(name); if (elements.getLength() == 1 && elements.item(0).hasChildNodes() && elements.item(0).getFirstChild() instanceof Text) return ((Text)elements.item(0).getFirstChild()).getData(); else return null; } static public String serialize(Document doc, boolean prettyPrint) { LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", prettyPrint); writer.getDomConfig().setParameter("xml-declaration", false); return writer.writeToString(doc); } }