I've got a few changes for XSchemaValidator.java that add to its usability:

Add 2 methods (addInternalEntityDecl(), addExternalEntityDecl()) that do just
what they imply.  There's really no other way to have a parser extension declare
entities and still validate against a schema.  Both already exist in
DTDValidator so I wouldn't think there'd be an issue here.  This is a resolution
for an issue I raised a few weeks ago.

Change the prototype of loadSchema() to accept the ErrorHandler and
EntityResolver objects that were set for the instance parser rather than
creating new ones for the schema parser.    If you're not setting them yourself,
the defaults will apply and in any case the internal resolver is always called
first to resolve structures.dtd, datatypes.dtd, and versionInfo.ent.  These
changes allows more consistent handling of errors between the instance and
schema documents and allows you to use external entities in schemas that might
require custom resolution.  There's a 1 line change to XMLParser.java for this
as well.

Finally, I've removed the try/catch blocks from loadSchema() to allow it to pass
any exceptions up the call stack.  If the schema parse fails, it usually doesn't
make sense to try parsing the rest of the instance document but this allows
instance document parser to make that decision rather than forcing a stack trace
or just eating the exception.

george

*** xml-xerces/java/src/org/apache/xerces/framework/XMLParser.java      Fri Jan 
14
17:38:18 2000
--- src/org/apache/xerces/framework/XMLParser.java      Sat Jan 29 22:02:26 2000
***************
*** 1949,1955 ****
                                        if (is == null) {
                                is = new InputSource(fs);
                                                }
!                 fSchemaValidator.loadSchema(is);
              }
          }
          if (!fValidator.attributeSpecified(elementType, fAttrList, attrName,
fAttrNameLocator, attValue)) {
--- 1949,1955 ----
                                        if (is == null) {
                                is = new InputSource(fs);
                                                }
!                 fSchemaValidator.loadSchema(is, fErrorHandler, fResolver);
              }
          }
          if (!fValidator.attributeSpecified(elementType, fAttrList, attrName,
fAttrNameLocator, attValue)) {

***
xml-xerces/java/src/org\apache\xerces\validators\schema\XSchemaValidator.java
Mon Jan 31 18:22:50 2000
--- src/org\apache\xerces\validators\schema\XSchemaValidator.java       Tue Feb 
01
19:47:02 2000
***************
*** 472,477 ****
--- 472,483 ----
      public String getSystemIdOfParameterEntity(int peIndex) {
          throw new RuntimeException("cannot happen 30"); // not called
      }
+     public int addInternalEntityDecl(int name, int value, int location) throws
Exception {
+         return fEntityPool.addEntityDecl(name, value, location, -1, -1, -1,
!usingStandaloneReader());
+     }
+     public int addExternalEntityDecl(int name, int publicId, int systemId)
throws Exception {
+         return fEntityPool.addEntityDecl(name, -1, -1, publicId, systemId, -1,
!usingStandaloneReader());
+     }
      public void rootElementSpecified(int rootElementType) throws Exception {
          if (fValidating) {
              fRootElementType = rootElementType; // REVISIT - how does schema
do this?
***************
*** 1658,1664 ****

      private DOMParser fSchemaParser = null;

!     public void loadSchema(InputSource is) {

          // create parser for schema
          if (fSchemaParser == null) {
--- 1664,1672 ----

      private DOMParser fSchemaParser = null;

!     public void loadSchema(InputSource is,
!                org.xml.sax.ErrorHandler defaultErrorHandler,
!                EntityResolver defaultEntityResolver) throws Exception{

          // create parser for schema
          if (fSchemaParser == null) {
***************
*** 1666,1707 ****
                  public void ignorableWhitespace(char ch[], int start, int
length) {}
                  public void ignorableWhitespace(int dataIdx) {}
              };
!             fSchemaParser.setEntityResolver(new Resolver());
!             fSchemaParser.setErrorHandler(new ErrorHandler());
!         }
!
!         // parser schema file
!         try {
!
!             fSchemaParser.setFeature("http://xml.org/sax/features/validation";,
true);
!
fSchemaParser.setFeature("http://apache.org/xml/features/dom/defer-node-expansio
n", false);
!             fSchemaParser.parse(is);
!         }
!         catch (SAXException se) {
!             se.getException().printStackTrace();
!             System.err.println("error parsing schema file");
! //            System.exit(1);
!         }
!         catch (Exception e) {
!             e.printStackTrace();
!             System.err.println("error parsing schema file");
! //            System.exit(1);
!         }
          fSchemaDocument = fSchemaParser.getDocument();
          if (fSchemaDocument == null) {
!             System.err.println("error: couldn't load schema file!");
!             return;
          }

          // traverse schema
!         try {
!             Element root = fSchemaDocument.getDocumentElement();
!             traverseSchema(root);
!         }
!         catch (Exception e) {
!             e.printStackTrace(System.err);
! //            System.exit(1);
!         }
      }

      private void traverseSchema(Element root) throws Exception {
--- 1674,1695 ----
                  public void ignorableWhitespace(char ch[], int start, int
length) {}
                  public void ignorableWhitespace(int dataIdx) {}
              };
!             fSchemaParser.setEntityResolver(new
Resolver(defaultEntityResolver));
!             if (defaultErrorHandler != null)
fSchemaParser.setErrorHandler(defaultErrorHandler);
!             else fSchemaParser.setErrorHandler(new ErrorHandler());
!         }
!         fSchemaParser.setFeature("http://xml.org/sax/features/validation";,
true);
!
fSchemaParser.setFeature("http://apache.org/xml/features/dom/defer-node-expansio
n", false);
!
fSchemaParser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-n
odes", false);
!         fSchemaParser.parse(is);
          fSchemaDocument = fSchemaParser.getDocument();
          if (fSchemaDocument == null) {
!             throw new SAXException("Couldn't load schema file!");
          }

          // traverse schema
!         Element root = fSchemaDocument.getDocumentElement();
!         traverseSchema(root);
      }

      private void traverseSchema(Element root) throws Exception {
***************
*** 2938,2947 ****
              "datatypes.dtd",
              "versionInfo.ent",
              };

          public InputSource resolveEntity(String publicId, String systemId)
!             throws IOException {
!
              // looking for the schema DTDs?
              for (int i = 0; i < SYSTEM.length; i++) {
                  if (systemId.equals(SYSTEM[i])) {
--- 2926,2939 ----
              "datatypes.dtd",
              "versionInfo.ent",
              };
+         private EntityResolver defaultEntityResolver = null;
+         public Resolver(EntityResolver der)
+         {
+            defaultEntityResolver = der;
+         }

          public InputSource resolveEntity(String publicId, String systemId)
!             throws SAXException, IOException {
              // looking for the schema DTDs?
              for (int i = 0; i < SYSTEM.length; i++) {
                  if (systemId.equals(SYSTEM[i])) {
***************
*** 2953,2959 ****
              }

              // use default resolution
!             return null;

          } // resolveEntity(String,String):InputSource

--- 2945,2952 ----
              }

              // use default resolution
!             if(defaultEntityResolver != null) return
defaultEntityResolver.resolveEntity(publicId, systemId);
!             return(null);

          } // resolveEntity(String,String):InputSource

Reply via email to