Moving DOM parsing code to DOMUtils
diff --git a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicy.java b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicy.java
index 22b5e47..f69309b 100644
--- a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicy.java
+++ b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicy.java
@@ -33,9 +33,6 @@
import java.io.File;
import java.util.Iterator;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.openaz.xacml.api.Identifier;
@@ -315,15 +312,11 @@
public static void main(String args[]) {
try {
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- documentBuilderFactory.setNamespaceAware(true);
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
-
for (String fileName : args) {
File filePolicy = new File(fileName);
if (filePolicy.exists() && filePolicy.canRead()) {
try {
- Document documentPolicy = documentBuilder.parse(filePolicy);
+ Document documentPolicy = DOMUtil.loadDocument(filePolicy);
if (documentPolicy.getFirstChild() == null) {
System.err.println(fileName + ": Error: No Policy found");
} else if (!XACML3.ELEMENT_POLICY.equals(documentPolicy.getFirstChild()
diff --git a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyDef.java b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyDef.java
index bb4661d..8193d2c 100644
--- a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyDef.java
+++ b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyDef.java
@@ -33,9 +33,6 @@
import java.io.File;
import java.io.InputStream;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
import org.apache.openaz.xacml.api.XACML3;
import org.apache.openaz.xacml.pdp.policy.PolicyDef;
import org.apache.openaz.xacml.pdp.policy.PolicySet;
@@ -56,10 +53,7 @@
throws DOMStructureException {
PolicyDef policyDef = null;
try {
- Node rootNode = document.getFirstChild();
- while (rootNode != null && rootNode.getNodeType() != Node.ELEMENT_NODE) {
- rootNode = rootNode.getNextSibling();
- }
+ Node rootNode = DOMUtil.getFirstChildElement(document);
if (rootNode == null) {
throw new Exception("No child in document");
}
@@ -88,31 +82,9 @@
}
public static PolicyDef load(InputStream inputStream) throws DOMStructureException {
- /*
- * Get the DocumentBuilderFactory
- */
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- if (documentBuilderFactory == null) {
- throw new DOMStructureException("No XML DocumentBuilderFactory configured");
- }
- documentBuilderFactory.setNamespaceAware(true);
-
- /*
- * Get the DocumentBuilder
- */
- DocumentBuilder documentBuilder = null;
- try {
- documentBuilder = documentBuilderFactory.newDocumentBuilder();
- } catch (Exception ex) {
- throw new DOMStructureException("Exception creating DocumentBuilder: " + ex.getMessage(), ex);
- }
-
- /*
- * Parse the XML file
- */
PolicyDef policyDef = null;
try {
- Document document = documentBuilder.parse(inputStream);
+ Document document = DOMUtil.loadDocument(inputStream);
if (document == null) {
throw new Exception("Null document returned");
}
diff --git a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyRepair.java b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyRepair.java
index ed0bdf7..510373f 100644
--- a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyRepair.java
+++ b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicyRepair.java
@@ -36,9 +36,6 @@
import java.io.InputStream;
import java.io.OutputStream;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
import org.apache.openaz.xacml.api.XACML3;
import org.apache.openaz.xacml.std.dom.DOMUtil;
import org.w3c.dom.Document;
@@ -91,11 +88,8 @@
/*
* Get the XML Parser for the input file
*/
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- documentBuilderFactory.setNamespaceAware(true);
try {
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
- Document documentInput = documentBuilder.parse(inputStream);
+ Document documentInput = DOMUtil.loadDocument(inputStream);
Element elementRoot = DOMUtil.getFirstChildElement(documentInput);
if (elementRoot == null) {
System.err.println("No root element");
diff --git a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicySet.java b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicySet.java
index ea05ac3..8a374a5 100644
--- a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicySet.java
+++ b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/policy/dom/DOMPolicySet.java
@@ -33,9 +33,6 @@
import java.io.File;
import java.util.Iterator;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.openaz.xacml.api.Identifier;
@@ -358,15 +355,11 @@
public static void main(String args[]) {
try {
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- documentBuilderFactory.setNamespaceAware(true);
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
-
for (String fileName : args) {
File filePolicy = new File(fileName);
if (filePolicy.exists() && filePolicy.canRead()) {
try {
- Document documentPolicy = documentBuilder.parse(filePolicy);
+ Document documentPolicy = DOMUtil.loadDocument(filePolicy);
if (documentPolicy.getFirstChild() == null) {
System.err.println(fileName + ": Error: No PolicySet found");
} else if (!XACML3.ELEMENT_POLICYSET.equals(documentPolicy.getFirstChild()
diff --git a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/std/functions/FunctionDefinitionAccessPermitted.java b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/std/functions/FunctionDefinitionAccessPermitted.java
index 3825759..489b179 100644
--- a/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/std/functions/FunctionDefinitionAccessPermitted.java
+++ b/openaz-xacml-pdp/src/main/java/org/apache/openaz/xacml/pdp/std/functions/FunctionDefinitionAccessPermitted.java
@@ -35,8 +35,6 @@
import java.net.URI;
import java.util.List;
-import javax.xml.parsers.DocumentBuilderFactory;
-
import org.apache.openaz.xacml.api.Identifier;
import org.apache.openaz.xacml.api.Request;
import org.apache.openaz.xacml.api.RequestAttributes;
@@ -49,6 +47,8 @@
import org.apache.openaz.xacml.std.datatypes.DataTypes;
import org.apache.openaz.xacml.std.dom.DOMRequestAttributes;
import org.apache.openaz.xacml.std.dom.DOMStructureException;
+import org.apache.openaz.xacml.std.dom.DOMUtil;
+import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
@@ -147,10 +147,10 @@
// TODO - need to get Namespace info from original Request? How can I recover the original Namespace
// from the EvaluationContext?
try (InputStream is = new ByteArrayInputStream(xmlContent.getBytes())) {
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
- docBuilderFactory.setNamespaceAware(true);
-
- newContentNode = docBuilderFactory.newDocumentBuilder().parse(is).getDocumentElement();
+ Document doc = DOMUtil.loadDocument(is);
+ if (doc != null) {
+ newContentNode = doc.getDocumentElement();
+ }
} catch (Exception e) {
String message = e.getMessage();
if (e.getCause() != null) {
diff --git a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMRequest.java b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMRequest.java
index 6a52e86..f61db06 100644
--- a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMRequest.java
+++ b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMRequest.java
@@ -36,9 +36,6 @@
import java.io.InputStream;
import java.io.IOException;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.openaz.xacml.api.Request;
@@ -73,13 +70,11 @@
* @throws DOMStructureException
*/
public static Request load(String xmlString) throws DOMStructureException {
- Request request = null;
try (InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"))) {
- request = DOMRequest.load(is);
+ return DOMRequest.load(is);
} catch (IOException ex) {
throw new DOMStructureException("Exception loading String Request: " + ex.getMessage(), ex);
}
- return request;
}
/**
@@ -93,13 +88,11 @@
* @throws DOMStructureException
*/
public static Request load(File fileRequest) throws DOMStructureException {
- Request request = null;
try (FileInputStream fis = new FileInputStream(fileRequest)) {
- request = DOMRequest.load(fis);
+ return DOMRequest.load(fis);
} catch (IOException ex) {
throw new DOMStructureException("Exception loading File Request: " + ex.getMessage(), ex);
}
- return request;
}
/**
@@ -111,40 +104,14 @@
* @throws DOMStructureException
*/
public static Request load(InputStream is) throws DOMStructureException {
- /*
- * Get the DocumentBuilderFactory
- */
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- if (documentBuilderFactory == null) {
- throw new DOMStructureException("No XML DocumentBuilderFactory configured");
- }
- documentBuilderFactory.setNamespaceAware(true);
-
- /*
- * Get the DocumentBuilder
- */
- DocumentBuilder documentBuilder = null;
- try {
- documentBuilder = documentBuilderFactory.newDocumentBuilder();
- } catch (Exception ex) {
- throw new DOMStructureException("Exception creating DocumentBuilder: " + ex.getMessage(), ex);
- }
-
- /*
- * Parse the XML file
- */
- Document document = null;
Request request = null;
try {
- document = documentBuilder.parse(is);
+ Document document = DOMUtil.loadDocument(is);
if (document == null) {
throw new Exception("Null document returned");
}
- Node rootNode = document.getFirstChild();
- while (rootNode != null && rootNode.getNodeType() != Node.ELEMENT_NODE) {
- rootNode = rootNode.getNextSibling();
- }
+ Node rootNode = DOMUtil.getFirstChildElement(document);
if (rootNode == null) {
throw new Exception("No child in document");
}
@@ -330,8 +297,6 @@
*/
public static void main(String[] args) {
if (args.length > 0) {
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- documentBuilderFactory.setNamespaceAware(true);
for (String xmlFileName : args) {
File fileXml = new File(xmlFileName);
if (!fileXml.exists()) {
@@ -344,9 +309,7 @@
}
System.out.println(fileXml.getAbsolutePath() + ":");
try {
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
- assert documentBuilder.isNamespaceAware();
- Document documentRequest = documentBuilder.parse(fileXml);
+ Document documentRequest = DOMUtil.loadDocument(fileXml);
assert documentRequest != null;
NodeList children = documentRequest.getChildNodes();
diff --git a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMResponse.java b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMResponse.java
index 9c2a4df..9e23ce1 100644
--- a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMResponse.java
+++ b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMResponse.java
@@ -30,11 +30,10 @@
*/
package org.apache.openaz.xacml.std.dom;
-import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -48,8 +47,6 @@
import javax.security.auth.x500.X500Principal;
import javax.xml.XMLConstants;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -176,13 +173,11 @@
}
public static Response load(String xmlString) throws DOMStructureException {
- Response response = null;
try (InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"))) {
- response = load(is);
+ return load(is);
} catch (Exception ex) {
throw new DOMStructureException("Exception loading String Response: " + ex.getMessage(), ex);
}
- return response;
}
/**
@@ -195,14 +190,8 @@
* @throws DOMStructureException
*/
public static Response load(File fileResponse) throws DOMStructureException {
- try (BufferedReader br = new BufferedReader(new FileReader(fileResponse))) {
- String responseString = "";
- String line;
- while ((line = br.readLine()) != null) {
- responseString += line;
- }
- br.close();
- return load(responseString);
+ try (FileInputStream fis = new FileInputStream(fileResponse)) {
+ return DOMResponse.load(fis);
} catch (Exception e) {
throw new DOMStructureException("File: " + fileResponse.getName() + " " + e.getMessage());
}
@@ -233,40 +222,14 @@
* @throws DOMStructureException
*/
public static Response load(InputStream is) throws DOMStructureException {
- /*
- * Get the DocumentBuilderFactory
- */
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- if (documentBuilderFactory == null) {
- throw new DOMStructureException("No XML DocumentBuilderFactory configured");
- }
- documentBuilderFactory.setNamespaceAware(true);
-
- /*
- * Get the DocumentBuilder
- */
- DocumentBuilder documentBuilder = null;
- try {
- documentBuilder = documentBuilderFactory.newDocumentBuilder();
- } catch (Exception ex) {
- throw new DOMStructureException("Exception creating DocumentBuilder: " + ex.getMessage(), ex);
- }
-
- /*
- * Parse the XML file
- */
- Document document = null;
Response request = null;
try {
- document = documentBuilder.parse(is);
+ Document document = DOMUtil.loadDocument(is);
if (document == null) {
throw new Exception("Null document returned");
}
- Node rootNode = document.getFirstChild();
- while (rootNode != null && rootNode.getNodeType() != Node.ELEMENT_NODE) {
- rootNode = rootNode.getNextSibling();
- }
+ Node rootNode = DOMUtil.getFirstChildElement(document);
if (rootNode == null) {
throw new Exception("No child in document");
}
@@ -862,8 +825,6 @@
*/
public static void main(String[] args) {
if (args.length > 0) {
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- documentBuilderFactory.setNamespaceAware(true);
for (String xmlFileName : args) {
File fileXml = new File(xmlFileName);
if (!fileXml.exists()) {
@@ -876,9 +837,7 @@
}
System.out.println(fileXml.getAbsolutePath() + ":");
try {
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
- assert documentBuilder.isNamespaceAware();
- Document documentResponse = documentBuilder.parse(fileXml);
+ Document documentResponse = DOMUtil.loadDocument(fileXml);
assert documentResponse != null;
NodeList children = documentResponse.getChildNodes();
diff --git a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMUtil.java b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMUtil.java
index b70d195..f7f8b1b 100644
--- a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMUtil.java
+++ b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/dom/DOMUtil.java
@@ -179,21 +179,18 @@
* @param node the <code>Node</code> to search
* @return the first child <code>Element</code> of the given <code>Node</code>
*/
- public static Element getFirstChildElement(Node node) {
- NodeList children = null;
- int numChildren = 0;
- if (node == null || (children = node.getChildNodes()) == null
- || (numChildren = node.getChildNodes().getLength()) == 0) {
+ public static Element getFirstChildElement(Node rootNode) {
+ if (rootNode == null) {
return null;
}
- Element result = null;
- for (int i = 0; i < numChildren && result == null; i++) {
- Node child = children.item(i);
- if (child.getNodeType() == Node.ELEMENT_NODE) {
- result = (Element)child;
- }
+ Node node = rootNode.getFirstChild();
+ while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
+ node = node.getNextSibling();
}
- return result;
+ if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
+ return (Element)node;
+ }
+ return null;
}
protected static DOMStructureException newMissingAttributeException(Node node, String attributeName) {
diff --git a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpRequest.java b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpRequest.java
index 5378b3c..a010c9c 100644
--- a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpRequest.java
+++ b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpRequest.java
@@ -38,8 +38,6 @@
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributesType;
@@ -49,6 +47,8 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.openaz.xacml.std.StdMutableRequest;
+import org.apache.openaz.xacml.std.dom.DOMStructureException;
+import org.apache.openaz.xacml.std.dom.DOMUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -101,24 +101,15 @@
* @throws java.io.IOException
* @throws org.xml.sax.SAXException
* @throws javax.xml.bind.JAXBException
+ * @throws DOMStructureException
*/
public static JaxpRequest load(File fileXmlRequest) throws ParserConfigurationException, IOException,
- SAXException, JAXBException {
+ SAXException, JAXBException, DOMStructureException {
if (fileXmlRequest == null) {
throw new NullPointerException("Null File");
}
- /*
- * Create XML document factory and builder
- */
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- documentBuilderFactory.setNamespaceAware(true);
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
-
- /*
- * Parse the file into a Document
- */
- Document document = documentBuilder.parse(fileXmlRequest);
+ Document document = DOMUtil.loadDocument(fileXmlRequest);
if (document == null) {
logger.error("No Document returned parsing \"" + fileXmlRequest.getAbsolutePath() + "\"");
return null;
diff --git a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpResponse.java b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpResponse.java
index 2b608ec..651b425 100644
--- a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpResponse.java
+++ b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/jaxp/JaxpResponse.java
@@ -38,8 +38,6 @@
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ResponseType;
@@ -48,6 +46,8 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.openaz.xacml.std.StdMutableResponse;
+import org.apache.openaz.xacml.std.dom.DOMStructureException;
+import org.apache.openaz.xacml.std.dom.DOMUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -88,24 +88,15 @@
* @throws java.io.IOException
* @throws org.xml.sax.SAXException
* @throws javax.xml.bind.JAXBException
+ * @throws DOMStructureException
*/
public static JaxpResponse load(File fileXmlResponse) throws ParserConfigurationException, IOException,
- SAXException, JAXBException {
+ SAXException, JAXBException, DOMStructureException {
if (fileXmlResponse == null) {
throw new NullPointerException("Null File");
}
- /*
- * Create XML document factory and builder
- */
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- documentBuilderFactory.setNamespaceAware(true);
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
-
- /*
- * Parse the file into a Document
- */
- Document document = documentBuilder.parse(fileXmlResponse);
+ Document document = DOMUtil.loadDocument(fileXmlResponse);
if (document == null) {
logger.error("No Document returned parsing \"" + fileXmlResponse.getAbsolutePath() + "\"");
return null;
diff --git a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONRequest.java b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONRequest.java
index f408bf7..3dee3af 100644
--- a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONRequest.java
+++ b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONRequest.java
@@ -53,9 +53,6 @@
import javax.security.auth.x500.X500Principal;
import javax.xml.XMLConstants;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
@@ -88,6 +85,7 @@
import org.apache.openaz.xacml.std.datatypes.ExtendedNamespaceContext;
import org.apache.openaz.xacml.std.datatypes.StringNamespaceContext;
import org.apache.openaz.xacml.std.datatypes.XPathExpressionWrapper;
+import org.apache.openaz.xacml.std.dom.DOMUtil;
import org.apache.openaz.xacml.util.FactoryException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
@@ -585,22 +583,6 @@
}
//
- // Create XML document factory and builder
- //
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db;
- try {
- db = dbf.newDocumentBuilder();
- } catch (ParserConfigurationException e1) {
- throw new JSONStructureException("Content unable to setup Parser Configuration");
- }
- //
- // Parse the content
- //
- Document doc = null;
-
- //
// First of all, the String is possible escaped.
//
// The meaning of "escaped" is defined in section 4.2.3.1 in the JSON spec
@@ -611,14 +593,14 @@
// logger.info("Escaped content: \n" + unescapedContent);
try (InputStream is = new ByteArrayInputStream(unescapedContent.getBytes("UTF-8"))) {
- doc = db.parse(is);
+ Document doc = DOMUtil.loadDocument(is);
+ if (doc != null) {
+ return doc.getDocumentElement();
+ }
+ return null;
} catch (Exception ex) {
throw new JSONStructureException("Unable to parse Content '" + xmlContent + "'");
}
-
- Node node = doc.getDocumentElement();
-
- return node;
}
/**
diff --git a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONResponse.java b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONResponse.java
index 1ecf1ab..c2da383 100644
--- a/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONResponse.java
+++ b/openaz-xacml/src/main/java/org/apache/openaz/xacml/std/json/JSONResponse.java
@@ -55,9 +55,6 @@
import javax.security.auth.x500.X500Principal;
import javax.xml.XMLConstants;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -98,6 +95,7 @@
import org.apache.openaz.xacml.std.datatypes.ExtendedNamespaceContext;
import org.apache.openaz.xacml.std.datatypes.StringNamespaceContext;
import org.apache.openaz.xacml.std.datatypes.XPathExpressionWrapper;
+import org.apache.openaz.xacml.std.dom.DOMUtil;
import org.apache.openaz.xacml.util.FactoryException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -753,18 +751,6 @@
// representation.
// TODO Unfortunately the JSON spec does not say how the XML is formatted
// (with/without whitespace, etc).
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db;
- try {
- db = dbf.newDocumentBuilder();
- } catch (ParserConfigurationException e1) {
- throw new JSONStructureException("Content unable to setup Parser Configuration");
- }
- //
- // Parse the content
- //
- Document doc = null;
//
// First of all, the String is possible escaped.
@@ -778,8 +764,9 @@
unescapedContent = "<ROOT>" + unescapedContent + "</ROOT>";
// logger.info("Escaped content: \n" + unescapedContent);
+ Document doc = null;
try (InputStream bis = new ByteArrayInputStream(unescapedContent.getBytes("UTF-8"))) {
- doc = db.parse(bis);
+ doc = DOMUtil.loadDocument(bis);
} catch (Exception ex) {
throw new JSONStructureException("Unable to parse Content '"
+ detailObject.toString() + "'");