}
/**
- * 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
- *
+ * Gets a dom level 3 implementation.
+ * @return Dom implementation.
+ * @throws ClassNotFoundException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
*/
- public static Document read(InputStream aIs) throws XMLException {
+ public static DOMImplementationLS getDomImplementationLS() {
+ final String message = "Could not get Dom level 3 implementation";
try {
DOMImplementationRegistry registry = DOMImplementationRegistry
.newInstance();
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 (InstantiationException e) {
- throw new XMLException(e.getMessage(), e);
+ return impl;
+ } catch (ClassCastException e) {
+ throw new RuntimeException(message, e);
} catch (ClassNotFoundException e) {
- throw new XMLException(e.getMessage(), e);
- } catch (LSException 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.
- *
- */
- 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));
-
- return doc;
- } catch (SAXException e) {
- throw new XMLException(e.getMessage(), e);
- } catch (IOException 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);
+ 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.