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;
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;
/**
}
/**
- * 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.