8202426: NPE thrown by Transformer when XMLStreamReader reports no xml attribute type
authorjoehw
Tue, 15 May 2018 13:28:08 -0700
changeset 50114 d2cfda6a00de
parent 50113 caf115bb98ad
child 50115 94a048a97de4
8202426: NPE thrown by Transformer when XMLStreamReader reports no xml attribute type Reviewed-by: lancea
src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2DOM.java
test/jaxp/javax/xml/jaxp/libs/jaxp/library/JAXPTestPolicy.java
test/jaxp/javax/xml/jaxp/unittest/transform/MyXMLInputFactoryImpl.java
test/jaxp/javax/xml/jaxp/unittest/transform/MyXMLStreamReader.java
test/jaxp/javax/xml/jaxp/unittest/transform/StAXSourceTest.java
--- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2DOM.java	Tue May 15 20:24:34 2018 +0200
+++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2DOM.java	Tue May 15 13:28:08 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -22,6 +22,7 @@
 package com.sun.org.apache.xalan.internal.xsltc.trax;
 
 import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants;
+import com.sun.org.apache.xerces.internal.util.XMLSymbols;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Stack;
@@ -194,14 +195,16 @@
             // checking if Namespace processing is being done
             String attQName = attrs.getQName(i);
             String attURI = attrs.getURI(i);
+            String type = (attrs.getType(i) == null) ?
+                    XMLSymbols.fCDATASymbol : attrs.getType(i);
             if (attrs.getLocalName(i).equals("")) {
                 tmp.setAttribute(attQName, attrs.getValue(i));
-                if (attrs.getType(i).equals("ID")) {
+                if (type.equals("ID")) {
                     tmp.setIdAttribute(attQName, true);
                 }
             } else {
                 tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
-                if (attrs.getType(i).equals("ID")) {
+                if (type.equals("ID")) {
                     tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
                 }
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/libs/jaxp/library/JAXPTestPolicy.java	Tue May 15 13:28:08 2018 -0700
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jaxp.library;
+
+import static jaxp.library.JAXPTestUtilities.getSystemProperty;
+import java.io.FilePermission;
+import java.net.SocketPermission;
+import org.testng.ITestContext;
+
+/**
+ * Covers all policies currently required for running JAXP tests
+ */
+public class JAXPTestPolicy extends BasePolicy {
+    @Override
+    public void onStart(ITestContext arg0) {
+        if (isRunWithSecurityManager()) {
+            JAXPPolicyManager policyManager = JAXPPolicyManager.getJAXPPolicyManager(true);
+            String userdir = getSystemProperty("user.dir");
+            policyManager.addPermission(new FilePermission(userdir + "/-", "read,write,delete"));
+            String testSrc = System.getProperty("test.src");
+            // to handle the directory structure of some functional test suite
+            if (testSrc.endsWith("ptests"))
+                testSrc = testSrc.substring(0, testSrc.length() - 7);
+            policyManager.addPermission(new FilePermission(testSrc + "/-", "read"));
+            policyManager.addPermission(new FilePermission(userdir, "read"));
+
+            policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xerces.internal.jaxp"));
+            policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xerces.internal.impl"));
+            policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xerces.internal.xni.parser"));
+            policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.bcel.internal.classfile"));
+            policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.bcel.internal.generic"));
+            policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xalan.internal.xsltc.trax"));
+            policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.xml.internal.stream"));
+
+            policyManager.addPermission(new SocketPermission("openjdk.java.net:80", "connect,resolve"));
+            policyManager.addPermission(new SocketPermission("www.w3.org:80", "connect,resolve"));
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/transform/MyXMLInputFactoryImpl.java	Tue May 15 13:28:08 2018 -0700
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package transform;
+
+import com.sun.org.apache.xerces.internal.impl.PropertyManager;
+import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
+import com.sun.xml.internal.stream.XMLInputFactoryImpl;
+import java.io.Reader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+public class MyXMLInputFactoryImpl extends XMLInputFactoryImpl {
+    //List of supported properties and default values.
+
+    private PropertyManager fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_READER);
+
+    public MyXMLInputFactoryImpl() {
+        super();
+    }
+
+    public XMLStreamReader createXMLStreamReader(Reader reader) throws XMLStreamException {
+        XMLInputSource inputSource = new XMLInputSource(null, null, null, reader, null);
+        return getXMLStreamReaderImpl(inputSource);
+    }
+
+    XMLStreamReader getXMLStreamReaderImpl(XMLInputSource inputSource) throws javax.xml.stream.XMLStreamException {
+        return new MyXMLStreamReader(inputSource, new PropertyManager(fPropertyManager));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/transform/MyXMLStreamReader.java	Tue May 15 13:28:08 2018 -0700
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package transform;
+
+import com.sun.org.apache.xerces.internal.impl.PropertyManager;
+import com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl;
+import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
+import java.io.InputStream;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+public class MyXMLStreamReader extends XMLStreamReaderImpl implements XMLStreamReader {
+
+    public MyXMLStreamReader(InputStream inputStream, PropertyManager props) throws XMLStreamException {
+        super(inputStream, props);
+    }
+
+    public MyXMLStreamReader(XMLInputSource inputSource, PropertyManager props)
+            throws XMLStreamException {
+        super(inputSource, props);
+    }
+
+    public String getAttributeType(int index) {
+        return null;
+    }
+}
--- a/test/jaxp/javax/xml/jaxp/unittest/transform/StAXSourceTest.java	Tue May 15 20:24:34 2018 +0200
+++ b/test/jaxp/javax/xml/jaxp/unittest/transform/StAXSourceTest.java	Tue May 15 13:28:08 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,9 +23,9 @@
 
 package transform;
 
+import static jaxp.library.JAXPTestUtilities.setSystemProperty;
 import java.io.StringReader;
 import java.io.StringWriter;
-
 import javax.xml.stream.XMLEventReader;
 import javax.xml.stream.XMLEventWriter;
 import javax.xml.stream.XMLInputFactory;
@@ -48,15 +48,45 @@
 
 /*
  * @test
- * @bug 8152530
+ * @bug 8152530 8202426
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
+ * @modules java.xml
+ * @modules java.xml/com.sun.org.apache.xerces.internal.impl
+ * @modules java.xml/com.sun.org.apache.xerces.internal.xni.parser
+ * @modules java.xml/com.sun.xml.internal.stream
+ * @clean MyXMLInputFactoryImpl MyXMLStreamReader
+ * @build MyXMLInputFactoryImpl MyXMLStreamReader
  * @run testng/othervm -DrunSecMngr=true transform.StAXSourceTest
  * @run testng/othervm transform.StAXSourceTest
  * @summary Test parsing from StAXSource.
  */
-@Listeners({jaxp.library.FilePolicy.class})
+@Listeners({jaxp.library.JAXPTestPolicy.class})
 public class StAXSourceTest {
     /**
+     * @bug 8202426
+     * Verifies that a null Attribute type is handled. NPE was thrown before the fix.
+     */
+    @Test
+    public final void testAttributeTypeNull() throws Exception {
+        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
+            "<t:test xmlns:t=\"http://www.example.org/Test\" attr=\"value\" /> ";
+        setSystemProperty("javax.xml.stream.XMLInputFactory", "transform.MyXMLInputFactoryImpl");
+        XMLInputFactory xif = XMLInputFactory.newInstance();
+        XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
+        TransformerFactory tf = TransformerFactory.newInstance();
+        Transformer t = tf.newTransformer();
+
+        while (xsr.hasNext()) {
+            xsr.next();
+            if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) {
+                t.reset();
+                DOMResult result = new DOMResult();
+                t.transform(new StAXSource(xsr), result);
+            }
+        }
+    }
+
+    /**
      * @bug 8152530
      * Verifies that StAXSource handles empty namespace properly. NPE was thrown
      * before the fix.