1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.reporting;
25
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.io.StringWriter;
29
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
33
34 import org.apache.xml.serialize.OutputFormat;
35 import org.apache.xml.serialize.XMLSerializer;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.InputSource;
41 import org.xml.sax.SAXException;
42
43
44
45
46
47
48
49
50
51
52 public abstract class SerializationXmlUtils {
53
54
55
56
57
58
59
60
61 private static final String[][] XML_CHARS_TO_ESCAPE = {
62 {"&", "&"},
63 {"<", "<"},
64 {">", ">"},
65 {"'", "'"},
66 {"\"", """}};
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public static Document createDocument (String xmlString)
82 throws InitializationException {
83
84 StringReader reader = new StringReader (xmlString.trim ());
85 InputSource inputSource = new InputSource (reader);
86
87 DocumentBuilderFactory domFactory = DocumentBuilderFactory
88 .newInstance ();
89 Document document = null;
90
91 try {
92 DocumentBuilder docBuilder = domFactory.newDocumentBuilder ();
93 document = docBuilder.parse (inputSource);
94 } catch (ParserConfigurationException e) {
95 throw new InitializationException (
96 "Could not parse serialized verifier.", e);
97 } catch (SAXException e) {
98 throw new InitializationException (
99 "Could not parse serialized verifier.", e);
100 } catch (IOException e) {
101 throw new InitializationException (
102 "Could not parse serialized verifier.", e);
103 }
104
105 return document;
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public static String getTextValue (Element element) {
125
126 String textValue = null;
127
128 NodeList textNodes = element.getChildNodes ();
129 for (int i = 0; i < textNodes.getLength (); i++ ) {
130 Node textNode = textNodes.item (i);
131 if (textNode.getNodeType () == Node.TEXT_NODE) {
132 textValue = textNode.getNodeValue ();
133 break;
134 }
135 }
136
137 return textValue;
138 }
139
140
141
142
143
144
145
146
147 public static String escapeSpecialChars (String unescapedString) {
148
149 StringBuffer escapedString = new StringBuffer(unescapedString);
150
151
152 for (int i = 0; i < XML_CHARS_TO_ESCAPE.length; i++) {
153 int charIndex = escapedString.indexOf(XML_CHARS_TO_ESCAPE[i][0]);
154 if (charIndex >= 0) {
155 while (charIndex != -1) {
156 escapedString.replace (charIndex, charIndex + 1,
157 XML_CHARS_TO_ESCAPE[i][1]);
158 charIndex = escapedString
159 .indexOf (XML_CHARS_TO_ESCAPE[i][0], charIndex
160 + XML_CHARS_TO_ESCAPE[i][1].length ());
161 }
162 }
163 }
164
165 return escapedString.toString();
166 }
167
168
169
170
171
172
173
174
175
176
177
178 public static String serializeXmlElement (Element element)
179 throws InitializationException {
180
181
182 OutputFormat format = new OutputFormat(element.getOwnerDocument());
183 format.setOmitXMLDeclaration(true);
184
185
186 StringWriter writer = new StringWriter();
187 XMLSerializer serializer = new XMLSerializer(writer, format);
188
189 try {
190 serializer.asDOMSerializer();
191 serializer.serialize(element);
192 } catch (IOException ie) {
193
194 throw new InitializationException ("Could not serialize element.",
195 ie);
196 }
197
198 return writer.toString();
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public static String getSerializedObjectClassName (
214 Document serializedVerifier)
215 throws InitializationException {
216
217
218
219 Element docElement = serializedVerifier.getDocumentElement ();
220 NodeList classElements = docElement.getElementsByTagName ("class");
221
222
223 if (classElements.getLength () == 0) {
224 throw new InitializationException (
225 "Ill formatted verifier / job string: Verifier or Job " +
226 "class name not found.");
227 }
228
229
230 Element classElement = (Element) classElements.item (0);
231 String className = SerializationXmlUtils.getTextValue (classElement);
232
233 if (null == className || className.trim ().length () == 0) {
234
235 throw new InitializationException (
236 "Ill formatted verifier / job string: Verifier or Job " +
237 "class name not found.");
238 }
239
240 return className;
241 }
242
243 }