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); 85 } 86 } 87 } 88 89 /** 90 * @bug 8152530 91 * Verifies that StAXSource handles empty namespace properly. NPE was thrown 92 * before the fix. 93 * @throws Exception if the test fails 94 */ 95 @Test 96 public final void testStAXSourceWEmptyNS() throws Exception { 97 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 98 + "<EntityList>\n" 99 + " <Entity xmlns=\"\">\n" 100 + " </Entity>\n" 101 + " <Entity xmlns=\"\">\n" 102 + " </Entity>\n" 103 + "</EntityList> "; 104 105 XMLInputFactory xif = XMLInputFactory.newInstance(); 106 XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); 107 xsr.nextTag(); 108 TransformerFactory tf = TransformerFactory.newInstance(); 109 Transformer t = tf.newTransformer(); 110 while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().equals("Entity")) { 111 StringWriter stringResult = new StringWriter(); 112 t.transform(new StAXSource(xsr), new StreamResult(stringResult)); 113 System.out.println("result: \n" + stringResult.toString()); 114 } 115 } 116 117 @Test 118 public final void testStAXSource() throws XMLStreamException { 119 XMLInputFactory ifactory = XMLInputFactory.newInstance(); 120 XMLOutputFactory ofactory = XMLOutputFactory.newInstance(); 121 122 String xslStylesheet = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" 123 + " <xsl:output method='xml' encoding='utf-8' indent='no'/>" + " <xsl:preserve-space elements='*'/>" + " <xsl:template match='*'>" 124 + " <xsl:copy><xsl:copy-of select='@*'/><xsl:apply-templates/></xsl:copy>" + " </xsl:template>" 125 + " <xsl:template match='comment()|processing-instruction()|text()'>" + " <xsl:copy/>" + " </xsl:template>" + "</xsl:stylesheet>"; 126 StringReader xslStringReader = new StringReader(xslStylesheet); 127 StringReader xmlStringReader = new StringReader(xslStylesheet); // identity 128 // on 129 // itself, 130 StringWriter xmlStringWriter = new StringWriter(); 131 132 XMLEventReader styleReader = ifactory.createXMLEventReader(xslStringReader); 133 XMLEventReader docReader = ifactory.createXMLEventReader(xmlStringReader); 134 XMLEventWriter writer = ofactory.createXMLEventWriter(xmlStringWriter); 135 136 StAXSource stylesheet = new StAXSource(styleReader); 137 StAXSource document = new StAXSource(docReader); 138 StAXResult result = new StAXResult(writer); 139 140 try { 141 document.setSystemId("sourceSystemId"); 142 } catch (UnsupportedOperationException e) { 143 System.out.println("Expected UnsupportedOperationException in StAXSource.setSystemId()"); 144 } catch (Exception e) { 145 Assert.fail("StAXSource.setSystemId() does not throw java.lang.UnsupportedOperationException"); 146 } 147 148 TransformerFactory tfactory = TransformerFactory.newInstance(); 149 try { 150 Transformer transformer = tfactory.newTransformer(stylesheet); 151 transformer.transform(document, result); 152 } catch (TransformerConfigurationException tce) { 153 throw new XMLStreamException(tce); 154 } catch (TransformerException te) { 155 throw new XMLStreamException(te); 156 } finally { 157 styleReader.close(); 158 docReader.close(); 159 writer.close(); 160 } 161 162 try { 163 result.setSystemId("systemId"); 164 } catch (UnsupportedOperationException e) { 165 System.out.println("Expected UnsupportedOperationException in StAXResult.setSystemId()"); 166 } catch (Exception e) { 167 Assert.fail("StAXResult.setSystemId() does not throw java.lang.UnsupportedOperationException"); 168 } 169 170 if (result.getSystemId() != null) { 171 Assert.fail("StAXResult.getSystemId() does not return null"); 172 } 173 } 174 175 @Test 176 public final void testStAXSource2() throws XMLStreamException { 177 XMLInputFactory ifactory = XMLInputFactory.newInstance(); 178 ifactory.setProperty("javax.xml.stream.supportDTD", Boolean.TRUE); 179 180 StAXSource ss = new StAXSource(ifactory.createXMLStreamReader(getClass().getResource("5368141.xml").toString(), 181 getClass().getResourceAsStream("5368141.xml"))); 182 DOMResult dr = new DOMResult(); 183 184 TransformerFactory tfactory = TransformerFactory.newInstance(); 185 try { 186 Transformer transformer = tfactory.newTransformer(); 187 transformer.transform(ss, dr); 188 } catch (Exception e) { 189 Assert.fail(e.getMessage()); 190 } 191 } 192 }