1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
}
}
|