1 /* 2 * Copyright (c) 2014, 2016, 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.StringReader; 27 import java.io.StringWriter; 28 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.XMLStreamException; 34 import javax.xml.transform.Transformer; 35 import javax.xml.transform.TransformerConfigurationException; 36 import javax.xml.transform.TransformerException; 37 import javax.xml.transform.TransformerFactory; 38 import javax.xml.transform.dom.DOMResult; 39 import javax.xml.transform.stax.StAXResult; 40 import javax.xml.transform.stax.StAXSource; 41 42 import org.testng.Assert; 43 import org.testng.annotations.Listeners; 44 import org.testng.annotations.Test; 45 46 /* 47 * @summary Test parsing from StAXSource. 48 */ 49 @Listeners({jaxp.library.FilePolicy.class}) 50 public class StAXSourceTest { 51 52 @Test 53 public final void testStAXSource() throws XMLStreamException { 54 XMLInputFactory ifactory = XMLInputFactory.newInstance(); 55 XMLOutputFactory ofactory = XMLOutputFactory.newInstance(); 56 57 String xslStylesheet = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" 58 + " <xsl:output method='xml' encoding='utf-8' indent='no'/>" + " <xsl:preserve-space elements='*'/>" + " <xsl:template match='*'>" 59 + " <xsl:copy><xsl:copy-of select='@*'/><xsl:apply-templates/></xsl:copy>" + " </xsl:template>" 60 + " <xsl:template match='comment()|processing-instruction()|text()'>" + " <xsl:copy/>" + " </xsl:template>" + "</xsl:stylesheet>"; 61 StringReader xslStringReader = new StringReader(xslStylesheet); 62 StringReader xmlStringReader = new StringReader(xslStylesheet); // identity 63 // on 64 // itself, 65 StringWriter xmlStringWriter = new StringWriter(); 66 67 XMLEventReader styleReader = ifactory.createXMLEventReader(xslStringReader); 68 XMLEventReader docReader = ifactory.createXMLEventReader(xmlStringReader); 69 XMLEventWriter writer = ofactory.createXMLEventWriter(xmlStringWriter); 70 71 StAXSource stylesheet = new StAXSource(styleReader); 72 StAXSource document = new StAXSource(docReader); 73 StAXResult result = new StAXResult(writer); 74 75 try { 76 document.setSystemId("sourceSystemId"); 77 } catch (UnsupportedOperationException e) { 78 System.out.println("Expected UnsupportedOperationException in StAXSource.setSystemId()"); 79 } catch (Exception e) { 80 Assert.fail("StAXSource.setSystemId() does not throw java.lang.UnsupportedOperationException"); 81 } 82 83 TransformerFactory tfactory = TransformerFactory.newInstance(); 84 try { 85 Transformer transformer = tfactory.newTransformer(stylesheet); 86 transformer.transform(document, result); 87 } catch (TransformerConfigurationException tce) { 88 throw new XMLStreamException(tce); 89 } catch (TransformerException te) { 90 throw new XMLStreamException(te); 91 } finally { 92 styleReader.close(); 93 docReader.close(); 94 writer.close(); 95 } 96 97 try { 98 result.setSystemId("systemId"); 99 } catch (UnsupportedOperationException e) { 100 System.out.println("Expected UnsupportedOperationException in StAXResult.setSystemId()"); 101 } catch (Exception e) { 102 Assert.fail("StAXResult.setSystemId() does not throw java.lang.UnsupportedOperationException"); 103 } 104 105 if (result.getSystemId() != null) { 106 Assert.fail("StAXResult.getSystemId() does not return null"); 107 } 108 } 109 110 @Test 111 public final void testStAXSource2() throws XMLStreamException { 112 XMLInputFactory ifactory = XMLInputFactory.newInstance(); 113 ifactory.setProperty("javax.xml.stream.supportDTD", Boolean.TRUE); 114 115 StAXSource ss = new StAXSource(ifactory.createXMLStreamReader(getClass().getResource("5368141.xml").toString(), 116 getClass().getResourceAsStream("5368141.xml"))); 117 DOMResult dr = new DOMResult(); 118 119 TransformerFactory tfactory = TransformerFactory.newInstance(); 120 try { 121 Transformer transformer = tfactory.newTransformer(); 122 transformer.transform(ss, dr); 123 } catch (Exception e) { 124 Assert.fail(e.getMessage()); 125 } 126 } 127 }