X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxml%2FDomUtils.java;h=44a9f8ee8268a4cb946d7bc6adb7478e21a84f34;hb=36320098ebdbe884869a9e04682a8a0291d25600;hp=3d1c2d8ffae0cc0b38031627db6b7131becf5d7b;hpb=6a50735b3dc6fa23f670828c1245d2607d7843ab;p=utils diff --git a/support/general/src/main/java/org/wamblee/xml/DomUtils.java b/support/general/src/main/java/org/wamblee/xml/DomUtils.java index 3d1c2d8f..44a9f8ee 100644 --- a/support/general/src/main/java/org/wamblee/xml/DomUtils.java +++ b/support/general/src/main/java/org/wamblee/xml/DomUtils.java @@ -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();