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