changeset 41506:72977e59ef4b

8152530: NullPointerException when xmlns="" Reviewed-by: dfuchs, naoto, lancea
author joehw
date Tue, 11 Oct 2016 17:39:31 -0700
parents 0875007901f7
children eee8855a6cc9
files jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java jaxp/test/javax/xml/jaxp/unittest/transform/StAXSourceTest.java
diffstat 2 files changed, 38 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java	Wed Jul 05 22:19:47 2017 +0200
+++ b/jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/StAXStream2SAX.java	Tue Oct 11 17:39:31 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2016, 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
@@ -305,9 +305,12 @@
                 if (prefix == null) { // true for default namespace
                     prefix = "";
                 }
-                _sax.startPrefixMapping(
-                    prefix,
-                    staxStreamReader.getNamespaceURI(i));
+                String uri = staxStreamReader.getNamespaceURI(i);
+                if (uri == null && prefix.isEmpty()) { // true for default namespace
+                    uri = "";
+                }
+
+                _sax.startPrefixMapping(prefix, uri);
             }
 
             // fire startElement
--- a/jaxp/test/javax/xml/jaxp/unittest/transform/StAXSourceTest.java	Wed Jul 05 22:19:47 2017 +0200
+++ b/jaxp/test/javax/xml/jaxp/unittest/transform/StAXSourceTest.java	Tue Oct 11 17:39:31 2016 -0700
@@ -30,7 +30,9 @@
 import javax.xml.stream.XMLEventWriter;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
@@ -38,6 +40,7 @@
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.stax.StAXResult;
 import javax.xml.transform.stax.StAXSource;
+import javax.xml.transform.stream.StreamResult;
 
 import org.testng.Assert;
 import org.testng.annotations.Listeners;
@@ -45,6 +48,7 @@
 
 /*
  * @test
+ * @bug 8152530
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true transform.StAXSourceTest
  * @run testng/othervm transform.StAXSourceTest
@@ -52,6 +56,33 @@
  */
 @Listeners({jaxp.library.FilePolicy.class})
 public class StAXSourceTest {
+    /**
+     * @bug 8152530
+     * Verifies that StAXSource handles empty namespace properly. NPE was thrown
+     * before the fix.
+     * @throws Exception if the test fails
+     */
+    @Test
+    public final void testStAXSourceWEmptyNS() throws Exception {
+        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            + "<EntityList>\n"
+            + "  <Entity xmlns=\"\">\n"
+            + "  </Entity>\n"
+            + "  <Entity xmlns=\"\">\n"
+            + "  </Entity>\n"
+            + "</EntityList> ";
+
+        XMLInputFactory xif = XMLInputFactory.newInstance();
+        XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
+        xsr.nextTag();
+        TransformerFactory tf = TransformerFactory.newInstance();
+        Transformer t = tf.newTransformer();
+        while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().equals("Entity")) {
+            StringWriter stringResult = new StringWriter();
+            t.transform(new StAXSource(xsr), new StreamResult(stringResult));
+            System.out.println("result: \n" + stringResult.toString());
+        }
+    }
 
     @Test
     public final void testStAXSource() throws XMLStreamException {