Mercurial > hg > mpdl-group
annotate software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/XmlUtil.java @ 6:2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 08 Feb 2011 14:54:09 +0100 |
parents | 408254cf2f1d |
children | 1ec29fdd0db8 |
rev | line source |
---|---|
0 | 1 package de.mpg.mpiwg.berlin.mpdl.util; |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import java.io.Reader; | |
6 import java.io.StringReader; | |
7 import java.io.StringWriter; | |
6
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
8 import java.io.Writer; |
0 | 9 import java.net.URL; |
10 import java.text.DateFormat; | |
11 import java.text.ParseException; | |
12 import java.text.SimpleDateFormat; | |
13 import java.util.ArrayList; | |
14 import java.util.Date; | |
15 | |
16 import javax.xml.XMLConstants; | |
17 import javax.xml.namespace.NamespaceContext; | |
18 import javax.xml.parsers.DocumentBuilder; | |
19 import javax.xml.parsers.DocumentBuilderFactory; | |
20 import javax.xml.transform.OutputKeys; | |
21 import javax.xml.transform.Source; | |
22 import javax.xml.transform.Transformer; | |
23 import javax.xml.transform.TransformerConfigurationException; | |
24 import javax.xml.transform.TransformerException; | |
25 import javax.xml.transform.TransformerFactory; | |
26 import javax.xml.transform.dom.DOMSource; | |
27 import javax.xml.transform.sax.SAXSource; | |
28 import javax.xml.transform.stream.StreamResult; | |
29 import javax.xml.validation.Schema; | |
30 import javax.xml.validation.SchemaFactory; | |
31 import javax.xml.validation.Validator; | |
32 import javax.xml.xpath.XPath; | |
33 import javax.xml.xpath.XPathConstants; | |
34 import javax.xml.xpath.XPathFactory; | |
35 | |
36 import net.sf.saxon.om.NodeInfo; | |
37 | |
38 import org.w3c.dom.Document; | |
6
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
39 import org.w3c.dom.DocumentType; |
0 | 40 import org.w3c.dom.NamedNodeMap; |
41 import org.w3c.dom.Node; | |
42 import org.w3c.dom.NodeList; | |
43 import org.xml.sax.InputSource; | |
44 import org.xml.sax.SAXException; | |
45 | |
46 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
47 | |
48 public class XmlUtil { | |
49 static String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; | |
50 static String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; | |
51 static String W3C_XML_SCHEMA = XMLConstants.W3C_XML_SCHEMA_NS_URI; | |
52 | |
53 public static XmlUtil getInstance() { | |
54 return new XmlUtil(); | |
55 } | |
56 | |
57 public Node doc(String url) throws ApplicationException { | |
58 Node root = null; | |
59 try { | |
60 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
61 dbf.setNamespaceAware(true); | |
62 DocumentBuilder db = dbf.newDocumentBuilder(); | |
63 InputSource inputSource = new InputSource(url); | |
64 Document doc = db.parse(inputSource); | |
65 root = doc.getFirstChild(); | |
66 } catch (Exception e) { | |
67 throw new ApplicationException(e); | |
68 } | |
69 return root; | |
70 } | |
71 | |
72 public Node parse(String xmlFileName) throws ApplicationException { | |
73 File xmlFile = new File(xmlFileName); | |
74 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
75 Node retNode = null; | |
76 try { | |
77 retNode = xmlUtil.doc(xmlFile); | |
78 } catch (ApplicationException e) { | |
79 throw new ApplicationException("Your source file is not valid: " + e.getMessage()); | |
80 } | |
81 return retNode; | |
82 } | |
83 | |
84 public Node doc(File xmlFile) throws ApplicationException { | |
85 Node root = null; | |
86 try { | |
87 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
88 dbf.setNamespaceAware(true); | |
89 DocumentBuilder db = dbf.newDocumentBuilder(); | |
90 Document doc = db.parse(xmlFile); | |
91 root = doc.getFirstChild(); | |
92 } catch (Exception e) { | |
93 throw new ApplicationException(e); | |
94 } | |
95 return root; | |
96 } | |
97 | |
98 public void validateByRelaxNG(File xmlFile, URL schemaUrl) throws ApplicationException { | |
99 System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); | |
100 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); | |
101 Schema schema = null; | |
102 try { | |
103 schema = factory.newSchema(schemaUrl); | |
104 } catch (SAXException e) { | |
105 throw new ApplicationException(e); | |
106 } | |
107 Validator validator = schema.newValidator(); | |
108 InputSource inputSource = new InputSource(xmlFile.getPath()); | |
109 Source source = new SAXSource(inputSource); | |
110 try { | |
111 validator.validate(source); | |
112 } catch (SAXException e) { | |
113 String message = e.getMessage(); | |
114 String text = "Your file is not valid against the RelaxNG schema: " + schemaUrl; | |
115 throw new ApplicationException(text + ":\n" + message); | |
116 } catch (IOException e) { | |
117 String message = e.getMessage(); | |
118 String text = "Your file is not valid against the RelaxNG schema: " + schemaUrl; | |
119 throw new ApplicationException(text + ": " + message); | |
120 } | |
121 } | |
122 | |
123 public String getNodeValue(Node node) { | |
124 String nodeValueStr = node.getNodeValue(); | |
125 if (nodeValueStr == null) | |
126 nodeValueStr = node.getTextContent(); | |
127 return nodeValueStr; | |
128 } | |
129 | |
130 public String getNodeAttributeValue(Node node, String attrName) { | |
131 NamedNodeMap attrs = node.getAttributes(); | |
132 if (attrs == null) { | |
133 return null; | |
134 } | |
135 Node attN = attrs.getNamedItem(attrName); | |
136 if (attN == null) { | |
137 return null; | |
138 } | |
139 return attN.getNodeValue(); | |
140 } | |
141 | |
142 public ArrayList<String> toStringArray(NodeList nodes) { | |
143 ArrayList<String> nodeValues = null; | |
144 for (int i=0; i< nodes.getLength(); i++) { | |
145 Node node = nodes.item(i); | |
146 if (nodeValues == null) | |
147 nodeValues = new ArrayList<String>(); | |
148 String nodeValue = node.getNodeValue(); | |
149 nodeValues.add(nodeValue); | |
150 } | |
151 return nodeValues; | |
152 } | |
153 | |
154 public String toXsDate(Date date) { | |
155 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
156 String xsDateStr = dateFormat.format(date); | |
157 return xsDateStr; | |
158 } | |
159 | |
160 public Date toDate(String xsDateStr) throws ApplicationException { | |
161 Date retDate = null; | |
162 if (xsDateStr == null) | |
163 return null; | |
164 try { | |
165 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
166 retDate = dateFormat.parse(xsDateStr); | |
167 } catch (ParseException e) { | |
168 throw new ApplicationException(e); | |
169 } | |
170 return retDate; | |
171 } | |
172 | |
173 public String evaluateToString(String xmlString, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { | |
174 String resultStr = null; | |
175 ArrayList<String> strArray = evaluateToStringArray(xmlString, xpathExpression, nsContext); | |
176 if (strArray != null && strArray.size() > 0) | |
177 resultStr = strArray.get(0); | |
178 return resultStr; | |
179 } | |
180 | |
181 public String evaluateToString(InputSource inputSource, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { | |
182 String resultStr = null; | |
183 ArrayList<String> strArray = evaluateToStringArray(inputSource, xpathExpression, nsContext); | |
184 if (strArray != null && strArray.size() > 0) | |
185 resultStr = strArray.get(0); | |
186 return resultStr; | |
187 } | |
188 | |
189 public String evaluateToString(Node node, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { | |
190 String resultStr = null; | |
191 ArrayList<String> strArray = evaluateToStringArray(node, xpathExpression, nsContext); | |
192 if (strArray != null && strArray.size() > 0) | |
193 resultStr = strArray.get(0); | |
194 return resultStr; | |
195 } | |
196 | |
197 public ArrayList<String> evaluateToStringArray(String xmlString, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { | |
198 Reader stringReader = new StringReader(xmlString); | |
199 InputSource inputSource = new InputSource(stringReader); | |
200 ArrayList<String> retStrArray = evaluateToStringArray(inputSource, xpathExpression, nsContext); | |
201 return retStrArray; | |
202 } | |
203 | |
204 public ArrayList<String> evaluateToStringArray(InputSource inputSource, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { | |
205 ArrayList<String> retStrArray = null; | |
206 try { | |
207 XPath xpath = XPathFactory.newInstance().newXPath(); | |
208 if (nsContext != null) | |
209 xpath.setNamespaceContext(nsContext); | |
210 Object resultObjects = xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET); | |
211 if (resultObjects != null) { | |
212 retStrArray = nodesetToStringArray(resultObjects); | |
213 } | |
214 } catch (Exception e) { | |
215 throw new ApplicationException(e); | |
216 } | |
217 return retStrArray; | |
218 } | |
219 | |
220 public ArrayList<Node> evaluateToNodeArray(InputSource inputSource, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { | |
221 ArrayList<Node> retArray = null; | |
222 try { | |
223 XPath xpath = XPathFactory.newInstance().newXPath(); | |
224 if (nsContext != null) | |
225 xpath.setNamespaceContext(nsContext); | |
226 Object resultObjects = xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET); | |
227 if (resultObjects != null) { | |
228 retArray = nodesetToNodeArray(resultObjects); | |
229 } | |
230 } catch (Exception e) { | |
231 throw new ApplicationException(e); | |
232 } | |
233 return retArray; | |
234 } | |
235 | |
236 public ArrayList<String> evaluateToStringArray(Node node, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { | |
237 ArrayList<String> retStrArray = null; | |
238 try { | |
239 XPath xpath = XPathFactory.newInstance().newXPath(); | |
240 if (nsContext != null) | |
241 xpath.setNamespaceContext(nsContext); | |
242 Object resultObjects = xpath.evaluate(xpathExpression, node, XPathConstants.NODESET); | |
243 if (resultObjects != null) { | |
244 retStrArray = nodesetToStringArray(resultObjects); | |
245 } | |
246 } catch (Exception e) { | |
247 throw new ApplicationException(e); | |
248 } | |
249 return retStrArray; | |
250 } | |
251 | |
252 /* | |
253 * XPath evaluation: handles both, javax and also Saxon's implementation | |
254 * javax XPath evaluation: returns a NodeList | |
255 * Saxon's XPath evaluation: returns an ArrayList of TinyTextImpl (which could be casted to NodeInfo which could be handled as if it was a dom node) | |
256 */ | |
257 private ArrayList<String> nodesetToStringArray(Object nodesetObjects) { | |
258 ArrayList<String> retStrArray = null; | |
259 if (nodesetObjects instanceof NodeList) { | |
260 NodeList resultNodeList = (NodeList) nodesetObjects; | |
261 int length = resultNodeList.getLength(); | |
262 if (length > 0) { | |
263 retStrArray = new ArrayList<String>(); | |
264 for (int i=0; i<length; i++) { | |
265 Node n = resultNodeList.item(i); | |
266 String nodeValue = n.getNodeValue(); | |
267 if (nodeValue == null) | |
268 nodeValue = n.getTextContent(); | |
269 if (nodeValue != null) | |
270 retStrArray.add(nodeValue); | |
271 } | |
272 } | |
273 } else if (nodesetObjects instanceof ArrayList) { | |
274 ArrayList arrayListNodes = (ArrayList) nodesetObjects; | |
275 retStrArray = new ArrayList<String>(); | |
276 for (int i=0; i<arrayListNodes.size(); i++) { | |
277 Object arrayListNode = arrayListNodes.get(i); | |
278 if (arrayListNode instanceof Node) { | |
279 Node n = (Node) arrayListNode; | |
280 String nodeValue = n.getNodeValue(); | |
281 if (nodeValue == null) | |
282 nodeValue = n.getTextContent(); | |
283 if (nodeValue != null) | |
284 retStrArray.add(nodeValue); | |
285 } else if (arrayListNode instanceof NodeInfo) { | |
286 NodeInfo n = (NodeInfo) arrayListNode; | |
287 String nodeValue = n.getStringValue(); | |
288 retStrArray.add(nodeValue); | |
289 } | |
290 } | |
291 } | |
292 return retStrArray; | |
293 } | |
294 | |
295 /* | |
296 * XPath evaluation: handles both, javax and also Saxon's implementation | |
297 * javax XPath evaluation: returns a NodeList | |
298 * Saxon's XPath evaluation: returns an ArrayList of TinyTextImpl (which could be casted to NodeInfo which could be handled as if it was a dom node) | |
299 */ | |
300 private ArrayList<Node> nodesetToNodeArray(Object nodesetObjects) { | |
301 ArrayList<Node> retArray = null; | |
302 if (nodesetObjects instanceof NodeList) { | |
303 NodeList resultNodeList = (NodeList) nodesetObjects; | |
304 int length = resultNodeList.getLength(); | |
305 if (length > 0) { | |
306 retArray = new ArrayList<Node>(); | |
307 for (int i=0; i<length; i++) { | |
308 Node n = resultNodeList.item(i); | |
309 retArray.add(n); | |
310 } | |
311 } | |
312 } else if (nodesetObjects instanceof ArrayList) { | |
313 ArrayList arrayListNodes = (ArrayList) nodesetObjects; | |
314 retArray = new ArrayList<Node>(); | |
315 for (int i=0; i<arrayListNodes.size(); i++) { | |
316 Object arrayListNode = arrayListNodes.get(i); | |
317 if (arrayListNode instanceof Node) { | |
318 Node n = (Node) arrayListNode; | |
319 retArray.add(n); | |
320 } else if (arrayListNode instanceof NodeInfo) { | |
321 NodeInfo n = (NodeInfo) arrayListNode; | |
322 // TODO provide clean return value | |
323 } | |
324 } | |
325 } | |
326 return retArray; | |
327 } | |
328 | |
6
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
329 |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
330 /* |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
331 * XPath evaluation: handles both, javax and also Saxon's implementation |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
332 * javax XPath evaluation: returns a NodeList |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
333 * Saxon's XPath evaluation: returns an ArrayList of TinyTextImpl (which could be casted to NodeInfo which could be handled as if it was a dom node) |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
334 */ |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
335 private String nodesetToXmlString(Object nodesetObjects) throws ApplicationException { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
336 Writer writer = new StringWriter(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
337 try { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
338 if (nodesetObjects instanceof NodeList) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
339 NodeList resultNodeList = (NodeList) nodesetObjects; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
340 int length = resultNodeList.getLength(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
341 if (length > 0) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
342 for (int i=0; i<length; i++) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
343 Node n = resultNodeList.item(i); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
344 serializeNode(n, writer, ""); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
345 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
346 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
347 } else if (nodesetObjects instanceof ArrayList) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
348 ArrayList arrayListNodes = (ArrayList) nodesetObjects; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
349 for (int i=0; i<arrayListNodes.size(); i++) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
350 Object arrayListNode = arrayListNodes.get(i); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
351 if (arrayListNode instanceof Node) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
352 Node n = (Node) arrayListNode; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
353 serializeNode(n, writer, ""); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
354 } else if (arrayListNode instanceof NodeInfo) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
355 NodeInfo n = (NodeInfo) arrayListNode; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
356 writer.write(n.getStringValue()); // TODO if that really happens |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
357 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
358 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
359 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
360 writer.flush(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
361 } catch (IOException e) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
362 throw new ApplicationException(e); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
363 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
364 return writer.toString(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
365 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
366 |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
367 public String evaluateToXmlString(String xmlString, String xpathExpression, NamespaceContext nsContext) throws ApplicationException { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
368 String resultStr = null; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
369 try { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
370 XPath xpath = XPathFactory.newInstance().newXPath(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
371 if (nsContext != null) |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
372 xpath.setNamespaceContext(nsContext); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
373 Reader stringReader = new StringReader(xmlString); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
374 InputSource inputSource = new InputSource(stringReader); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
375 Object resultObjects = xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
376 if (resultObjects != null) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
377 resultStr = nodesetToXmlString(resultObjects); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
378 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
379 } catch (Exception e) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
380 throw new ApplicationException(e); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
381 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
382 return resultStr; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
383 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
384 |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
385 /** |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
386 * <p> This will serialize a DOM <code>Node</code> to |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
387 * the supplied <code>Writer</code>. </p> |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
388 * |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
389 * @param node DOM <code>Node</code> to serialize. |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
390 * @param writer <code>Writer</code> to write to. |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
391 * @param indentLevel current indentation. |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
392 */ |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
393 private void serializeNode(Node node, Writer writer, String indentLevel) throws ApplicationException { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
394 try { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
395 // Determine action based on node type |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
396 switch (node.getNodeType()) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
397 case Node.DOCUMENT_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
398 writer.write("<?xml version=\"1.0\"?>"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
399 writer.write("\n"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
400 // recurse on each child |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
401 NodeList nodes = node.getChildNodes(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
402 if (nodes != null) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
403 for (int i=0; i<nodes.getLength(); i++) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
404 serializeNode(nodes.item(i), writer, ""); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
405 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
406 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
407 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
408 case Node.ELEMENT_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
409 String name = node.getNodeName(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
410 writer.write(indentLevel + "<" + name); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
411 NamedNodeMap attributes = node.getAttributes(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
412 for (int i=0; i<attributes.getLength(); i++) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
413 Node current = attributes.item(i); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
414 writer.write(" " + current.getNodeName() + "=\"" + current.getNodeValue() + "\""); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
415 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
416 writer.write(">"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
417 // recurse on each child |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
418 NodeList children = node.getChildNodes(); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
419 if (children != null) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
420 if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE)) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
421 writer.write("\n"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
422 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
423 for (int i=0; i<children.getLength(); i++) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
424 serializeNode(children.item(i), writer, indentLevel + " "); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
425 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
426 if ((children.item(0) != null) && (children.item(children.getLength()-1).getNodeType() == Node.ELEMENT_NODE)) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
427 writer.write(indentLevel); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
428 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
429 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
430 writer.write("</" + name + ">"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
431 writer.write("\n"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
432 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
433 case Node.TEXT_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
434 writer.write(node.getNodeValue()); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
435 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
436 case Node.CDATA_SECTION_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
437 writer.write("<![CDATA[" + node.getNodeValue() + "]]>"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
438 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
439 case Node.COMMENT_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
440 writer.write(indentLevel + "<!-- " + node.getNodeValue() + " -->"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
441 writer.write("\n"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
442 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
443 case Node.PROCESSING_INSTRUCTION_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
444 writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
445 writer.write("\n"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
446 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
447 case Node.ENTITY_REFERENCE_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
448 writer.write("&" + node.getNodeName() + ";"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
449 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
450 case Node.DOCUMENT_TYPE_NODE: |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
451 DocumentType docType = (DocumentType)node; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
452 writer.write("<!DOCTYPE " + docType.getName()); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
453 if (docType.getPublicId() != null) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
454 System.out.print(" PUBLIC \"" + docType.getPublicId() + "\" "); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
455 } else { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
456 writer.write(" SYSTEM "); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
457 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
458 writer.write("\"" + docType.getSystemId() + "\">"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
459 writer.write("\n"); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
460 break; |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
461 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
462 } catch (IOException e) { |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
463 throw new ApplicationException(e); |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
464 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
465 } |
2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
466 |
0 | 467 // TODO not used yet, test it |
468 public Node doc(File xmlFile, File schemaFile) throws ApplicationException { | |
469 Node root = null; | |
470 try { | |
471 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
472 dbf.setNamespaceAware(true); | |
473 // dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); // TODO vielleicht doch wieder anschalten | |
474 dbf.setAttribute(JAXP_SCHEMA_SOURCE, schemaFile); | |
475 DocumentBuilder db = dbf.newDocumentBuilder(); | |
476 Document doc = db.parse(xmlFile); | |
477 root = doc.getFirstChild(); | |
478 } catch (Exception e) { | |
479 throw new ApplicationException(e); | |
480 } | |
481 return root; | |
482 } | |
483 | |
484 // TODO not used yet, test it | |
485 public void validateByRelaxNG(Node docNode, URL schemaUrl) throws ApplicationException { | |
486 // System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory"); | |
487 System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); | |
488 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); | |
489 Schema schema = null; | |
490 try { | |
491 schema = factory.newSchema(schemaUrl); | |
492 } catch (SAXException e) { | |
493 throw new ApplicationException(e); | |
494 } | |
495 Validator validator = schema.newValidator(); | |
496 DOMSource source = new DOMSource(docNode); | |
497 try { | |
498 validator.validate(source); | |
499 } catch (SAXException e) { | |
500 throw new ApplicationException(e); | |
501 } catch (IOException e) { | |
502 throw new ApplicationException(e); | |
503 } | |
504 } | |
505 | |
506 // TODO not used yet, test it | |
507 public String toStringByTransformer(Node doc) throws ApplicationException { | |
508 String xmlString = null; | |
509 try { | |
510 Transformer transformer = TransformerFactory.newInstance().newTransformer(); | |
511 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
512 //initialize StreamResult with File object to save to file | |
513 StreamResult result = new StreamResult(new StringWriter()); | |
514 DOMSource source = new DOMSource(doc); | |
515 transformer.transform(source, result); | |
516 xmlString = result.getWriter().toString(); | |
517 } catch (TransformerConfigurationException e) { | |
518 throw new ApplicationException(e); | |
519 } catch (TransformerException e) { | |
520 throw new ApplicationException(e); | |
521 } | |
522 return xmlString; | |
523 } | |
524 } |