diff options
Diffstat (limited to 'source/com/c2kernel/persistency/outcome/Outcome.java')
| -rwxr-xr-x | source/com/c2kernel/persistency/outcome/Outcome.java | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/source/com/c2kernel/persistency/outcome/Outcome.java b/source/com/c2kernel/persistency/outcome/Outcome.java new file mode 100755 index 0000000..f919230 --- /dev/null +++ b/source/com/c2kernel/persistency/outcome/Outcome.java @@ -0,0 +1,159 @@ +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;
+ }
+
+ public void setName(String name) {
+ try {
+ mID = Integer.parseInt(name);
+ } catch (NumberFormatException e) {
+ Logger.error("Invalid id set on Outcome:"+name);
+ }
+ }
+
+ 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;
+ }
+
+ 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;
+ }
+}
|
