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));
}
/**
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
*/
}
}
- 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);
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) {
@Override
public DOMSource process(DOMSource aDocument) throws XMLException {
- validate(aDocument);
+ validate(aDocument);
return aDocument;
}
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;
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;
*
* @see junit.framework.TestCase#setUp()
*/
- @Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
resolver = new ClasspathUriResolver();
}
* thrown.
*
*/
+ @Test
public void testResolveNonExistingFileURIResolver() {
try {
resolver
* @throws TransformerException
* @throws IOException
*/
+ @Test
public void testResolveExistingFileLSResourceResolver()
throws TransformerException, IOException {
LSInput source = resolver.resolveResource(null, null, "kees",
* @throws TransformerException
* @throws IOException
*/
+ @Test
public void testResolveExistingFileLSResourceResolverUsingBasePath()
throws TransformerException, IOException {
* thrown.
*
*/
+ @Test
public void testResolveNonExistingFileLSResourceResolver() {
LSInput input = resolver.resolveResource(null, null, "kees",
NON_EXISTING_RESOURCE, null);
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());
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"));
}
}