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

NPE thrown by Transformer when XMLStreamReader reports no xml attribute type

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8, 9, 10, 11
    • Fix Version/s: 11
    • Component/s: xml

      Backports

        Description

        ADDITIONAL SYSTEM INFORMATION :
        OS Name: Microsoft Windows 10 Enterprise
        OS Version: 10.0.16299 N/A Build 16299

        java version "1.8.0_172"
        Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
        Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

        A DESCRIPTION OF THE PROBLEM :
        The javax.xml.transform.Transformer.transform will throw NullPointerException when transforming from a StAXSource to a DOMResult if the source XMLStreamReader implementation's getAttributeType(int) method returns null.

        The NullPointerException seems to be caused by a missing null check at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.startElement(SAX2DOM.java:204).



        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the main() method of the BugDemo class from the attached maven project to observe the exception.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Since the stax specification doesn't seem to explicitly forbid XMLStreamReader.getAttributeType(int index) methods from returning null values. The SAX2DOM class should therefore perform extra checks to prevent NullPointerExceptions in cases where XMLStreamReaders report unknown attribute types as null.
        ACTUAL -
        java.lang.NullPointerException
        at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.startElement(SAX2DOM.java:204)
        at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.closeStartTag(ToXMLSAXHandler.java:208)
        at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.flushPending(ToSAXHandler.java:281)
        at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.startElement(ToXMLSAXHandler.java:650)
        at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartElement(StAXStream2SAX.java:319)
        at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(StAXStream2SAX.java:145)
        at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(StAXStream2SAX.java:101)
        at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:688)
        at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:737)
        at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351)

        ---------- BEGIN SOURCE ----------
        File 1 of 4: /sax2domerror/pom.xml
        -------------------------------------------------------------------------------

        <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>sax2domerror</groupId>
        <artifactId>sax2domerror</artifactId>
        <version>0.0.1-SNAPSHOT</version>

        <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        </properties>

        <dependencies>
        <dependency>
        <groupId>com.fasterxml.woodstox</groupId>
        <artifactId>woodstox-core</artifactId>
        <version>5.1.0</version>
        </dependency>
        <dependency>
        <groupId>net.java.dev.msv</groupId>
        <artifactId>msv-core</artifactId>
        <version>2013.6.1</version>
        </dependency>
        </dependencies>

        </project>

        ---- END OF FILE ----

        File 2 of 4: /sax2domerror/src/main/java/sax2domerror/BugDemo.java
        -------------------------------------------------------------------------------

        package sax2domerror;

        import java.net.URL;

        import javax.xml.stream.FactoryConfigurationError;
        import javax.xml.stream.XMLInputFactory;
        import javax.xml.stream.XMLStreamConstants;
        import javax.xml.stream.XMLStreamException;
        import javax.xml.transform.Transformer;
        import javax.xml.transform.TransformerException;
        import javax.xml.transform.TransformerFactory;
        import javax.xml.transform.TransformerFactoryConfigurationError;
        import javax.xml.transform.dom.DOMResult;
        import javax.xml.transform.stax.StAXSource;

        import org.codehaus.stax2.XMLInputFactory2;
        import org.codehaus.stax2.XMLStreamReader2;
        import org.codehaus.stax2.validation.XMLValidationSchema;
        import org.codehaus.stax2.validation.XMLValidationSchemaFactory;

        public class BugDemo {

        public static void main(String[] args) throws XMLStreamException, FactoryConfigurationError,
        TransformerFactoryConfigurationError, TransformerException {

        URL xmlFile = BugDemo.class.getResource("/Test.xml");
        URL schemaFile = BugDemo.class.getResource("/Test.xsd");

        XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory2.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, true);

        XMLValidationSchema xmlValidationSchema = XMLValidationSchemaFactory
        .newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA).createSchema(schemaFile);

        XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlFile);
        xmlStreamReader.validateAgainst(xmlValidationSchema);

        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        while (xmlStreamReader.hasNext()) {
        xmlStreamReader.next();
        if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
        transformer.reset();
        DOMResult result = new DOMResult();
        transformer.transform(new StAXSource(xmlStreamReader), result);
        }
        }

        }

        }

        ---- END OF FILE ----

        File 3 of 4: /sax2domerror/src/main/resources/test.xml
        -------------------------------------------------------------------------------

        <?xml version="1.0" encoding="UTF-8"?>
        <t:test xmlns:t="http://www.example.org/Test" attr="value" />

        ---- END OF FILE ----


        File 4 of 4: /sax2domerror/src/main/resources/test.xsd
        -------------------------------------------------------------------------------

        <?xml version="1.0" encoding="UTF-8"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Test"
        xmlns:tns="http://www.example.org/Test" elementFormDefault="qualified">
        <element name="test">
        <complexType>
        <attribute name="attr" type="string" />
        </complexType>
        </element>
        </schema>

        ---- END OF FILE ----
        ---------- END SOURCE ----------

        FREQUENCY : always


          Attachments

            Issue Links

              Activity

                People

                Assignee:
                joehw Joe Wang
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: