1 /* 2 * Copyright (c) 2003, 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 package javax.xml.transform.ptests; 24 25 import java.io.File; 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 import java.util.Arrays; 29 import java.util.Properties; 30 import javax.xml.parsers.DocumentBuilder; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.ParserConfigurationException; 33 import javax.xml.transform.Transformer; 34 import javax.xml.transform.TransformerException; 35 import javax.xml.transform.TransformerFactory; 36 import javax.xml.transform.dom.DOMSource; 37 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 38 import javax.xml.transform.stream.StreamResult; 39 import javax.xml.transform.stream.StreamSource; 40 import jaxp.library.JAXPFileBaseTest; 41 import static jaxp.library.JAXPTestUtilities.USER_DIR; 42 import static jaxp.library.JAXPTestUtilities.failUnexpected; 43 import org.testng.annotations.Test; 44 import org.w3c.dom.Document; 45 import org.xml.sax.SAXException; 46 47 /** 48 * Test a StreamResult using a file name that contains URL characters that need 49 * to be encoded. 50 */ 51 public class StreamResultTest extends JAXPFileBaseTest { 52 /** 53 * Unit test for StreamResult. 54 */ 55 @Test 56 public void testcase01() { 57 // Set Transformer properties 58 Properties transformProperties = new Properties(); 59 transformProperties.put("method", "xml"); 60 transformProperties.put("encoding", "UTF-8"); 61 transformProperties.put("omit-xml-declaration", "yes"); 62 transformProperties.put("{http://xml.apache.org/xslt}indent-amount", "0"); 63 transformProperties.put("indent", "no"); 64 transformProperties.put("standalone", "no"); 65 transformProperties.put("version", "1.0"); 66 transformProperties.put("media-type", "text/xml"); 67 68 String[] fileNames = { 69 "StreamResult01.out", 70 "StreamResult 02.out", 71 "StreamResult#03.out" 72 }; 73 74 String xslFile = XML_DIR + "cities.xsl"; 75 String xmlFile = XML_DIR + "cities.xml"; 76 77 Arrays.stream(fileNames).forEach(file -> { 78 try { 79 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 80 dbf.setNamespaceAware(true); 81 DocumentBuilder db = dbf.newDocumentBuilder(); 82 Document document = db.parse(new File(xslFile)); 83 DOMSource domSource = new DOMSource(document); 84 StreamSource streamSource = new StreamSource(new FileInputStream(xmlFile)); 85 86 File streamResultFile = new File(USER_DIR + file); 87 StreamResult streamResult = new StreamResult(streamResultFile); 88 89 Transformer transformer = TransformerFactory.newInstance().newTransformer(domSource); 90 transformer.setOutputProperties(transformProperties); 91 transformer.transform(streamSource, streamResult); 92 } catch (SAXException | IOException | ParserConfigurationException 93 | TransformerException ex) { 94 failUnexpected(ex); 95 } 96 }); 97 } 98 }