1 /* 2 * Copyright (c) 2003, 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 package javax.xml.transform.ptests; 24 25 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 26 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 27 import static jaxp.library.JAXPTestUtilities.USER_DIR; 28 import static jaxp.library.JAXPTestUtilities.compareWithGold; 29 import static org.testng.Assert.assertTrue; 30 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.FileOutputStream; 34 import java.util.Properties; 35 36 import javax.xml.parsers.DocumentBuilderFactory; 37 import javax.xml.transform.Transformer; 38 import javax.xml.transform.TransformerFactory; 39 import javax.xml.transform.dom.DOMSource; 40 import javax.xml.transform.stream.StreamResult; 41 import javax.xml.transform.stream.StreamSource; 42 43 import org.testng.annotations.Listeners; 44 import org.testng.annotations.Test; 45 46 /** 47 * Here Properties Object is populated with required properties.A transformer 48 * is created using DOMSource. Using setOutputProperties(), Properties are set 49 * for transformer. Then transform(StreamSource, StreamResult) is used for 50 * transformation. This tests the setOutputProperties() method. 51 */ 52 /* 53 * @test 54 * @library /javax/xml/jaxp/libs 55 * @run testng/othervm -DrunSecMngr=true javax.xml.transform.ptests.TransformerTest03 56 * @run testng/othervm javax.xml.transform.ptests.TransformerTest03 57 */ 58 @Listeners({jaxp.library.FilePolicy.class}) 59 public class TransformerTest03 { 60 /** 61 * Test for Transformer.setOutputProperties method. 62 * 63 * @throws Exception If any errors occur. 64 */ 65 @Test 66 public void testcase01() throws Exception { 67 String outputFile = USER_DIR + "transformer03.out"; 68 String goldFile = GOLDEN_DIR + "transformer03GF.out"; 69 String xsltFile = XML_DIR + "cities.xsl"; 70 String xmlFile = XML_DIR + "cities.xml"; 71 72 try (FileInputStream fis = new FileInputStream(xmlFile); 73 FileOutputStream fos = new FileOutputStream(outputFile)) { 74 Properties properties = new Properties(); 75 properties.put("method", "xml"); 76 properties.put("encoding", "UTF-8"); 77 properties.put("omit-xml-declaration", "yes"); 78 properties.put("{http://xml.apache.org/xslt}indent-amount", "0"); 79 properties.put("indent", "no"); 80 properties.put("standalone", "no"); 81 properties.put("version", "1.0"); 82 properties.put("media-type", "text/xml"); 83 84 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 85 dbf.setNamespaceAware(true); 86 DOMSource domSource = new DOMSource(dbf.newDocumentBuilder(). 87 parse(new File(xsltFile))); 88 89 Transformer transformer = TransformerFactory.newInstance(). 90 newTransformer(domSource); 91 transformer.setOutputProperties(properties); 92 transformer.transform(new StreamSource(fis), new StreamResult(fos)); 93 } 94 assertTrue(compareWithGold(goldFile, outputFile)); 95 } 96 } 97 98