X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxml%2FDomUtils.java;h=f283ee07d9d51ff344d9f803fede157a70eba521;hb=c59a5c77753484df1a1aed3214f5f34d9692c42c;hp=3d1c2d8ffae0cc0b38031627db6b7131becf5d7b;hpb=f821a28ffddc9b91d23c8ef6993c19bee70e3b37;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..f283ee07 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; /** @@ -66,134 +72,32 @@ public final class DomUtils { } /** - * Parses an XML document from a string. - * - * @param aDocument - * document. - * - * @return - * - */ - public static Document read(String aDocument) throws XMLException { - ByteArrayInputStream is = new ByteArrayInputStream(aDocument.getBytes()); - - return read(is); - } - - /** - * Parses an XML document from a stream. - * - * @param aIs - * Input stream. - * - * @return - * - */ - public static Document read(InputStream aIs) throws XMLException { - try { - DocumentBuilder builder = DocumentBuilderFactory.newInstance() - .newDocumentBuilder(); - - return builder.parse(aIs); - } 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 { - aIs.close(); - } catch (Exception e) { - LOG.log(Level.WARNING, "Error closing XML file", e); - } - } - } - - /** - * Reads and validates a document against a schema. - * - * @param aIs - * Input stream. - * @param aSchema - * Schema. - * - * @return Parsed and validated document. - * + * Gets a dom level 3 implementation. + * @return Dom implementation. + * @throws ClassNotFoundException + * @throws InstantiationException + * @throws IllegalAccessException */ - public static Document readAndValidate(InputStream aIs, InputStream aSchema) - throws XMLException { + public static DOMImplementationLS getDomImplementationLS() { + final String message = "Could not get Dom level 3 implementation"; try { - final Schema schema = SchemaFactory.newInstance( - XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( - new StreamSource(aSchema)); - - final DocumentBuilderFactory factory = DocumentBuilderFactory + DOMImplementationRegistry registry = DOMImplementationRegistry .newInstance(); - factory.setValidating(true); - factory.setNamespaceAware(true); - factory.setSchema(schema); - - return factory.newDocumentBuilder().parse(aIs); - } 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(); - } catch (Exception e) { - LOG.log(Level.WARNING, "Error closing schema", e); - } - - try { - aIs.close(); - } catch (Exception e) { - LOG.log(Level.WARNING, "Error closing XML file", e); - } - } - } - /** - * Serializes an XML document to a stream. - * - * @param aDocument - * Document to serialize. - * @param aOs - * Output stream. - * - */ - public static void serialize(Document aDocument, OutputStream aOs) - throws IOException { - try { - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer identityTransform = factory.newTransformer(); - DOMSource source = new DOMSource(aDocument); - StreamResult result = new StreamResult(aOs); - identityTransform.transform(source, result); - } catch (TransformerException e) { - throw new IOException(e.getMessage(), e); + DOMImplementationLS impl = (DOMImplementationLS) registry + .getDOMImplementation("LS"); + return impl; + } catch (ClassCastException e) { + throw new RuntimeException(message, e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(message, e); + } catch (InstantiationException e) { + throw new RuntimeException(message, e); + } catch (IllegalAccessException e) { + throw new RuntimeException(message, e); } } - /** - * Serializes an XML document. - * - * @param aDocument - * Document to serialize. - * - * @return Serialized document. - * - */ - public static String serialize(Document aDocument) throws IOException { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - serialize(aDocument, os); - - return os.toString(); - } - /** * Removes duplicate attributes from a DOM tree.This is useful for * postprocessing the output of JTidy as a workaround for a bug in JTidy.