aligned schema API with that of XSL transformation.
[utils] / support / general / src / main / java / org / wamblee / xml / XMLSchema.java
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;
     }