8152530: NullPointerException when xmlns=""

Reviewed-by: dfuchs, naoto, lancea
This commit is contained in:
Joe Wang 2016-10-11 17:39:31 -07:00
parent e91b43837d
commit b414bb7023
2 changed files with 38 additions and 4 deletions

View File

@ -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 @@ public class StAXStream2SAX implements XMLReader, Locator {
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

View File

@ -30,7 +30,9 @@ import javax.xml.stream.XMLEventReader;
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.TransformerFactory;
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 @@ import org.testng.annotations.Test;
/*
* @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 @@ import org.testng.annotations.Test;
*/
@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 {