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

Schema parser fails with NPE if ErrorHandler is set

XMLWordPrintable

        Attached invalid schematas lead to NPE if ErrorHandler is set.

        The parseSchemaDoc method which fails with NPE:
        -------Code snippet--------------
        class TestRun {
           
            protected static class ErrorHandler extends DefaultHandler {
            public int warningCounter = 0;
            public int errorCounter = 0;
            protected final PrintWriter err;

            public ErrorHandler(PrintWriter err) {
              this.err = err;
            }

            public void warning(SAXParseException e) throws SAXException {
              warningCounter++;
              err.println("Warning: " + strException(e));
              err.flush();
            }

            public void error(SAXParseException e) throws SAXException {
              errorCounter++;
              err.println("Error: " + strException(e));
              err.flush();
            }

            public void fatalError(SAXParseException e) throws SAXException {
              err.println("Fatal Error: " + strException(e));
              err.flush();
              throw e;
            }
          }
          
          static public Status parseSchemaDoc(StreamSource sources) {
            SchemaFactory schemaFactory =
                    SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
            
            ErrorHandler errorHandler = new ErrorHandler(new PrintWriter(System.out)); //!!
            
            schemaFactory.setErrorHandler(errorHandler);
           
            try {
                schemaFactory.newSchema(sources); //!!
            } catch (SAXException e) {
                String msg = "Fatal Error: " + strException(e);
                e.printStackTrace();
                return Status.failed(msg);
            }

            if (errorHandler.errorCounter != 0) {
                return Status.failed(errorHandler.errorCounter + " errors, "
                        + errorHandler.warningCounter + " warnings.");
            }
            return Status.passed("OK.");
          }
          
          protected static String strException(Exception ex) {
                StringBuffer sb = new StringBuffer();

                while (ex != null) {
                    if (ex instanceof SAXParseException) {
                        SAXParseException e = (SAXParseException) ex;
                        sb.append("" + e.getSystemId() + "(" + e.getLineNumber() + ","
                                + e.getColumnNumber() + "): " + e.getMessage());
                        ex = e.getException();
                    } else {
                        sb.append(ex);
                        ex = null;
                    }
                }
                return sb.toString();
            }

        ...............
        }
        -----End of code snippet----------------
        The stack trace is as follows:

        Error: file:/files/devel/ws/jck/7/_build/out/runtime/shared/suite/JCK-runtime-7/tests/xml_schema/msData/notations/notatF037.xsd(5,71): s4s-elt-invalid: Element 'notation' is not a valid element in a schema document.
        Error: file:/files/devel/ws/jck/7/_build/out/runtime/shared/suite/JCK-runtime-7/tests/xml_schema/msData/notations/notatF037.xsd(5,71): s4s-elt-must-match.1: The content of 'identity constraint' must match (annotation?, selector, field+). A problem was found starting at: selector.
        Exception in thread "main" java.lang.NullPointerException
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAbstractIDConstraintTraverser.traverseIdentityConstraint(XSDAbstractIDConstraintTraverser.java:107)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDUniqueOrKeyTraverser.traverse(XSDUniqueOrKeyTraverser.java:74)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(XSDElementTraverser.java:401)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseGlobal(XSDElementTraverser.java:217)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(XSDHandler.java:1262)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:583)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:555)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:522)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:488)
        at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:214)
        at javasoft.sqe.tests.api.xml.schema.TestRun.parseSchemaDoc(TestRun.java:316)
        at javasoft.sqe.tests.api.xml.schema.TestRun.runPositiveTest(TestRun.java:251)
        at javasoft.sqe.tests.api.xml.schema.TestRun.runNegativeTest(TestRun.java:329)
        at javasoft.sqe.tests.api.xml.schema.TestRun.run(TestRun.java:119)
        at javasoft.sqe.tests.api.xml.schema.TestRun.setLogsAndRun(TestRun.java:342)
        at javasoft.sqe.tests.api.xml.schema.TestRun.main(TestRun.java:350)

        Also the same NPE can be shown if the standard JDK tool xjc is used

        > xjc notatF071.xsd
        parsing a schema...
        [ERROR] s4s-elt-invalid: Element 'notation' is not a valid element in a schema document.
          line 5 of file:/files/devel/analysis/notatF071.xsd

        [ERROR] s4s-elt-must-match.1: The content of 'identity constraint' must match (annotation?, selector, field+). A problem was found starting at: selector.
          line 5 of file:/files/devel/analysis/notatF071.xsd

        Exception in thread "main" java.lang.NullPointerException
                at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAbstractIDConstraintTraverser.traverseIdentityConstraint(XSDAbstractIDConstraintTraverser.java:104)
                at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDUniqueOrKeyTraverser.traverse(XSDUniqueOrKeyTraverser.java:71)
                at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(XSDElementTraverser.java:398)
                at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseGlobal(XSDElementTraverser.java:214)
                at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(XSDHandler.java:1258)
                at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:579)
                at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:552)
                at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:519)
                at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:485)
                at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:210)
                at com.sun.tools.internal.xjc.reader.xmlschema.parser.SchemaConstraintChecker.check(SchemaConstraintChecker.java:90)
                at com.sun.tools.internal.xjc.ModelLoader.loadXMLSchema(ModelLoader.java:346)
                at com.sun.tools.internal.xjc.ModelLoader.load(ModelLoader.java:156)
                at com.sun.tools.internal.xjc.ModelLoader.load(ModelLoader.java:102)
                at com.sun.tools.internal.xjc.Driver.run(Driver.java:302)
                at com.sun.tools.internal.xjc.Driver.run(Driver.java:180)
                at com.sun.tools.internal.xjc.Driver._main(Driver.java:105)
                at com.sun.tools.internal.xjc.Driver.access$000(Driver.java:63)
                at com.sun.tools.internal.xjc.Driver$1.run(Driver.java:85)

              joehw Joe Wang
              lkuskov Leonid Kuskov
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: