You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by "George T. Joseph" <gt...@peakin.com> on 2000/02/02 01:52:08 UTC

XSchemaValidator.java update

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


RE: XSchemaValidator.java update

Posted by "George T. Joseph" <gt...@peakin.com>.
I asked for alternatives for entities with schemas but I hever heard any...

What I'm asking for has nothing to do with the Schema WG.  They may have nixed
entities from the schema language but they did not nix the concept of including
data from one document into another.  The external entity code in Xerces would
be a good start for handling XLink or XInclude, no?  If so, you'll probably need
to add the 2 APIs in question anyway.

As for internal entities, I don't see anything in the Schema WD that says you
can't have internal entities.  It's Xerces that forbids a DOCTYPE and an
internal DTD subset when using a schema and therefore removes the mechanism for
declaring internal entities.  Give me an alternative mechanism for declaring
parameter constants and I'll be happy.

george

-----Original Message-----
From: twleung@sauria.com [mailto:twleung@sauria.com]
Sent: Thursday, February 03, 2000 3:20 AM
To: xerces-dev@xml.apache.org
Subject: Re: XSchemaValidator.java update


I'm philosophically opposed to adding in entities when the Schema WG
has worked so hard to take them out, especially before the final draft
of Schema comes out.   If we put this non-standard feature in, then it will
be very hard to take it out.  I've no problem with the error checking
improvements.  I'm kind of swamped at the moment with my new job so
it will probably be next week before I can sort through the patch.

Ted

----- Original Message -----
From: George T. Joseph <gt...@peakin.com>
To: xerces-dev <xe...@xml.apache.org>
Sent: Tuesday, February 01, 2000 4:52 PM
Subject: XSchemaValidator.java update


> 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
>
>



Re: XSchemaValidator.java update

Posted by tw...@sauria.com.
I'm philosophically opposed to adding in entities when the Schema WG
has worked so hard to take them out, especially before the final draft
of Schema comes out.   If we put this non-standard feature in, then it will
be very hard to take it out.  I've no problem with the error checking 
improvements.  I'm kind of swamped at the moment with my new job so
it will probably be next week before I can sort through the patch.

Ted

----- Original Message ----- 
From: George T. Joseph <gt...@peakin.com>
To: xerces-dev <xe...@xml.apache.org>
Sent: Tuesday, February 01, 2000 4:52 PM
Subject: XSchemaValidator.java update


> 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
> 
>