From: erik Date: Thu, 3 Mar 2011 22:18:56 +0000 (+0000) Subject: aligned schema API with that of XSL transformation. X-Git-Tag: BEFORE_SVN_TO_GIT~20 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=cb143ce2d352844758678484e60e17fb206c2d57;p=utils aligned schema API with that of XSL transformation. --- diff --git a/support/general/src/main/java/org/wamblee/xml/XMLDocument.java b/support/general/src/main/java/org/wamblee/xml/XMLDocument.java index 2671d5ba..9ce35c0e 100644 --- a/support/general/src/main/java/org/wamblee/xml/XMLDocument.java +++ b/support/general/src/main/java/org/wamblee/xml/XMLDocument.java @@ -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)); } /** diff --git a/support/general/src/main/java/org/wamblee/xml/XMLSchema.java b/support/general/src/main/java/org/wamblee/xml/XMLSchema.java index 956de701..4127a106 100644 --- a/support/general/src/main/java/org/wamblee/xml/XMLSchema.java +++ b/support/general/src/main/java/org/wamblee/xml/XMLSchema.java @@ -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; } diff --git a/support/general/src/test/java/org/wamblee/xml/ClasspathUriResolverTest.java b/support/general/src/test/java/org/wamblee/xml/ClasspathUriResolverTest.java index f406c69d..bc1c9cfc 100644 --- a/support/general/src/test/java/org/wamblee/xml/ClasspathUriResolverTest.java +++ b/support/general/src/test/java/org/wamblee/xml/ClasspathUriResolverTest.java @@ -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); diff --git a/support/general/src/test/java/org/wamblee/xml/XMLSchemaTest.java b/support/general/src/test/java/org/wamblee/xml/XMLSchemaTest.java index 4881824d..ab4603c8 100644 --- a/support/general/src/test/java/org/wamblee/xml/XMLSchemaTest.java +++ b/support/general/src/test/java/org/wamblee/xml/XMLSchemaTest.java @@ -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")); } }