Now using the dom level 3 API for parsing. Also extended the test case
[utils] / support / general / src / main / java / org / wamblee / xml / DomUtils.java
index 3d1c2d8ffae0cc0b38031627db6b7131becf5d7b..44a9f8ee8268a4cb946d7bc6adb7478e21a84f34 100644 (file)
@@ -39,6 +39,7 @@ import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Validator;
 
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
@@ -46,6 +47,11 @@ import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.w3c.dom.bootstrap.DOMImplementationRegistry;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSParser;
 import org.xml.sax.SAXException;
 
 /**
@@ -91,15 +97,24 @@ public final class DomUtils {
      */
     public static Document read(InputStream aIs) throws XMLException {
         try {
-            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
-                .newDocumentBuilder();
+            DOMImplementationRegistry registry = DOMImplementationRegistry
+                .newInstance();
 
-            return builder.parse(aIs);
-        } catch (SAXException e) {
+            DOMImplementationLS impl = (DOMImplementationLS) registry
+                .getDOMImplementation("LS");
+
+            LSParser builder = impl.createLSParser(
+                DOMImplementationLS.MODE_SYNCHRONOUS, null);
+            LSInput input = impl.createLSInput();
+            input.setByteStream(aIs);
+            return builder.parse(input);
+        } catch (IllegalAccessException e) {
             throw new XMLException(e.getMessage(), e);
-        } catch (IOException e) {
+        } catch (InstantiationException e) {
             throw new XMLException(e.getMessage(), e);
-        } catch (ParserConfigurationException e) {
+        } catch (ClassNotFoundException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (LSException e) {
             throw new XMLException(e.getMessage(), e);
         } finally {
             try {
@@ -124,23 +139,18 @@ public final class DomUtils {
     public static Document readAndValidate(InputStream aIs, InputStream aSchema)
         throws XMLException {
         try {
+            Document doc = read(aIs);
             final Schema schema = SchemaFactory.newInstance(
                 XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
                 new StreamSource(aSchema));
+            Validator validator = schema.newValidator();
+            validator.validate(new DOMSource(doc));
 
-            final DocumentBuilderFactory factory = DocumentBuilderFactory
-                .newInstance();
-            factory.setValidating(true);
-            factory.setNamespaceAware(true);
-            factory.setSchema(schema);
-
-            return factory.newDocumentBuilder().parse(aIs);
+            return doc;
         } catch (SAXException e) {
             throw new XMLException(e.getMessage(), e);
         } catch (IOException e) {
             throw new XMLException(e.getMessage(), e);
-        } catch (ParserConfigurationException e) {
-            throw new XMLException(e.getMessage(), e);
         } finally {
             try {
                 aSchema.close();