aligned schema API with that of XSL transformation.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 3 Mar 2011 22:18:56 +0000 (22:18 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 3 Mar 2011 22:18:56 +0000 (22:18 +0000)
support/general/src/main/java/org/wamblee/xml/XMLDocument.java
support/general/src/main/java/org/wamblee/xml/XMLSchema.java
support/general/src/test/java/org/wamblee/xml/ClasspathUriResolverTest.java
support/general/src/test/java/org/wamblee/xml/XMLSchemaTest.java

index 2671d5baf165ff8211a095affcef79d45d7cc283..9ce35c0ea8826fe9112913e10ee689a0b6a711df 100644 (file)
@@ -132,9 +132,9 @@ public class XMLDocument {
         return process(new XMLSchema(aUri));
     }
 
-    public XMLDocument validate(String aSystemId, InputStream aIs,
+    public XMLDocument validate(String aSystemId,
         LSResourceResolver aResolver) throws XMLException {
-        return process(new XMLSchema(aSystemId, aIs, aResolver));
+        return process(new XMLSchema(aSystemId, aResolver));
     }
 
     /**
index 956de70116556a3559a6855e19e2011d122edfe9..4127a106eae6deb7744b7f5311b90150f74c9328 100644 (file)
@@ -29,11 +29,12 @@ import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.ls.LSInput;
 import org.w3c.dom.ls.LSResourceResolver;
 import org.xml.sax.SAXException;
 
 /**
- * Represents an XML Schema. 
+ * Represents an XML Schema.
  * 
  * @author Erik Brakkee
  */
@@ -52,12 +53,20 @@ public class XMLSchema implements XMLProcessor {
         }
     }
 
-    public XMLSchema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
-        initialize(aSystemId, aIs, aResolver);
+    public XMLSchema(String aSystemId, LSResourceResolver aResolver)
+        throws XMLException {
+        LSInput input = aResolver.resolveResource(null, null, null, aSystemId,
+            null);
+        if (input == null) {
+            throw new XMLException("Schema classpath resource '" + aSystemId +
+                "' not found");
+        }
+        InputStream is = input.getByteStream();
+        initialize(aSystemId, is, aResolver);
     }
 
-    private void initialize(String aSystemId, InputStream aIs, LSResourceResolver aResolver)
-        throws XMLException {
+    private void initialize(String aSystemId, InputStream aIs,
+        LSResourceResolver aResolver) throws XMLException {
         try {
             SchemaFactory factory = SchemaFactory
                 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
@@ -77,11 +86,12 @@ public class XMLSchema implements XMLProcessor {
         return new XMLSchema(aUri);
     }
 
-    public static XMLSchema schema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
-        return new XMLSchema(aSystemId, aIs, aResolver);
+    public static XMLSchema schema(String aSystemId,
+        LSResourceResolver aResolver) throws XMLException {
+        return new XMLSchema(aSystemId, aResolver);
     }
-    
-    private void validateImpl(DOMSource aDoc) throws XMLException { 
+
+    private void validateImpl(DOMSource aDoc) throws XMLException {
         try {
             validator.validate(aDoc);
         } catch (SAXException e) {
@@ -101,7 +111,7 @@ public class XMLSchema implements XMLProcessor {
 
     @Override
     public DOMSource process(DOMSource aDocument) throws XMLException {
-        validate(aDocument); 
+        validate(aDocument);
         return aDocument;
     }
 
index f406c69d5820bbc261e677160747194985c59c70..bc1c9cfc043a5500b8b8e64d4242629bc1223150 100644 (file)
@@ -18,6 +18,8 @@ package org.wamblee.xml;
 import static org.wamblee.xml.XMLDocument.*;
 import junit.framework.TestCase;
 
+import org.junit.Before;
+import org.junit.Test;
 import org.w3c.dom.ls.LSInput;
 import org.w3c.dom.ls.LSResourceResolver;
 import org.wamblee.io.ClassPathResource;
@@ -30,12 +32,14 @@ import javax.xml.transform.TransformerException;
 import javax.xml.transform.URIResolver;
 import javax.xml.transform.stream.StreamSource;
 
+import static junit.framework.TestCase.*;
+
 /**
  * Tests for {@link org.wamblee.xml.ClasspathUriResolver}.
  * 
  * @author Erik Brakkee
  */
-public class ClasspathUriResolverTest extends TestCase {
+public class ClasspathUriResolverTest {
     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;
@@ -47,8 +51,8 @@ public class ClasspathUriResolverTest extends TestCase {
      * 
      * @see junit.framework.TestCase#setUp()
      */
-    @Override
-    protected void setUp() throws Exception {
+    @Before
+    public void setUp() throws Exception {
         resolver = new ClasspathUriResolver();
     }
 
@@ -57,6 +61,7 @@ public class ClasspathUriResolverTest extends TestCase {
      * thrown.
      * 
      */
+    @Test
     public void testResolveNonExistingFileURIResolver() {
         try {
             resolver
@@ -74,6 +79,7 @@ public class ClasspathUriResolverTest extends TestCase {
      * @throws TransformerException
      * @throws IOException
      */
+    @Test
     public void testResolveExistingFileLSResourceResolver()
         throws TransformerException, IOException {
         LSInput source = resolver.resolveResource(null, null, "kees",
@@ -97,6 +103,7 @@ public class ClasspathUriResolverTest extends TestCase {
      * @throws TransformerException
      * @throws IOException
      */
+    @Test
     public void testResolveExistingFileLSResourceResolverUsingBasePath()
         throws TransformerException, IOException {
         
@@ -121,6 +128,7 @@ public class ClasspathUriResolverTest extends TestCase {
      * thrown.
      * 
      */
+    @Test
     public void testResolveNonExistingFileLSResourceResolver() {
         LSInput input = resolver.resolveResource(null, null, "kees",
             NON_EXISTING_RESOURCE, null);
index 4881824d3b1cf1008dd45bdfb44bb178fd37f670..ab4603c8ec9c74f71d24fbe2c0c727526d39cbd1 100644 (file)
@@ -34,14 +34,18 @@ import static org.wamblee.xml.XMLDocument.*;
 
 public class XMLSchemaTest {
     
+    private URI getResourceUri(String aResource) throws URISyntaxException { 
+        return getClass().getResource(aResource).toURI();
+    }
+    
     private InputStream getResource(String aResource) { 
         return getClass().getResourceAsStream(aResource);
     }
     
     @Test
-    public void testReadAndValidateOk() throws Exception { 
+    public void testReadAndValidateOkUseUri() throws Exception { 
         
-        XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate("test.xsd", getResource("test.xsd"), null);
+        XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate(getResourceUri("test.xsd"));
         Document doc = xmldoc.getDocument();
         Element element = doc.getDocumentElement();
         assertEquals("http://wamblee.org/test", element.getNamespaceURI());
@@ -52,8 +56,31 @@ public class XMLSchemaTest {
         XmlUtils.assertEquals("", doc, doc2);
     }
     
+    @Test
+    public void testReadAndValidateOkUseClasspath() throws Exception { 
+        
+        XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate(
+            "test.xsd", new ClasspathUriResolver("org/wamblee/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("input.xml", new ByteArrayInputStream(val.getBytes())).getDocument();
+        XmlUtils.assertEquals("", doc, doc2);
+    }
+    
+    @Test(expected = XMLException.class)
+    public void testSchemaNotFoundUseClasspath() throws Exception { 
+        
+        XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate(
+            "test.xsd.unknown", new ClasspathUriResolver("org/wamblee/xml"));
+    }
+    
+    
     @Test(expected = XMLException.class)
     public void testReadAndValidateNotOk() throws Exception {
-        XMLDocument xmldoc = xmldocument("testInvalid.xml", getResource("testInvalid.xml")).validate("test.xsd", getResource("test.xsd"), null);
+        XMLDocument xmldoc = xmldocument("testInvalid.xml", getResource("testInvalid.xml")).validate(getResourceUri("test.xsd"));
     }
 }