1 /* 2 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package transform; 25 26 import static jaxp.library.JAXPTestUtilities.setSystemProperty; 27 import java.io.StringReader; 28 import java.io.StringWriter; 29 import javax.xml.stream.XMLEventReader; 30 import javax.xml.stream.XMLEventWriter; 31 import javax.xml.stream.XMLInputFactory; 32 import javax.xml.stream.XMLOutputFactory; 33 import javax.xml.stream.XMLStreamConstants; 34 import javax.xml.stream.XMLStreamException; 35 import javax.xml.stream.XMLStreamReader; 36 import javax.xml.transform.Transformer; 37 import javax.xml.transform.TransformerConfigurationException; 38 import javax.xml.transform.TransformerException; 39 import javax.xml.transform.TransformerFactory; 40 import javax.xml.transform.dom.DOMResult; 41 import javax.xml.transform.stax.StAXResult; 42 import javax.xml.transform.stax.StAXSource; 43 import javax.xml.transform.stream.StreamResult; 44 45 import org.testng.Assert; 46 import org.testng.annotations.Listeners; 47 import org.testng.annotations.Test; 48 49 /* 50 * @test 51 * @bug 8152530 8202426 52 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 53 * @modules java.xml 54 * @modules java.xml/com.sun.org.apache.xerces.internal.impl 55 * @modules java.xml/com.sun.org.apache.xerces.internal.xni.parser 56 * @modules java.xml/com.sun.xml.internal.stream 57 * @clean MyXMLInputFactoryImpl MyXMLStreamReader 58 * @build MyXMLInputFactoryImpl MyXMLStreamReader 59 * @run testng/othervm -DrunSecMngr=true transform.StAXSourceTest 60 * @run testng/othervm transform.StAXSourceTest 61 * @summary Test parsing from StAXSource. 62 */ 63 @Listeners({jaxp.library.JAXPTestPolicy.class}) 64 public class StAXSourceTest { 65 /** 66 * @bug 8202426 67 * Verifies that a null Attribute type is handled. NPE was thrown before the fix. 68 */ 69 @Test 70 public final void testAttributeTypeNull() throws Exception { 71 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" + 72 "<t:test xmlns:t=\"http://www.example.org/Test\" attr=\"value\" /> "; 73 setSystemProperty("javax.xml.stream.XMLInputFactory", "transform.MyXMLInputFactoryImpl"); 74 XMLInputFactory xif = XMLInputFactory.newInstance(); 75 XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); 76 TransformerFactory tf = TransformerFactory.newInstance(); 77 Transformer t = tf.newTransformer(); 78 79 while (xsr.hasNext()) { 80 xsr.next(); 81 if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) { 82 t.reset(); 83 DOMResult result = new DOMResult(); 84 t.transform(new StAXSource(xsr), result); | 1 /* 2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package transform; 25 26 import java.io.ByteArrayInputStream; 27 import static jaxp.library.JAXPTestUtilities.setSystemProperty; 28 import java.io.StringReader; 29 import java.io.StringWriter; 30 import javax.xml.stream.XMLEventReader; 31 import javax.xml.stream.XMLEventWriter; 32 import javax.xml.stream.XMLInputFactory; 33 import javax.xml.stream.XMLOutputFactory; 34 import javax.xml.stream.XMLStreamConstants; 35 import javax.xml.stream.XMLStreamException; 36 import javax.xml.stream.XMLStreamReader; 37 import javax.xml.transform.Result; 38 import javax.xml.transform.Source; 39 import javax.xml.transform.Transformer; 40 import javax.xml.transform.TransformerConfigurationException; 41 import javax.xml.transform.TransformerException; 42 import javax.xml.transform.TransformerFactory; 43 import javax.xml.transform.dom.DOMResult; 44 import javax.xml.transform.stax.StAXResult; 45 import javax.xml.transform.stax.StAXSource; 46 import javax.xml.transform.stream.StreamResult; 47 import javax.xml.transform.stream.StreamSource; 48 49 import org.testng.Assert; 50 import org.testng.annotations.DataProvider; 51 import org.testng.annotations.Listeners; 52 import org.testng.annotations.Test; 53 54 /* 55 * @test 56 * @bug 8152530 8202426 7148925 57 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 58 * @modules java.xml 59 * @modules java.xml/com.sun.org.apache.xerces.internal.impl 60 * @modules java.xml/com.sun.org.apache.xerces.internal.xni.parser 61 * @modules java.xml/com.sun.xml.internal.stream 62 * @clean MyXMLInputFactoryImpl MyXMLStreamReader 63 * @build MyXMLInputFactoryImpl MyXMLStreamReader 64 * @run testng/othervm -DrunSecMngr=true transform.StAXSourceTest 65 * @run testng/othervm transform.StAXSourceTest 66 * @summary Test parsing from StAXSource. 67 */ 68 @Listeners({jaxp.library.JAXPTestPolicy.class}) 69 public class StAXSourceTest { 70 @DataProvider(name = "xml") 71 public Object[][] getData() throws Exception { 72 // from 6715417, all other data were from 7148925 73 String xmlDT = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " 74 + "<!DOCTYPE bookstore [ " 75 + "<!ELEMENT bookstore (book)*> " 76 + "<!ELEMENT book (title,author,price)> " 77 + "<!ATTLIST book genre CDATA #REQUIRED> " 78 + "<!ELEMENT title (#PCDATA)> " 79 + "<!ELEMENT author (#PCDATA)> " 80 + "<!ELEMENT price (#PCDATA)> ]> " 81 + "<bookstore> " 82 + "<book genre=\"fantasy\" > " 83 + "<title>Oberon's Legacy</title> " 84 + "<author>Corets, Eva</author> " 85 + "<price>5.95</price> " 86 + "</book> " 87 + "</bookstore>"; 88 return new Object[][]{ 89 {"<root/>"}, 90 {"<!DOCTYPE root [<!ENTITY et 'Come Home'>]><root et='&et;'/>"}, 91 {"<?xml-stylesheet href='show.xsl' type='text/html'?><root/>"}, 92 {"<?xml version='1.0'?><?xml-stylesheet href='show.xsl' type='text/html'?><root/>"}, 93 {"<?xml version='1.0'?><?xml-stylesheet href='show.xsl' type='text/html'?>" 94 + "<!DOCTYPE root><root/>"}, 95 {"<?xml version='1.0'?><!DOCTYPE root [<!ELEMENT greeting (#PCDATA)>]><root/>"}, 96 {"<?xml version='1.0'?><?xml-stylesheet href='show.xsl' type='text/html'?>" 97 + "<!DOCTYPE root [<!ELEMENT greeting (#PCDATA)>]><root/>"}, 98 {xmlDT}, 99 }; 100 } 101 102 /** 103 * @bug 7148925 6715417 104 * 105 * Verifies that the transformation is successful with a StreamSource. 106 * 107 * @param xml the xml 108 * @throws Exception if the test fails 109 */ 110 @Test(dataProvider = "xml") 111 public void parseStreamSource(String xml) throws Exception { 112 Source source = new StreamSource(new StringReader(xml)); 113 transform(source, xml); 114 } 115 116 /** 117 * @bug 7148925 6715417 118 * 119 * Verifies that the transformation is successful with a StAXSource created 120 * out of a StreamReader. 121 * 122 * Note that the patch fixes the Exception, but does not include any improvement 123 * over the current. The result may differ from that of StreamSource. 124 * 125 * @param xml the xml 126 * @throws Exception if the test fails 127 */ 128 @Test(dataProvider = "xml") 129 public void parseSSSR(String xml) throws Exception { 130 XMLInputFactory xif = XMLInputFactory.newDefaultFactory(); 131 XMLStreamReader sr = xif.createXMLStreamReader(new StringReader(xml)); 132 StAXSource source = new StAXSource(sr); 133 transform(source, xml); 134 } 135 136 /** 137 * @bug 7148925 6715417 138 * 139 * Verifies that the transformation is successful with a StAXSource created 140 * out of an EventReader. 141 * 142 * Note that the patch fixes the Exception, but does not include any improvement 143 * over the current. The result may differ from that of StreamSource. 144 * 145 * @param xml the xml 146 * @throws Exception if the test fails 147 */ 148 @Test(dataProvider = "xml") 149 public void parseSSER(String xml) throws Exception { 150 XMLInputFactory xif = XMLInputFactory.newDefaultFactory(); 151 XMLEventReader er = xif.createXMLEventReader(new StringReader(xml)); 152 StAXSource source = new StAXSource(er); 153 transform(source, xml); 154 } 155 156 private void transform(Source source, String sourceXml) throws Exception{ 157 StringWriter sw = new StringWriter(); 158 Result result = new StreamResult(sw); 159 TransformerFactory tf = TransformerFactory.newInstance(); 160 tf.newTransformer().transform(source, result); 161 System.out.printf("%n%s:%nSource: %s%nResult: %s%n", source.getClass().getSimpleName(), sourceXml, sw); 162 } 163 164 /** 165 * @bug 8202426 166 * Verifies that a null Attribute type is handled. NPE was thrown before the fix. 167 */ 168 @Test 169 public final void testAttributeTypeNull() throws Exception { 170 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" + 171 "<t:test xmlns:t=\"http://www.example.org/Test\" attr=\"value\" /> "; 172 setSystemProperty("javax.xml.stream.XMLInputFactory", "transform.MyXMLInputFactoryImpl"); 173 XMLInputFactory xif = XMLInputFactory.newInstance(); 174 XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); 175 TransformerFactory tf = TransformerFactory.newInstance(); 176 Transformer t = tf.newTransformer(); 177 178 while (xsr.hasNext()) { 179 xsr.next(); 180 if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) { 181 t.reset(); 182 DOMResult result = new DOMResult(); 183 t.transform(new StAXSource(xsr), result); |