Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4964374

DocumentBuilder with Schema requires DOCTYPE after setValidating(true)

XMLWordPrintable

      Name: inR10064 Date: 12/05/2003



      The method
           javax.xml.parsers.DocumentBuilder.parse(InputSource)

      reports ErrorHandler error about not found DOCTYPE definition when Schema object
      is set for the DocumentBuilder and validating is set to true (see the sample code and
      log below). The description of the method

           DocumentBuilderFactory.setValidating(boolean validating)

      does not specify which grammar definition is used for the validation so the provided
      Schema object is awaiten to be used just like the schema provided thru the attribute

           "http://java.sun.com/xml/jaxp/properties/schemaSource"


      The bug affects new tests in JCK 1.5 (not yet integrated):
        api/javax_xml/parsers/DocumentBuilderFactory/index.html#SetSchema[SetSchema101]

      The bug found in the JDK 1.5.0-beta-b30.

      --------------------------------------------------------------------------

      package tests;

      import java.io.IOException;
      import java.io.PrintWriter;
      import java.io.StringReader;
      import javax.xml.XMLConstants;
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.ParserConfigurationException;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.Schema;
      import javax.xml.validation.SchemaFactory;
      import org.w3c.dom.Document;
      import org.xml.sax.ErrorHandler;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;
      import org.xml.sax.SAXParseException;

      public class SetSchema101 {

          String schemaSource =
                            "<?xml version='1.0'?>\n"
                            + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema&#39;>\n"
                            + " <xsd:element name='test101'>\n"
                            + " <xsd:complexType>\n"
                            + " <xsd:attribute name='attr'/>\n"
                            + " <xsd:attribute name='attr2' default='DEF'/>\n"
                            + " </xsd:complexType>\n"
                            + " </xsd:element>\n"
                            + "</xsd:schema>\n";

          InputSource createDocInputSource() {

              String source = "<?xml version='1.0'?>\n"
                          // + " <!DOCTYPE elem [\n"
                          // + " ELEMENT elem ANY ]>\n"
                          // + " <elem />\n"
                            + " <test101 attr='a' />\n"
      ;
      return new InputSource(new StringReader(source));
          }

          Schema createSchema() {
              SchemaFactory schFactory = SchemaFactory.newInstance(
      XMLConstants.W3C_XML_SCHEMA_NS_URI);
      try {
      Schema sch = schFactory.newSchema(
      new StreamSource(
      new StringReader(schemaSource)
      ));
      return sch;
      } catch (Exception se) {
      throw new IllegalStateException("No Schema : " + se);
      }
          }

          ErrorHandler errHdlr = new ErrorHandler() {
             public void warning(SAXParseException e) {
      System.out.println("ErrorHandler warning: "+ e);
      }
      public void error(SAXParseException e) {
      System.out.println("ErrorHandler error: "+ e);
      }
      public void fatalError(SAXParseException e) {
      System.out.println("ErrorHandler fatalError: "+ e);
      }
      };

          void parseValidating(boolean validating) {
              DocumentBuilderFactory docBFactory = DocumentBuilderFactory.newInstance();
              Schema sch = createSchema();
              docBFactory.setSchema(sch);

              DocumentBuilder docBuilder;

              System.out.println("---- docBFactory.setValidating("+ validating +") ----");
              docBFactory.setValidating(validating);

              try {
                  docBuilder = docBFactory.newDocumentBuilder();
              } catch (ParserConfigurationException pce) {
                  pce.printStackTrace();
                  return;
              }
              docBuilder.setErrorHandler(errHdlr);
      System.out.println("-- docBuilder.isValidating() = "+ docBuilder.isValidating());

              try {
                  System.out.println("-- parse() returns "+
      docBuilder.parse(createDocInputSource()));
              } catch (Exception e) {
                  System.out.println("** Exception: " + e);
              }
          }

          public static void main(String argv[]) {
      SetSchema101 test = new SetSchema101();
      test.parseValidating(true);
      test.parseValidating(false);
          }

      }
      --------------------------------------------------------------------------

      % java -showversion tests.SetSchema101
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b30)
      Java HotSpot(TM) Server VM (build 1.5.0-beta-b30, mixed mode)

      ---- docBFactory.setValidating(true) ----
      -- docBuilder.isValidating() = true
      ErrorHandler error: org.xml.sax.SAXParseException: Document root element "test101", must match
      DOCTYPE root "null".
      ErrorHandler error: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
      -- parse() returns [#document: null]
      ---- docBFactory.setValidating(false) ----
      -- docBuilder.isValidating() = false
      -- parse() returns [#document: null]

      --------------------------------------------------------------------------

      ======================================================================
      ###@###.### 2003-12-08

      Name: inR10064 Date: 12/11/2003


      Add info from mailing on this issue.
      --------
       Date: Tue, 9 Dec 2003 21:52:06 +0600 (NST)
       From: "Ilya V. Neverov" <###@###.###>
       Subject: 4964374: DocumentBuilder with Schema requires DOCTYPE after setValidating(true)
      .....

      Actually when a schema is provided with the attribute

            "http://java.sun.com/xml/jaxp/properties/schemaSource"

      the setValidating(true) forces validation against this schema and the DTD
      isn't required or is ignored (please see log below and the source attached).

      I believe this inconsistency in using schema provided thru the attribute or
      by Schema object will confuse users and so it needs to be resolved.
      .....
      ---------------------------------------------------------------------------------
      $ java -showversion tests.SchemaSource
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b30)
      Java HotSpot(TM) Server VM (build 1.5.0-beta-b30, mixed mode)

      -------- setValidating(false) setSchema==(true) putDTD==(true) ----
      -- parse() returns [#document: null]
      -------- setValidating(true) setSchema==(true) putDTD==(true) ----
      -- parse() returns [#document: null]
      -------- setValidating(true) setSchema==(false) putDTD==(true) ----
      ErrorHandler error: org.xml.sax.SAXParseException: Document root element "test101", must match
      DOCTYPE root "elem".
      ErrorHandler error: org.xml.sax.SAXParseException: Element type "test101" must be declared.
      -- parse() returns [#document: null]
      -------- setValidating(true) setSchema==(true) putDTD==(false) ----
      -- parse() returns [#document: null]
      -------- setValidating(true) setSchema==(false) putDTD==(false) ----
      ErrorHandler error: org.xml.sax.SAXParseException: Document root element "test101", must match
      DOCTYPE root "null".
      ErrorHandler error: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
      -- parse() returns [#document: null]
      ---------------------------------------------------------------------------------

       Date: Tue, 09 Dec 2003 09:08:47 -0800
       From: Kohsuke Kawaguchi <###@###.###>
       Subject: Re: 4964374: DocumentBuilder with Schema requires DOCTYPE after setValidating(true)
      .....
      I'm aware of that, but that behavior *was* inconsistent in the first
      place. That is, it mixes the validation as defined in the XML rec with
      other definitions of the term "validation", and thus making it
      impossible to control those two independently.

      Also, there are non-trivial difference in the way a non-validating
      parser and a validating parser works. For example, a non-validating
      parser is not required to parse external entities, it may not normalize
      some of the attribute values a validating processor would normalize, or
      it may not provide some of the attribute default values.

      Therefore, if we use the setValidating method to control both (1)
      whether a parser would behave as a non-validating one or a validating
      one, and (2) validation of the parsed infoset by the Schema object at
      the same time, then it makes impossible to configure the parsing in a
      certain way.


      In short, the way things are done today is very consistent and maps
      naturally to the various relevant specs. So one part of me thinks we
      should get things straight, even if it could be confusing for migrating
      users.


      > I believe this inconsistency in using schema provided thru the attribute or
      > by Schema object will confuse users and so it needs to be resolved.

      But then from the average user's point of view, I can see that this
      could be confusing (because the relevant XML specs are confusing!)

      Also from the use case point of view, entities and attribute defaults
      are used less and less these days, so it's not like we have tons of
      people who need to control two things separately.

      So the other part of me thinks it is worthwhile to hide the mess by
      defining the setValidating() method to be a magic method.
      .....


      ======================================================================

            kkawagucsunw Kohsuke Kawaguchi (Inactive)
            inevsunw Inev Inev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: