added XMLDocument, XMLSchema, and XSLTransformation classes.
authorErik Brakkee <erik@brakkee.org>
Mon, 28 Feb 2011 22:23:41 +0000 (22:23 +0000)
committerErik Brakkee <erik@brakkee.org>
Mon, 28 Feb 2011 22:23:41 +0000 (22:23 +0000)
XSLTransformer and most of DomUtils have now been removed.

13 files changed:
support/general/src/main/java/org/wamblee/xml/ClasspathUriResolver.java
support/general/src/main/java/org/wamblee/xml/DomUtils.java
support/general/src/main/java/org/wamblee/xml/XMLDocument.java [new file with mode: 0644]
support/general/src/main/java/org/wamblee/xml/XMLProcessor.java [new file with mode: 0644]
support/general/src/main/java/org/wamblee/xml/XMLSchema.java [new file with mode: 0644]
support/general/src/main/java/org/wamblee/xml/XMLTextProcessor.java [new file with mode: 0644]
support/general/src/main/java/org/wamblee/xml/XSLTransformation.java [new file with mode: 0644]
support/general/src/main/java/org/wamblee/xml/XslTransformer.java [deleted file]
support/general/src/test/java/org/wamblee/xml/ClasspathUriResolverTest.java
support/general/src/test/java/org/wamblee/xml/XMLSchemaTest.java [moved from support/general/src/test/java/org/wamblee/xml/DomUtilsTest.java with 52% similarity]
support/general/src/test/java/org/wamblee/xml/XSLTransformationTest.java [new file with mode: 0644]
support/general/src/test/java/org/wamblee/xml/XmlDocumentTest.java [new file with mode: 0644]
support/general/src/test/java/org/wamblee/xml/XslTransformerTest.java [deleted file]

index 6ee7389e4014256d03c67acaa1a7d260e4971f7a..cad0cb2f388fb3ddab48b81545f6621b229eb8df 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2010 the original author or authors.
+ * Copyright 2005-2011 the original author or authors.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -33,12 +33,23 @@ import javax.xml.transform.stream.StreamSource;
  * URI resolver that resolves stylesheets through the classpath.
  */
 public class ClasspathUriResolver implements URIResolver, LSResourceResolver {
+    
+    private String base; 
+    
     /**
      * Constructs the resolver.
      * 
      */
     public ClasspathUriResolver() {
-        // Empty.
+        base = null; 
+    }
+    
+    /**
+     * Location in the classpath relative to which resolution takes place. 
+     * @param aBase Base. 
+     */
+    public ClasspathUriResolver(String aBase) { 
+        base = aBase;
     }
 
     /*
@@ -49,7 +60,8 @@ public class ClasspathUriResolver implements URIResolver, LSResourceResolver {
      */
     public Source resolve(String aHref, String aBase)
         throws TransformerException {
-        InputResource xslt = new ClassPathResource(aHref);
+        
+        InputResource xslt = resolveImpl(aHref);
 
         try {
             return new StreamSource(xslt.getInputStream());
@@ -60,12 +72,20 @@ public class ClasspathUriResolver implements URIResolver, LSResourceResolver {
         }
     }
 
+    private InputResource resolveImpl(String aHref) {
+        String systemId = aHref;
+        if ( base != null ) { 
+             systemId = base + "/" + aHref;
+        }
+        InputResource xslt = new ClassPathResource(systemId);
+        return xslt;
+    }
+
     @Override
     public LSInput resolveResource(String aType, String aNamespaceURI,
         String aPublicId, String aSystemId, String aBaseURI) {
         try {
-            InputStream xslt = new ClassPathResource(aSystemId)
-                .getInputStream();
+            InputStream xslt = resolveImpl(aSystemId).getInputStream();
             DOMImplementationLS impl = DomUtils.getDomImplementationLS();
             LSInput input = impl.createLSInput();
             input.setPublicId(aPublicId);
index a0a118c1062cd82e8fd0aac29bb84fb01a371fb3..f283ee07d9d51ff344d9f803fede157a70eba521 100644 (file)
@@ -71,50 +71,6 @@ public final class DomUtils {
         // Empty.
     }
 
-    /**
-     * 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 {
-            DOMImplementationLS impl = getDomImplementationLS();
-
-            LSParser builder = impl.createLSParser(
-                DOMImplementationLS.MODE_SYNCHRONOUS, null);
-            LSInput input = impl.createLSInput();
-            input.setByteStream(aIs);
-            return builder.parse(input);
-        } 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);
-            }
-        }
-    }
-
     /**
      * Gets a dom level 3 implementation. 
      * @return Dom implementation.
@@ -142,85 +98,6 @@ public final class DomUtils {
         }
     }
 
-    /**
-     * 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);
-        }
-    }
-
-    /**
-     * 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.
diff --git a/support/general/src/main/java/org/wamblee/xml/XMLDocument.java b/support/general/src/main/java/org/wamblee/xml/XMLDocument.java
new file mode 100644 (file)
index 0000000..2671d5b
--- /dev/null
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMSource;
+
+import org.w3c.dom.Document;
+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.w3c.dom.ls.LSResourceResolver;
+
+/**
+ * Convenience class for representing an XML document. Together with
+ * {@link XMLProcessor}, {@link XMLSchema}, {@link XMLTextProcessor}, and
+ * {@link XSLTransformation} it provides a simple API for XML parsing,
+ * validation, and transformation.
+ * 
+ * @author Erik Brakkee
+ */
+public class XMLDocument {
+
+    private static final Logger LOG = Logger.getLogger(XMLDocument.class
+        .getName());
+
+    private DOMSource doc;
+
+    public XMLDocument(DOMSource aDoc) {
+        doc = aDoc;
+    }
+
+    public XMLDocument(URI aUri) throws IOException, XMLException {
+        this(aUri.toString(), aUri.toURL().openStream());
+    }
+    
+    public XMLDocument(String aSystemId, String aContents) throws XMLException { 
+        this(aSystemId, new ByteArrayInputStream(aContents.getBytes()));
+    }
+
+    public XMLDocument(String aSystemId, InputStream aStream)
+        throws XMLException {
+        LSInput input = DomUtils.getDomImplementationLS().createLSInput();
+        input.setByteStream(aStream);
+        input.setSystemId(aSystemId);
+        doc = read(input, null);
+    }
+
+    public XMLDocument(String aSystemId, LSResourceResolver aResolver)
+        throws XMLException {
+        LSInput input = aResolver.resolveResource(null, null, null, aSystemId,
+            null);
+        doc = read(input, aResolver);
+    }
+
+    public static XMLDocument xmldocument(URI aUri) throws IOException,
+        XMLException {
+        return new XMLDocument(aUri);
+    }
+    
+    public static XMLDocument xmldocument(String aSystemId, String aContents) throws XMLException { 
+        return new XMLDocument(aSystemId, aContents);
+    }
+
+    public static XMLDocument xmldocument(String aSystemId, InputStream aStream)
+        throws XMLException {
+        return new XMLDocument(aSystemId, aStream);
+    }
+
+    public static XMLDocument xmldocument(String aSystemId,
+        LSResourceResolver aResolver) throws XMLException {
+        return new XMLDocument(aSystemId, aResolver);
+    }
+
+    public DOMSource getDOMSource() {
+        return doc;
+    }
+    
+    public Document getDocument() { 
+        return (Document)doc.getNode();
+    }
+
+    public XMLDocument process(XMLProcessor aProcessor) throws XMLException {
+        return new XMLDocument(aProcessor.process(doc));
+    }
+
+    public void write(XMLTextProcessor aProcessor, OutputStream aOs)
+        throws XMLException {
+        aProcessor.write(doc, aOs);
+    }
+    
+    public String write(XMLTextProcessor aProcessor) throws XMLException { 
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        write(aProcessor, bos); 
+        return bos.toString();
+    }
+
+    public XMLDocument transform(URI aUri) throws XMLException {
+        return process(new XSLTransformation(aUri));
+    }
+
+    public XMLDocument transform(String aSystemId, URIResolver aResolver)
+        throws XMLException {
+        return process(new XSLTransformation(aSystemId, aResolver));
+    }
+
+    public XMLDocument validate(URI aUri) throws XMLException {
+        return process(new XMLSchema(aUri));
+    }
+
+    public XMLDocument validate(String aSystemId, InputStream aIs,
+        LSResourceResolver aResolver) throws XMLException {
+        return process(new XMLSchema(aSystemId, aIs, aResolver));
+    }
+
+    /**
+     * Prints an XML document.
+     * 
+     * @param aOs
+     *            Output stream to print on.
+     * @param aPrettyPrint
+     *            Pretty print or not.
+     */
+    public void print(OutputStream aOs, boolean aPrettyPrint) {
+        try {
+            write(new XSLTransformation().setPrettyPrint(aPrettyPrint), aOs);
+        } catch (XMLException e) {
+            throw new RuntimeException(
+                "Programming error, cannot print a DOM tree?: " +
+                    doc.getSystemId(), e);
+        }
+    }
+
+    /**
+     * Prints an XML document.
+     * 
+     * @param aPrettyPrint
+     *            Pretty print or not.
+     * @return XML string.
+     */
+    public String print(boolean aPrettyPrint) {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        print(bos, aPrettyPrint);
+        return bos.toString();
+    }
+
+    private static DOMSource read(LSInput aInput, LSResourceResolver aResolver)
+        throws XMLException {
+        try {
+            DOMImplementationLS impl = DomUtils.getDomImplementationLS();
+
+            LSParser builder = impl.createLSParser(
+                DOMImplementationLS.MODE_SYNCHRONOUS, null);
+            if (aResolver != null) {
+                builder.getDomConfig().setParameter("resource-resolver",
+                    aResolver);
+            }
+            Document docraw = builder.parse(aInput);
+            DOMSource result = new DOMSource(docraw);
+            result.setSystemId(aInput.getSystemId());
+            return result;
+        } catch (LSException e) {
+            throw new XMLException(e.getMessage(), e);
+        }
+    }
+
+}
diff --git a/support/general/src/main/java/org/wamblee/xml/XMLProcessor.java b/support/general/src/main/java/org/wamblee/xml/XMLProcessor.java
new file mode 100644 (file)
index 0000000..1159f92
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import javax.xml.transform.dom.DOMSource;
+
+/**
+ * Generic XML processor. 
+ * The XML process may transform the dom tree or it can perform checks on it. 
+ * 
+ * @author Erik Brakkee
+ *
+ */
+public interface XMLProcessor {
+
+    /**
+     * Processes a document. 
+     * @param aDocument Document to process. 
+     * @return Processed document. 
+     * @throws XMLException In case of problems. 
+     */
+    DOMSource process(DOMSource aDocument) throws XMLException; 
+    
+}
diff --git a/support/general/src/main/java/org/wamblee/xml/XMLSchema.java b/support/general/src/main/java/org/wamblee/xml/XMLSchema.java
new file mode 100644 (file)
index 0000000..956de70
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+
+import javax.xml.XMLConstants;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+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.Document;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.xml.sax.SAXException;
+
+/**
+ * Represents an XML Schema. 
+ * 
+ * @author Erik Brakkee
+ */
+public class XMLSchema implements XMLProcessor {
+
+    private Schema schema;
+    private Validator validator;
+
+    public XMLSchema(URI aUri) throws XMLException {
+        try {
+            initialize(aUri.toString(), aUri.toURL().openStream(), null);
+        } catch (MalformedURLException e) {
+            throw new XMLException("Could not read '" + aUri + "'", e);
+        } catch (IOException e) {
+            throw new XMLException("Could not read '" + aUri + "'", e);
+        }
+    }
+
+    public XMLSchema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
+        initialize(aSystemId, aIs, aResolver);
+    }
+
+    private void initialize(String aSystemId, InputStream aIs, LSResourceResolver aResolver)
+        throws XMLException {
+        try {
+            SchemaFactory factory = SchemaFactory
+                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+            if (aResolver != null) {
+                factory.setResourceResolver(aResolver);
+            }
+            Source source = new StreamSource(aIs);
+            source.setSystemId(aSystemId);
+            schema = factory.newSchema(source);
+            validator = schema.newValidator();
+        } catch (SAXException e) {
+            throw new XMLException(e.getMessage(), e);
+        }
+    }
+
+    public static XMLSchema schema(URI aUri) throws XMLException {
+        return new XMLSchema(aUri);
+    }
+
+    public static XMLSchema schema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
+        return new XMLSchema(aSystemId, aIs, aResolver);
+    }
+    
+    private void validateImpl(DOMSource aDoc) throws XMLException { 
+        try {
+            validator.validate(aDoc);
+        } catch (SAXException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (IOException e) {
+            throw new XMLException(e.getMessage(), e);
+        }
+    }
+
+    public void validate(XMLDocument aDocument) throws XMLException {
+        validateImpl(aDocument.getDOMSource());
+    }
+
+    public void validate(DOMSource aDocument) throws XMLException {
+        validateImpl(aDocument);
+    }
+
+    @Override
+    public DOMSource process(DOMSource aDocument) throws XMLException {
+        validate(aDocument); 
+        return aDocument;
+    }
+
+}
diff --git a/support/general/src/main/java/org/wamblee/xml/XMLTextProcessor.java b/support/general/src/main/java/org/wamblee/xml/XMLTextProcessor.java
new file mode 100644 (file)
index 0000000..565c0fe
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import java.io.OutputStream;
+
+import javax.xml.transform.dom.DOMSource;
+
+import org.w3c.dom.Document;
+
+/**
+ * Represents an XML Processor that processes an XML file into text. 
+ * 
+ * @author Erik Brakkee
+ */
+public interface XMLTextProcessor {
+
+    /**
+     * Writes a document. 
+     * @param aDocument Document. 
+     * @param aStream Stream to write to. 
+     * @throws XMLException in case of problems. 
+     */
+    void write(DOMSource aDocument, OutputStream aStream) throws XMLException;
+}
diff --git a/support/general/src/main/java/org/wamblee/xml/XSLTransformation.java b/support/general/src/main/java/org/wamblee/xml/XSLTransformation.java
new file mode 100644 (file)
index 0000000..26a515e
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.w3c.dom.Document;
+
+/**
+ * Represents an XSL Transformation.
+ * @author Erik Brakkee
+ *
+ */
+public class XSLTransformation implements XMLProcessor, XMLTextProcessor {
+
+    private TransformerFactory factory;
+    private Transformer transformer;
+    private String systemId; 
+
+    /**
+     * Identity transform.
+     */
+    public XSLTransformation() throws XMLException {
+        factory = TransformerFactory.newInstance();
+        try {
+            transformer = factory.newTransformer();
+            systemId = "identityTransform";
+        } catch (TransformerConfigurationException e) {
+            throw new XMLException(e.getMessage(), e);
+        }
+    }
+
+    public XSLTransformation(URI aUri) throws XMLException {
+        try {
+            factory = TransformerFactory.newInstance();
+            Source source = new StreamSource(aUri.toURL().openStream());
+            source.setSystemId(aUri.toString());
+            systemId = aUri.toString();
+            transformer = factory.newTransformer(source);
+        } catch (MalformedURLException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (TransformerConfigurationException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (TransformerFactoryConfigurationError e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (IOException e) {
+            throw new XMLException(e.getMessage(), e);
+        }
+
+    }
+
+    public XSLTransformation(String aSystemId, URIResolver aResolver)
+        throws XMLException {
+        try {
+            factory = TransformerFactory.newInstance();
+            factory.setURIResolver(aResolver);
+            Source source = aResolver.resolve(aSystemId, null);
+            source.setSystemId(aSystemId);
+            systemId = aSystemId;
+            transformer = factory.newTransformer(source);
+        } catch (TransformerConfigurationException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (TransformerFactoryConfigurationError e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (TransformerException e) { 
+            throw new XMLException(e.getMessage(), e);
+        }
+    }
+    
+    /**
+     * Configure the transformation to do pretty-printing. 
+     */
+    public XSLTransformation setPrettyPrint(boolean aPrettyPrint) { 
+        transformer.setOutputProperty(OutputKeys.INDENT, aPrettyPrint ? "yes": "no");
+        return this; 
+    }
+    
+  
+    public XSLTransformation transformation(URI aUri) throws XMLException {
+        return new XSLTransformation(aUri);
+    }
+
+    public XSLTransformation transformation(String aSystemId, URIResolver aResolver)
+        throws XMLException {
+        return new XSLTransformation(aSystemId, aResolver);
+    }
+
+    @Override
+    public DOMSource process(DOMSource aDocument) throws XMLException {
+        DOMResult result = new DOMResult();
+        try {
+            transform(aDocument, result);
+        } catch (IOException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (TransformerException e) {
+            throw new XMLException(e.getMessage(), e);
+        }
+        Document docraw = (Document)result.getNode();
+        DOMSource resultSource = new DOMSource(docraw); 
+        resultSource.setSystemId(aDocument.getSystemId() + "/transformed(" + systemId + ")");
+        return resultSource;
+    }
+
+    @Override
+    public void write(DOMSource aDocument, OutputStream aStream) throws XMLException {
+        StreamResult result = new StreamResult(aStream);
+        try {
+            transform(aDocument, result);
+        } catch (IOException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (TransformerException e) {
+            throw new XMLException(e.getMessage(), e);
+        }
+    }
+
+    /**
+     * Transforms a document using XSLT.
+     * 
+     * @param aSource
+     *            Document to transform.
+     * @param aResult
+     *            Result of the transformation.
+     * 
+     * @throws IOException
+     *             In case of problems reading resources.
+     * @throws TransformerException
+     *             In case transformation fails.
+     */
+    private void transform(Source aSource, Result aResult) throws IOException,
+        TransformerException {
+        try {
+            transformer.transform(aSource, aResult);
+        } catch (TransformerConfigurationException e) {
+            throw new TransformerException(
+                "Configuration problem of XSLT transformation", e);
+        }
+    }
+}
diff --git a/support/general/src/main/java/org/wamblee/xml/XslTransformer.java b/support/general/src/main/java/org/wamblee/xml/XslTransformer.java
deleted file mode 100644 (file)
index b3ab9ed..0000000
+++ /dev/null
@@ -1,191 +0,0 @@
-/*
- * Copyright 2005-2010 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.xml;
-
-import org.w3c.dom.Document;
-
-import org.wamblee.io.FileResource;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.IOException;
-
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.URIResolver;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-
-/**
- * XSL transformer for simplified usage of XSL transformations.
- * 
- * @author Erik Brakkee
- */
-public class XslTransformer {
-    private TransformerFactory factory;
-
-    /**
-     * Constructs the URL resolver.
-     * 
-     * @param aResolver
-     *            URI resolver to use.
-     */
-    public XslTransformer(URIResolver aResolver) {
-        factory = TransformerFactory.newInstance();
-        factory.setURIResolver(aResolver);
-    }
-
-    /**
-     * Constructs the XSLT processor.
-     * 
-     */
-    public XslTransformer() {
-        factory = TransformerFactory.newInstance();
-    }
-
-    /**
-     * Resolves an XSLT based on URI.
-     * 
-     * @param aXslt
-     *            XSLT to resolve,
-     * 
-     * @return Source for the XSLT
-     * 
-     * @throws TransformerException
-     *             In case the XSLT cannot be found.
-     */
-    public Source resolve(String aXslt) throws TransformerException {
-        URIResolver resolver = factory.getURIResolver();
-
-        if (resolver == null) {
-            if (new File(aXslt).canRead()) {
-                try {
-                    return new StreamSource(new FileResource(new File(aXslt))
-                        .getInputStream());
-                } catch (IOException e) {
-                    throw new TransformerException(e.getMessage(), e);
-                }
-            }
-            throw new TransformerException("Cannot read '" + aXslt + "'");
-        }
-
-        return resolver.resolve(aXslt, "");
-    }
-
-    /**
-     * Transforms a DOM document into another DOM document using a given XSLT
-     * transformation.
-     * 
-     * @param aDocument
-     *            Document to transform.
-     * @param aXslt
-     *            XSLT to use.
-     * 
-     * @return Transformed document.
-     * 
-     * @throws IOException
-     *             In case of problems reading resources.
-     * @throws TransformerException
-     *             In case transformation fails.
-     */
-    public Document transform(Document aDocument, Source aXslt)
-        throws IOException, TransformerException {
-        Source source = new DOMSource(aDocument);
-        DOMResult result = new DOMResult();
-        transform(source, result, aXslt);
-
-        return (Document) result.getNode();
-    }
-
-    /**
-     * Transforms a document using XSLT.
-     * 
-     * @param aDocument
-     *            Document to transform.
-     * @param aXslt
-     *            XSLT to use.
-     * 
-     * @return Transformed document.
-     * 
-     * @throws IOException
-     *             In case of problems reading resources.
-     * @throws TransformerException
-     *             In case transformation fails.
-     */
-    public Document transform(byte[] aDocument, Source aXslt)
-        throws IOException, TransformerException {
-        Source source = new StreamSource(new ByteArrayInputStream(aDocument));
-        DOMResult result = new DOMResult();
-        transform(source, result, aXslt);
-
-        return (Document) result.getNode();
-    }
-
-    /**
-     * Transforms a document to a text output. This supports XSLT
-     * transformations that result in text documents.
-     * 
-     * @param aDocument
-     *            Document to transform.
-     * @param aXslt
-     *            XSL transformation.
-     * 
-     * @return Transformed document.
-     * 
-     */
-    public String textTransform(byte[] aDocument, Source aXslt)
-        throws IOException, TransformerException {
-        Source source = new StreamSource(new ByteArrayInputStream(aDocument));
-        ByteArrayOutputStream os = new ByteArrayOutputStream();
-        StreamResult result = new StreamResult(os);
-        transform(source, result, aXslt);
-
-        return new String(os.toByteArray());
-    }
-
-    /**
-     * Transforms a document using XSLT.
-     * 
-     * @param aSource
-     *            Document to transform.
-     * @param aResult
-     *            Result of the transformation.
-     * @param aXslt
-     *            XSLT to use.
-     * 
-     * @throws IOException
-     *             In case of problems reading resources.
-     * @throws TransformerException
-     *             In case transformation fails.
-     */
-    public void transform(Source aSource, Result aResult, Source aXslt)
-        throws IOException, TransformerException {
-        try {
-            Transformer transformer = factory.newTransformer(aXslt);
-            transformer.transform(aSource, aResult);
-        } catch (TransformerConfigurationException e) {
-            throw new TransformerException(
-                "Configuration problem of XSLT transformation", e);
-        }
-    }
-}
index 7abe5d2ad8951b86bd092278fce95caed68434c8..f406c69d5820bbc261e677160747194985c59c70 100644 (file)
@@ -15,6 +15,7 @@
  */
 package org.wamblee.xml;
 
+import static org.wamblee.xml.XMLDocument.*;
 import junit.framework.TestCase;
 
 import org.w3c.dom.ls.LSInput;
@@ -35,7 +36,9 @@ import javax.xml.transform.stream.StreamSource;
  * @author Erik Brakkee
  */
 public class ClasspathUriResolverTest extends TestCase {
-    private static final String EXISTING_RESOURCE = "org/wamblee/xml/reportToHtml.xsl";
+    private static final String REPORT_TO_HTML_XSL = "reportToHtml.xsl";
+    private static final String PREFIX = "org/wamblee/xml";
+    private static final String EXISTING_RESOURCE = PREFIX + "/" + REPORT_TO_HTML_XSL;
     private static final String NON_EXISTING_RESOURCE = "org/wamblee/xml/reportToHtml-nonexisting.xsl";
     private ClasspathUriResolver resolver;
 
@@ -49,27 +52,6 @@ public class ClasspathUriResolverTest extends TestCase {
         resolver = new ClasspathUriResolver();
     }
 
-    /**
-     * Resolves an existing file. Verifies the file is resolved correctly.
-     * 
-     * @throws TransformerException
-     * @throws IOException
-     */
-    public void testResolveExistingFileURIResolver()
-        throws TransformerException, IOException {
-        Source source = resolver
-            .resolve(EXISTING_RESOURCE, "");
-        assertTrue(source instanceof StreamSource);
-
-        String resolved = FileSystemUtils.read(((StreamSource) source)
-            .getInputStream());
-
-        ClassPathResource resource = new ClassPathResource(
-            EXISTING_RESOURCE);
-        String expected = FileSystemUtils.read(resource.getInputStream());
-        assertEquals(expected, resolved);
-    }
-
     /**
      * Resolves a non-existing file. Verifies that a TransformerException is
      * thrown.
@@ -108,6 +90,31 @@ public class ClasspathUriResolverTest extends TestCase {
         String expected = FileSystemUtils.read(resource.getInputStream());
         assertEquals(expected, resolved);
     }
+    
+    /**
+     * Resolves an existing file using a base path. Verifies the file is resolved correctly.
+     * 
+     * @throws TransformerException
+     * @throws IOException
+     */
+    public void testResolveExistingFileLSResourceResolverUsingBasePath()
+        throws TransformerException, IOException {
+        
+        resolver = new ClasspathUriResolver(PREFIX);
+        LSInput source = resolver.resolveResource(null, null, "kees",
+            REPORT_TO_HTML_XSL, null);
+        assertNotNull(source);
+        
+        assertEquals("kees", source.getPublicId());
+        assertEquals(REPORT_TO_HTML_XSL, source.getSystemId());
+
+        String resolved = FileSystemUtils.read(source.getByteStream());
+
+        ClassPathResource resource = new ClassPathResource(
+            EXISTING_RESOURCE);
+        String expected = FileSystemUtils.read(resource.getInputStream());
+        assertEquals(expected, resolved);
+    }
 
     /**
      * Resolves a non-existing file. Verifies that a TransformerException is
similarity index 52%
rename from support/general/src/test/java/org/wamblee/xml/DomUtilsTest.java
rename to support/general/src/test/java/org/wamblee/xml/XMLSchemaTest.java
index d85f44646b0ed5751e6ad1c3f51a278117695aa8..4881824d3b1cf1008dd45bdfb44bb178fd37f670 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2010 the original author or authors.
+ * Copyright 2005-2011 the original author or authors.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.wamblee.xml;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
 
 import org.junit.Test;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
 
+import static junit.framework.Assert.*;
 import static junit.framework.TestCase.*;
 
-public class DomUtilsTest {
+import static org.wamblee.xml.XMLDocument.*;
+
+public class XMLSchemaTest {
     
     private InputStream getResource(String aResource) { 
         return getClass().getResourceAsStream(aResource);
     }
-
-    @Test
-    public void testReadWrite() throws Exception { 
-        Document doc = DomUtils.read(getResource("test.xml"));
-        Element element = doc.getDocumentElement();
-        assertEquals("http://wamblee.org/test", element.getNamespaceURI());
-        String val = DomUtils.serialize(doc);
-        // parse the written document
-        Document doc2 = DomUtils.read(new ByteArrayInputStream(val.getBytes()));
-        XmlUtils.assertEquals("", doc, doc2);
-    }
-    
-    @Test(expected = XMLException.class)
-    public void testReadNotWellFormed() throws Exception { 
-        Document doc = DomUtils.read(getResource("testNotWellFormed.xml"));
-    }
     
     @Test
     public void testReadAndValidateOk() throws Exception { 
-        Document doc = DomUtils.readAndValidate(getResource("test.xml"), getResource("test.xsd"));
+        
+        XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate("test.xsd", getResource("test.xsd"), null);
+        Document doc = xmldoc.getDocument();
         Element element = doc.getDocumentElement();
         assertEquals("http://wamblee.org/test", element.getNamespaceURI());
-        String val = DomUtils.serialize(doc);
+        
+        String val = xmldoc.print(false);
         // parse the written document
-        Document doc2 = DomUtils.read(new ByteArrayInputStream(val.getBytes()));
+        Document doc2 = xmldocument("input.xml", new ByteArrayInputStream(val.getBytes())).getDocument();
         XmlUtils.assertEquals("", doc, doc2);
     }
     
     @Test(expected = XMLException.class)
-    public void testReadAndValidateNotOk() throws Exception { 
-        Document doc = DomUtils.readAndValidate(getResource("testInvalid.xml"), getResource("test.xsd"));
+    public void testReadAndValidateNotOk() throws Exception {
+        XMLDocument xmldoc = xmldocument("testInvalid.xml", getResource("testInvalid.xml")).validate("test.xsd", getResource("test.xsd"), null);
     }
 }
diff --git a/support/general/src/test/java/org/wamblee/xml/XSLTransformationTest.java b/support/general/src/test/java/org/wamblee/xml/XSLTransformationTest.java
new file mode 100644 (file)
index 0000000..3cc494c
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.wamblee.io.ClassPathResource;
+import org.wamblee.io.FileSystemUtils;
+import org.wamblee.io.InputResource;
+
+import static org.wamblee.xml.XMLDocument.*;
+
+import static junit.framework.TestCase.*;
+
+/**
+ * Tests the XSL transformation
+ * 
+ * @author Erik Brakkee
+ */
+public class XSLTransformationTest  {
+    private static final String PREFIX = "org/wamblee/xml/";
+    private static final String INCLUDED_XSL_FILE = "utilities.xsl";
+    private static final String REPORT_XML = "report.xml";
+    private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
+    private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
+    private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
+    private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
+    private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
+
+    private String getResourcePath(String aResource) {
+        return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
+            aResource;
+    }
+
+    /**
+     * Transforms a file while using the default resolver, where the included
+     * file can be found. Verifies the transformation is done correctly.
+     * 
+     */
+    @Test
+    public void testTransformUsingDefaultResolver() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_XSLT).toURI());
+       
+        Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument();
+
+        Document output = transformed.getDocument();
+        XmlUtils.assertEquals("byte[] transform", expected, output);
+    }
+    
+    @Test
+    public void testTransformUsingDefaultResolverTextTransform() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        String transformed = xmldoc.write(new XSLTransformation(getClass().getResource(REPORT_TO_HTML_XSLT).toURI()));
+       
+        XMLDocument xmldocTransformed = xmldocument("read-transformed-text", new ByteArrayInputStream(transformed.getBytes()));
+        Document expected = xmldocument("output-reportToHtml-report.xml", 
+            new ClassPathResource(
+                getResourcePath("output-reportToHtml-report.xml"))
+                .getInputStream()).getDocument();
+        XmlUtils
+            .assertEquals("text transform", expected, xmldocTransformed.getDocument());
+    }
+   
+    /**
+     * Transforms a file using the default resolver where the included file
+     * cannot be found. Verifies that a TransformerException is thrown.
+     * 
+     */
+    @Test(expected = XMLException.class)
+    public void testTransformUsingDefaultResolverFails() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML2_XSLT).toURI());
+    }
+
+    /**
+     * Transforms a file using an invalid Xslt. Verifies that a
+     * TransformerException is thrown.
+     * 
+     */
+    @Test(expected = XMLException.class)
+    public void testTransformInvalidXslt() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_INVALID_XSLT).toURI());
+    }
+
+    /**
+     * Transforms a file using a non-well formed xslt. Verifies that a
+     * TransformerException is thrown.
+     * 
+     */
+    @Test(expected=XMLException.class)
+    public void testTransformNonWellformedXslt() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_NONWELLFORMED_XSLT).toURI());
+    }
+
+    /**
+     * Transforms a file using a class path resolver.
+     * 
+     */
+    @Test
+    public void testTransformUsingClassPathResolver() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        XMLDocument transformed = xmldoc.transform(PREFIX + REPORT_TO_HTML2_XSLT, new ClasspathUriResolver());
+        Document output1 = transformed.getDocument();
+        
+        Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument();
+        XmlUtils.assertEquals("doc", expected, output1);
+    }
+
+    /**
+     * Transforms a file to text output. Verifies the file is transformed
+     * correctly.
+     * 
+     */
+    @Test
+    public void testTransformToTextOutput() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        String transformed = xmldoc.write(new XSLTransformation(PREFIX + REPORT_TO_TEXT_XSLT, new ClasspathUriResolver()));
+     
+        String expected = "Hello world!";
+        assertEquals("text transform", expected, transformed);
+    }
+
+    /**
+     * Tests resolving a file using {@link XslTransformer#resolve(String)} with
+     * the default resolver where the file does not exist.
+     * 
+     */
+    @Test(expected = XMLException.class)
+    public void testResolveWithDefaultResolverFileNotFound() throws Exception {
+        XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
+        XMLDocument transformed = xmldoc.transform(PREFIX + "utilities-nonexistent.xsl", new ClasspathUriResolver());
+    }
+}
diff --git a/support/general/src/test/java/org/wamblee/xml/XmlDocumentTest.java b/support/general/src/test/java/org/wamblee/xml/XmlDocumentTest.java
new file mode 100644 (file)
index 0000000..6e30e5f
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import static junit.framework.TestCase.*;
+
+import static org.wamblee.xml.XMLDocument.*;
+
+public class XmlDocumentTest {
+    
+    private InputStream getResource(String aResource) { 
+        return getClass().getResourceAsStream(aResource);
+    }
+
+    @Test
+    public void testReadWriteFromInputStream() throws Exception { 
+        XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml"));
+        
+        Document doc = xmldoc.getDocument();
+        Element element = doc.getDocumentElement();
+        assertEquals("http://wamblee.org/test", element.getNamespaceURI());
+        
+        String val = xmldoc.print(false);
+        // parse the written document
+        Document doc2 = xmldocument("val.xml", val).getDocument();
+        XmlUtils.assertEquals("", doc, doc2);
+    }
+    
+    @Test
+    public void testReadWriteFromUri() throws XMLException, URISyntaxException, IOException { 
+        XMLDocument xmldoc = xmldocument( getClass().getResource("test.xml").toURI());
+        Node node = xmldoc.getDocument().getFirstChild();
+        assertEquals("root", node.getLocalName());
+    }
+    
+    @Test
+    public void testReadWriteFromClassPath() throws XMLException { 
+        XMLDocument xmldoc = xmldocument("org/wamblee/xml/test.xml", new ClasspathUriResolver());
+        Node node = xmldoc.getDocument().getFirstChild();
+        assertEquals("root", node.getLocalName());
+    }
+    
+    @Test(expected = XMLException.class)
+    public void testReadNotWellFormed() throws Exception { 
+        xmldocument("testNotWellFormed.xml", getResource("testNotWellFormed.xml"));
+    }
+}
diff --git a/support/general/src/test/java/org/wamblee/xml/XslTransformerTest.java b/support/general/src/test/java/org/wamblee/xml/XslTransformerTest.java
deleted file mode 100644 (file)
index c3cf78b..0000000
+++ /dev/null
@@ -1,263 +0,0 @@
-/*
- * Copyright 2005-2010 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.xml;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-
-import junit.framework.TestCase;
-
-import org.w3c.dom.Document;
-import org.wamblee.io.ClassPathResource;
-import org.wamblee.io.FileSystemUtils;
-import org.wamblee.io.InputResource;
-
-/**
- * Tests the XSL transformer.
- * 
- * @author Erik Brakkee
- */
-public class XslTransformerTest extends TestCase {
-    private static final String INCLUDED_XSL_FILE = "utilities.xsl";
-    private static final String REPORT_XML = "report.xml";
-    private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
-    private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
-    private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
-    private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
-    private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
-
-    private String getResourcePath(String aResource) {
-        return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
-            aResource;
-    }
-
-    /**
-     * Transforms a file while using the default resolver, where the included
-     * file can be found. Verifies the transformation is done correctly.
-     * 
-     */
-    public void testTransformUsingDefaultResolver() throws Exception {
-        XslTransformer transformer = new XslTransformer();
-
-        InputResource xmlResource = new ClassPathResource(
-            getResourcePath(REPORT_XML));
-
-        Source xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
-
-        byte[] documentData = FileSystemUtils
-            .read(xmlResource.getInputStream()).getBytes();
-        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
-            .newDocumentBuilder();
-        Document document = builder.parse(xmlResource.getInputStream());
-        Source documentSource = new StreamSource(xmlResource.getInputStream());
-
-        Document expected = DomUtils
-            .read(new ClassPathResource(
-                getResourcePath("output-reportToHtml-report.xml"))
-                .getInputStream());
-
-        Document output1 = transformer.transform(documentData, xslt);
-        XmlUtils.assertEquals("byte[] transform", expected, output1);
-
-        xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
-
-        Document output2 = transformer.transform(document, xslt);
-        XmlUtils.assertEquals("document transform", expected, output2);
-
-        ByteArrayOutputStream os = new ByteArrayOutputStream();
-        Result output = new StreamResult(os);
-
-        xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
-        transformer.transform(documentSource, output, xslt);
-        XmlUtils.assertEquals("document source transform", expected, DomUtils
-            .read(os.toString()));
-
-        xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
-
-        String result = transformer.textTransform(documentData, xslt);
-        XmlUtils
-            .assertEquals("text transform", expected, DomUtils.read(result));
-    }
-
-    /**
-     * Transforms a file using the default resolver where the included file
-     * cannot be found. Verifies that a TransformerException is thrown.
-     * 
-     */
-    public void testTransformUsingDefaultResolverFails() throws IOException {
-        XslTransformer transformer = new XslTransformer();
-
-        InputResource xmlResource = new ClassPathResource(
-            getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
-
-        byte[] documentData = FileSystemUtils
-            .read(xmlResource.getInputStream()).getBytes();
-
-        try {
-            transformer.transform(documentData, xslt);
-        } catch (TransformerException e) {
-            return; // ok
-        }
-
-        fail();
-    }
-
-    /**
-     * Transforms a file using an invalid Xslt. Verifies that a
-     * TransformerException is thrown.
-     * 
-     */
-    public void testTransformInvalidXslt() throws IOException {
-        XslTransformer transformer = new XslTransformer();
-
-        InputResource xmlResource = new ClassPathResource(
-            getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
-
-        byte[] documentData = FileSystemUtils
-            .read(xmlResource.getInputStream()).getBytes();
-
-        try {
-            transformer.transform(documentData, xslt);
-        } catch (TransformerException e) {
-            return; // ok
-        }
-
-        fail();
-    }
-
-    /**
-     * Transforms a file using a non-well formed xslt. Verifies that a
-     * TransformerException is thrown.
-     * 
-     */
-    public void testTransformNonWellformedXslt() throws IOException {
-        XslTransformer transformer = new XslTransformer();
-
-        InputResource xmlResource = new ClassPathResource(
-            getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML_NONWELLFORMED_XSLT))
-            .getInputStream());
-
-        byte[] documentData = FileSystemUtils
-            .read(xmlResource.getInputStream()).getBytes();
-
-        try {
-            transformer.transform(documentData, xslt);
-        } catch (TransformerException e) {
-            return; // ok
-        }
-
-        fail();
-    }
-
-    /**
-     * Transforms a file using a class path resolver.
-     * 
-     */
-    public void testTransformUsingClassPathResolver() throws Exception {
-        XslTransformer transformer = new XslTransformer(
-            new ClasspathUriResolver());
-
-        InputResource xmlResource = new ClassPathResource(
-            getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
-
-        byte[] documentData = FileSystemUtils
-            .read(xmlResource.getInputStream()).getBytes();
-
-        Document output1 = transformer.transform(documentData, xslt);
-        Document expected = DomUtils
-            .read(new ClassPathResource(
-                getResourcePath("output-reportToHtml-report.xml"))
-                .getInputStream());
-        XmlUtils.assertEquals("doc", expected, output1);
-    }
-
-    /**
-     * Transforms a file to text output. Verifies the file is transformed
-     * correctly.
-     * 
-     */
-    public void testTransformToTextOutput() throws Exception {
-        XslTransformer transformer = new XslTransformer(
-            new ClasspathUriResolver());
-
-        InputResource xmlResource = new ClassPathResource(
-            getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(new ClassPathResource(
-            getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
-
-        byte[] documentData = FileSystemUtils
-            .read(xmlResource.getInputStream()).getBytes();
-
-        String result = transformer.textTransform(documentData, xslt);
-        String expected = "Hello world!";
-        assertEquals("text transform", expected, result);
-    }
-
-    /**
-     * Tests resolving a file using {@link XslTransformer#resolve(String)} with
-     * the default resolver where the file does not exist.
-     * 
-     */
-    public void testResolveWithDefaultResolverFileNotFound() {
-        XslTransformer transformer = new XslTransformer();
-
-        try {
-            transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
-        } catch (TransformerException e) {
-            return; // ok
-        }
-
-        fail();
-    }
-
-    /**
-     * Tests resolving a file using {@link XslTransformer#resolve(String)} with
-     * the default resolver.
-     * 
-     */
-    public void testResolveWithClasspathResolver() throws Exception {
-        XslTransformer transformer = new XslTransformer(
-            new ClasspathUriResolver());
-        Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
-        assert (source instanceof StreamSource);
-
-        StreamSource ssource = (StreamSource) source;
-        String data = FileSystemUtils.read(ssource.getInputStream());
-        String expected = FileSystemUtils.read(new ClassPathResource(
-            getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
-        assertEquals(expected, data);
-    }
-}