1515 */
1616package org .utplsql .sqldev .model ;
1717
18+ import java .io .IOException ;
1819import java .io .StringWriter ;
1920import java .util .logging .Logger ;
2021
2122import javax .xml .XMLConstants ;
23+ import javax .xml .parsers .DocumentBuilder ;
24+ import javax .xml .parsers .DocumentBuilderFactory ;
25+ import javax .xml .parsers .ParserConfigurationException ;
2226import javax .xml .transform .OutputKeys ;
2327import javax .xml .transform .Transformer ;
2428import javax .xml .transform .TransformerException ;
3236import javax .xml .xpath .XPathFactory ;
3337
3438import org .utplsql .sqldev .exception .GenericRuntimeException ;
39+ import org .w3c .dom .Document ;
3540import org .w3c .dom .Element ;
3641import org .w3c .dom .NamedNodeMap ;
3742import org .w3c .dom .Node ;
3843import org .w3c .dom .NodeList ;
44+ import org .xml .sax .InputSource ;
45+ import org .xml .sax .SAXException ;
3946
4047public class XMLTools {
4148 private static final Logger logger = Logger .getLogger (XMLTools .class .getName ());
@@ -47,7 +54,7 @@ public NodeList getNodeList(final Node doc, final String xpathString) {
4754 final XPathExpression expr = xpath .compile (xpathString );
4855 return ((NodeList ) expr .evaluate (doc , XPathConstants .NODESET ));
4956 } catch (XPathExpressionException e ) {
50- final String msg = "XPathExpressionException for " + xpathString + " due to " + e . getMessage () ;
57+ final String msg = "XPathExpressionException for " + xpathString + "." ;
5158 logger .severe (() -> msg );
5259 throw new GenericRuntimeException (msg , e );
5360 }
@@ -58,7 +65,7 @@ public Node getNode(final Node doc, final String xpathString) {
5865 final XPathExpression expr = xpath .compile (xpathString );
5966 return ((Node ) expr .evaluate (doc , XPathConstants .NODE ));
6067 } catch (XPathExpressionException e ) {
61- final String msg = "XPathExpressionException for " + xpathString + " due to " + e . getMessage () ;
68+ final String msg = "XPathExpressionException for " + xpathString + "." ;
6269 logger .severe (() -> msg );
6370 throw new GenericRuntimeException (msg , e );
6471 }
@@ -90,7 +97,7 @@ public String nodeToString(final Node node, final String cdataSectionElements) {
9097 final String result = writer .toString ();
9198 return result .replaceAll ("<!\\ [CDATA\\ [\\ s*\\ ]\\ ]>" , "" );
9299 } catch (TransformerException e ) {
93- final String msg = "TransformerException for " + cdataSectionElements + " due to " + e . getMessage () ;
100+ final String msg = "TransformerException for " + cdataSectionElements + "." ;
94101 logger .severe (() -> msg );
95102 throw new GenericRuntimeException (msg , e );
96103 }
@@ -129,4 +136,26 @@ public Node getElementNode(final Node node, final String tagName) {
129136 }
130137 return resultNode ;
131138 }
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+ }
132161}
0 commit comments