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

SAXPArser parse() method throws IllegalStateException

XMLWordPrintable

    • 1.3
    • x86
    • windows_xp

        Name: gm110360 Date: 05/12/2004


        FULL PRODUCT VERSION :
        java version "1.5.0-beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
        Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows XP [Version 5.1.2600]

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        JAXP with the Tiger Release

        A DESCRIPTION OF THE PROBLEM :
        The parse() method throws IllegalStateException when validating a document using an XML Schema. The parser will ONLY validate if a schema is identified programmatically, not from xsi: hint.
        Note: the SAXLocalNameCount example from JAXP site does not validate - implement ignorableWhitespace() to confirm this, as a validating parser MUST call this method.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the sample code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Either correct parsing output or parsing error messages.
        ACTUAL -
        IllegalStateException was thrown on processing the end element tag for first nested element.

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        Starting parsing of sketch.xml

        Start document:
        Start "xsi" namespace scope. URI: http://www.w3.org/2001/XMLSchema-instance
        Start element: local name: sketch qname: sketch uri:
        Attributes:
          Name : xsi:noNamespaceSchemaLocation
          Type : CDATA
          Value: Sketcher.xsd
        Ignorable whitespace:

        Start element: local name: line qname: line uri:
        Attributes:
          Name : angle
          Type : CDATA
          Value: 0
        Ignorable whitespace:

        Start element: local name: color qname: color uri:
        Attributes:
          Name : R
          Type : CDATA
          Value: 0
          Name : G
          Type : CDATA
          Value: 0
          Name : B
          Type : CDATA
          Value: 255
        java.lang.IllegalStateException
                at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl+3.checkState(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl+3.getElementTypeInfo(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.elementAug(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.validation.XNI2SAXEx.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler.endElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
                at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
                at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
                at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
                at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
                at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
                at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
                at javax.xml.parsers.SAXParser.parse(Unknown Source)
                at javax.xml.parsers.SAXParser.parse(Unknown Source)
                at TrySAXHandler.process(TrySAXHandler.java:51)
                at TrySAXHandler.main(TrySAXHandler.java:20)


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import javax.xml.parsers.SAXParserFactory;
        import javax.xml.parsers.SAXParser;
        import javax.xml.parsers.ParserConfigurationException;
        import javax.xml.validation.Schema;
        import javax.xml.validation.SchemaFactory;
        import org.xml.sax.SAXException;
        import java.io.File;
        import java.io.IOException;

        public class TrySAXHandler {
          public static void main(String args[]) {
            if(args.length == 0) {
              System.out.println("No file to process. Usage is:"
                                 +"\n java TrySax \"xmlFilename\" "
                                 +"\nor:\n java TrySaxHandler \"xmlFilename\" \"schemaFileName\" ");
              return;
            }
            File xmlFile = new File(args[0]);
            File schemaFile = args.length>1 ? new File(args[1]) : null;
            process(xmlFile, schemaFile);
          }

          private static void process(File file, File schemaFile) {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = null;
            spf.setNamespaceAware(true);
            spf.setValidating(true);
            try {
             if(schemaFile != null) {
               SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
               spf.setSchema(sf.newSchema(schemaFile));
             }
             parser = spf.newSAXParser();
             parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                                   "http://www.w3.org/2001/XMLSchema");
            
            }
            catch(SAXException e) {
              e.printStackTrace(System.err);
              System.exit(1);
            }
            catch(ParserConfigurationException e) {
              e.printStackTrace(System.err);
              System.exit(1);
            }

            System.out.println("\nStarting parsing of "+file+"\n");
            MySAXHandler handler = new MySAXHandler();
            try {
               parser.parse(file, handler);
            } catch(IOException e) {
              e.printStackTrace(System.err);
            }
            catch(SAXException e) {
              e.printStackTrace(System.err);
            }
            catch(IllegalStateException e) {
              e.printStackTrace(System.err);
            }
          }
        }

        // SAX Event handler
        import org.xml.sax.helpers.DefaultHandler;
        import org.xml.sax.Attributes;
        import org.xml.sax.SAXParseException;
        import org.xml.sax.SAXException;

        public class MySAXHandler extends DefaultHandler {
          public void startDocument() {
           System.out.println("Start document: ");
          }
            public void endDocument() {
           System.out.println("End document: ");
          }
          
          public void startElement(String uri, String localName, String qname,
                                                                       Attributes attr)
          {
            System.out.println("Start element: local name: " + localName + " qname: "
                                                                + qname + " uri: "+uri);
            int attrCount = attr.getLength();
            if(attrCount>0) {
              System.out.println("Attributes:");
              for(int i = 0 ; i<attrCount ; i++) {
                System.out.println(" Name : " + attr.getQName(i));
                System.out.println(" Type : " + attr.getType(i));
                System.out.println(" Value: " + attr.getValue(i));
              }
            }
          }
          
          public void endElement(String uri, String localName, String qname) {
            System.out.println("End element: local name: " + localName + " qname: "
                                                                 + qname + " uri: "+uri);
          }
          
          public void characters(char[] ch, int start, int length) {
            System.out.println("Characters: " + new String(ch, start, length));
          }

          public void ignorableWhitespace(char[] ch, int start, int length) {
            System.out.println("Ignorable whitespace: " + length + " Characters."));
          }

          public void startPrefixMapping(String prefix, String uri) {
            System.out.println("Start \"" + prefix + "\" namespace scope. URI: " + uri);
          }

          public void endPrefixMapping(String prefix) {
            System.out.println("End \"" + prefix + "\" namespace scope.");
          }

          public void warning(SAXParseException spe) {
            System.out.println("Warning at line "+spe.getLineNumber());
            System.out.println(spe.getMessage());
          }

          public void fatalError(SAXParseException spe) throws SAXException {
            System.out.println("Fatal error at line "+spe.getLineNumber());
            System.out.println(spe.getMessage());
            throw spe;
          }
        }

        /* XML file contents:
        <?xml version="1.0" ?>
        <sketch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="Sketcher.xsd">
            <line angle="0">
                <color R="0" G="0" B="255"></color>
                <position x="10" y="10" />
                <endpoint x="30" y="40" />
            </line>
        </sketch>
        */

        /* Schema contents
        <?xml version="1.0" encoding="UTF-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            
            <xsd:element name="sketch" type="SketchType"/>

            <!--Type for a sketch root element -->
            <xsd:complexType name="SketchType">
                <xsd:choice minOccurs="0" maxOccurs="unbounded">
                    <xsd:element name="line" type="LineType"/>
                    <xsd:element name="rectangle" type="RectangleType"/>
                    <xsd:element name="circle" type="CircleType"/>
                    <xsd:element name="curve" type="CurveType"/>
                    <xsd:element name="text" type="TextType"/>
                </xsd:choice>
            </xsd:complexType>

            <!--Type for a line element -->
            <xsd:complexType name="LineType">
                <xsd:sequence>
                    <xsd:element name="color" type="ColorType"/>
                    <xsd:element name="position" type="PointType"/>
                    <xsd:element name="endpoint" type="PointType"/>
                </xsd:sequence>
                <xsd:attribute name="angle" type="xsd:double" use="required"/>
            </xsd:complexType>

            <!--Type for a rectangle element -->
            <xsd:complexType name="RectangleType">
                <xsd:sequence>
                    <xsd:element name="color" type="ColorType"/>
                    <xsd:element name="position" type="PointType"/>
                    <xsd:element name="bottomright" type="PointType"/>
                </xsd:sequence>
                <xsd:attribute name="angle" type="xsd:double" use="required"/>
            </xsd:complexType>

            <!--Type for a circle element -->
            <xsd:complexType name="CircleType">
                <xsd:sequence>
                    <xsd:element name="color" type="ColorType"/>
                    <xsd:element name="position" type="PointType"/>
                </xsd:sequence>
                <xsd:attribute name="radius" type="xsd:double" use="required"/>
                <xsd:attribute name="angle" type="xsd:double" use="required"/>
            </xsd:complexType>

            <!--Type for a curve element -->
            <xsd:complexType name="CurveType">
                <xsd:sequence>
                    <xsd:element name="color" type="ColorType"/>
                    <xsd:element name="position" type="PointType"/>
                    <xsd:element name="point" type="PointType" minOccurs="1" maxOccurs="unbounded"/>
                </xsd:sequence>
                <xsd:attribute name="angle" type="xsd:double" use="required"/>
            </xsd:complexType>

            <!--Type for a text element -->
            <xsd:complexType name="TextType">
                <xsd:sequence>
                    <xsd:element name="color" type="ColorType"/>
                    <xsd:element name="position" type="PointType"/>
                    <xsd:element name="font" type="FontType"/>
                    <xsd:element name="string" type="xsd:string"/>
                </xsd:sequence>
                <xsd:attribute name="angle" type="xsd:double" use="required"/>
            </xsd:complexType>

            <!--Type for a font element -->
            <xsd:complexType name="FontType">
                <xsd:attribute name="fontname" type="xsd:string" use="required"/>
                <xsd:attribute name="fontstyle" use="required">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="plain"/>
                            <xsd:enumeration value="bold"/>
                            <xsd:enumeration value="italic"/>
                            <xsd:enumeration value="bold-italic"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:attribute>
            </xsd:complexType>

            <!--Type for elements representing points -->
            <xsd:complexType name="PointType">
                <xsd:attributeGroup ref="coords"/>
            </xsd:complexType>

            <!--Type for a color element -->
            <xsd:complexType name="ColorType">
                <xsd:attribute name="R" type="xsd:nonNegativeInteger" use="required"/>
                <xsd:attribute name="G" type="xsd:nonNegativeInteger" use="required"/>
                <xsd:attribute name="B" type="xsd:nonNegativeInteger" use="required"/>
            </xsd:complexType>

            <!-- Attribute group specifying point coordinates -->
            <xsd:attributeGroup name="coords">
                <xsd:attribute name="x" type="xsd:double" use="required"/>
                <xsd:attribute name="y" type="xsd:double" use="required"/>
            </xsd:attributeGroup>
        </xsd:schema>
        */

        ---------- END SOURCE ----------
        (Incident Review ID: 260971)
        ======================================================================

              vkorcl Venugopal K (Inactive)
              gmanwanisunw Girish Manwani (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: