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.FileOutputStream; 28 import java.io.IOException; 29 import java.nio.file.Files; 30 import java.nio.file.Path; 31 import java.nio.file.Paths; 32 import javax.xml.parsers.DocumentBuilder; 33 import javax.xml.parsers.DocumentBuilderFactory; 34 import javax.xml.parsers.ParserConfigurationException; 35 import javax.xml.transform.Transformer; 36 import javax.xml.transform.TransformerException; 37 import javax.xml.transform.TransformerFactory; 38 import javax.xml.transform.dom.DOMSource; 39 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 40 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 41 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 42 import javax.xml.transform.stream.StreamResult; 43 import javax.xml.transform.stream.StreamSource; 44 import static jaxp.library.JAXPTestUtilities.compareWithGold; 45 import static jaxp.library.JAXPTestUtilities.failCleanup; 46 import static jaxp.library.JAXPTestUtilities.failUnexpected; 47 import static org.testng.Assert.assertTrue; 48 import org.testng.annotations.Test; 49 import org.w3c.dom.Document; 50 import org.xml.sax.SAXException; 51 52 53 /** 54 * Here a transformer is created using DOMSource. Some specific output property 55 * is set on transformer. Then transform(StreamSource, StreamResult) is tested. 56 */ 57 public class TransformerTest02 { 58 /** 59 * Unit test for transform(StreamSource, StreamResult). 60 */ 61 @Test 62 public void testcase01() { 63 String outputFile = CLASS_DIR + "transformer02.out"; 64 String goldFile = GOLDEN_DIR + "transformer02GF.out"; 65 String xsltFile = XML_DIR + "cities.xsl"; 66 String xmlFile = XML_DIR + "cities.xml"; 67 68 try (FileInputStream fis = new FileInputStream(xmlFile); 69 FileOutputStream fos = new FileOutputStream(outputFile)) { 70 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 71 dbf.setNamespaceAware(true); 72 DocumentBuilder db = dbf.newDocumentBuilder(); 73 Document document = db.parse(new File(xsltFile)); 74 DOMSource domSource = new DOMSource(document); 75 76 Transformer transformer = TransformerFactory.newInstance(). 77 newTransformer(domSource); 78 StreamSource streamSource = new StreamSource(fis); 79 StreamResult streamResult = new StreamResult(fos); 80 81 transformer.setOutputProperty("indent", "no"); 82 transformer.transform( streamSource, streamResult); 83 assertTrue(compareWithGold(goldFile, outputFile)); 84 } catch (IOException | IllegalArgumentException 85 | ParserConfigurationException | TransformerException 86 | SAXException ex) { 87 failUnexpected(ex); 88 } finally { 89 try { 90 Path outputPath = Paths.get(outputFile); 91 if(Files.exists(outputPath)) 92 Files.delete(outputPath); 93 } catch (IOException ex) { 94 failCleanup(ex, outputFile); 95 } 96 } 97 } 98 }