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

invalid particle restriction is not detected

XMLWordPrintable

    • b49
    • generic
    • generic

      Name: erR10175 Date: 12/13/2002



      The RI doesn't reject the schema (see test.xsd below), that violates the constraint on
      occurrence range of the particle derived from another one by restriction. Its maxOccurs
      is greater than that of the base particle.

      The XML Schema spec (XML Schema Part 1: Structures, Section 3.9.6 Constraints on Particle
      Schema Components) reads:
      "Schema Component Constraint: Occurrence Range OK
      For a particle's occurrence range to be a valid restriction of another's occurrence
      range all of the following must be true:
      1 Its {min occurs} is greater than or equal to the other's {min occurs}.
      2 one of the following must be true:
      2.1 The other's {max occurs} is unbounded.
      2.2 Both {max occurs} are numbers, and the particle's is less than or equal to the other's."

      So the following schema is invalid, but the RI successfully use it to validate the document
      test.xml:
      -------------------------- test.xsd
      <?xml version="1.0"?>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="test"
        targetNamespace="test">

           <xsd:complexType name="A">
                <xsd:sequence>
                     <xsd:element name="c" type="xsd:int" maxOccurs="1"/>
                </xsd:sequence>
           </xsd:complexType>

           <xsd:element name="E" type="A"/>

           <xsd:complexType name="C">
                <xsd:complexContent>
                     <xsd:restriction base="A">
                          <xsd:sequence>
                               <xsd:element name="c" type="xsd:int" maxOccurs="3"/>
                          </xsd:sequence>
                     </xsd:restriction>
                </xsd:complexContent>
           </xsd:complexType>

      </xsd:schema>
      --------------------------

      -------------------------- test.xml
      <?xml version="1.0"?>
      <test:E xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:test="test"
          xsi:schemaLocation="test test.xsd">
      <c>1</c>
      </test:E>
      --------------------------

      To reproduce the bug compile and run the class TestRun:
      -------------------------- RestRun.java
      import javax.xml.parsers.SAXParserFactory;
      import javax.xml.parsers.SAXParser;
      import org.xml.sax.SAXException;
      import org.xml.sax.SAXParseException;
      import org.xml.sax.helpers.DefaultHandler;

      public class TestRun {

          protected static class ErrorHandler extends DefaultHandler {
              public int errorCounter = 0;

              public void error(SAXParseException e) throws SAXException {
                  errorCounter++;
              }

              public void fatalError(SAXParseException e) throws SAXException {
                  errorCounter++;
              }
          }

          public static void main(String [] args) {
              ErrorHandler eh = new ErrorHandler();

              try {
                  SAXParserFactory spf = SAXParserFactory.newInstance();
                  spf.setNamespaceAware(true);
                  spf.setValidating(true);
                  SAXParser parser = spf.newSAXParser();

                  parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                                     "http://www.w3.org/2001/XMLSchema");

                  parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
                                     "test.xsd");
       
                  parser.parse("test.xml", (DefaultHandler)eh);
              } catch (Exception e) {
                  exit(1, "Failed: " + e.toString());
              }
              
              exit(eh.errorCounter, "OK");
          }

          public static void exit(int errCode, String msg) {
              System.out.println(msg);
              System.exit(errCode);
          }
      }
      --------------------------

      as the following log shows:
      --------------------------
      $ls
      TestRun.java test.xml test.xsd
      $javac TestRun.java && java TestRun; echo $?
      OK
      0
      --------------------------

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

            nbajajsunw Neeraj Bajaj (Inactive)
            reysunw Rey Rey (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: