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