15
15
*/
16
16
package org .utplsql .sqldev .model ;
17
17
18
+ import java .io .IOException ;
18
19
import java .io .StringWriter ;
19
20
import java .util .logging .Logger ;
20
21
21
22
import javax .xml .XMLConstants ;
23
+ import javax .xml .parsers .DocumentBuilder ;
24
+ import javax .xml .parsers .DocumentBuilderFactory ;
25
+ import javax .xml .parsers .ParserConfigurationException ;
22
26
import javax .xml .transform .OutputKeys ;
23
27
import javax .xml .transform .Transformer ;
24
28
import javax .xml .transform .TransformerException ;
32
36
import javax .xml .xpath .XPathFactory ;
33
37
34
38
import org .utplsql .sqldev .exception .GenericRuntimeException ;
39
+ import org .w3c .dom .Document ;
35
40
import org .w3c .dom .Element ;
36
41
import org .w3c .dom .NamedNodeMap ;
37
42
import org .w3c .dom .Node ;
38
43
import org .w3c .dom .NodeList ;
44
+ import org .xml .sax .InputSource ;
45
+ import org .xml .sax .SAXException ;
39
46
40
47
public class XMLTools {
41
48
private static final Logger logger = Logger .getLogger (XMLTools .class .getName ());
@@ -47,7 +54,7 @@ public NodeList getNodeList(final Node doc, final String xpathString) {
47
54
final XPathExpression expr = xpath .compile (xpathString );
48
55
return ((NodeList ) expr .evaluate (doc , XPathConstants .NODESET ));
49
56
} catch (XPathExpressionException e ) {
50
- final String msg = "XPathExpressionException for " + xpathString + " due to " + e . getMessage () ;
57
+ final String msg = "XPathExpressionException for " + xpathString + "." ;
51
58
logger .severe (() -> msg );
52
59
throw new GenericRuntimeException (msg , e );
53
60
}
@@ -58,7 +65,7 @@ public Node getNode(final Node doc, final String xpathString) {
58
65
final XPathExpression expr = xpath .compile (xpathString );
59
66
return ((Node ) expr .evaluate (doc , XPathConstants .NODE ));
60
67
} catch (XPathExpressionException e ) {
61
- final String msg = "XPathExpressionException for " + xpathString + " due to " + e . getMessage () ;
68
+ final String msg = "XPathExpressionException for " + xpathString + "." ;
62
69
logger .severe (() -> msg );
63
70
throw new GenericRuntimeException (msg , e );
64
71
}
@@ -90,7 +97,7 @@ public String nodeToString(final Node node, final String cdataSectionElements) {
90
97
final String result = writer .toString ();
91
98
return result .replaceAll ("<!\\ [CDATA\\ [\\ s*\\ ]\\ ]>" , "" );
92
99
} catch (TransformerException e ) {
93
- final String msg = "TransformerException for " + cdataSectionElements + " due to " + e . getMessage () ;
100
+ final String msg = "TransformerException for " + cdataSectionElements + "." ;
94
101
logger .severe (() -> msg );
95
102
throw new GenericRuntimeException (msg , e );
96
103
}
@@ -129,4 +136,26 @@ public Node getElementNode(final Node node, final String tagName) {
129
136
}
130
137
return resultNode ;
131
138
}
139
+
140
+ public DocumentBuilder createDocumentBuilder () {
141
+ DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
142
+ try {
143
+ factory .setFeature (XMLConstants .FEATURE_SECURE_PROCESSING , Boolean .TRUE );
144
+ return factory .newDocumentBuilder ();
145
+ } catch (ParserConfigurationException e ) {
146
+ final String msg = "Could not create no document builder." ;
147
+ logger .severe (() -> msg );
148
+ throw new GenericRuntimeException (msg , e );
149
+ }
150
+ }
151
+
152
+ public Document parse (final DocumentBuilder builder , final InputSource inputSource ) {
153
+ try {
154
+ return builder .parse (inputSource );
155
+ } catch (SAXException | IOException e ) {
156
+ final String msg = "Could not parse XML input." ;
157
+ logger .severe (() -> msg );
158
+ throw new GenericRuntimeException (msg , e );
159
+ }
160
+ }
132
161
}
0 commit comments