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.FileOutputStream; 26 import java.io.IOException; 27 import java.nio.file.Files; 28 import java.nio.file.Path; 29 import java.nio.file.Paths; 30 import javax.xml.transform.Result; 31 import javax.xml.transform.TransformerConfigurationException; 32 import javax.xml.transform.TransformerFactory; 33 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 34 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 35 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 36 import javax.xml.transform.sax.SAXTransformerFactory; 37 import javax.xml.transform.sax.TemplatesHandler; 38 import javax.xml.transform.sax.TransformerHandler; 39 import javax.xml.transform.stream.StreamResult; 40 import static jaxp.library.JAXPTestUtilities.compareWithGold; 41 import static jaxp.library.JAXPTestUtilities.failCleanup; 42 import static jaxp.library.JAXPTestUtilities.failUnexpected; 43 import static org.testng.Assert.assertTrue; 44 import org.testng.annotations.Test; 45 import org.xml.sax.SAXException; 46 import org.xml.sax.XMLReader; 47 import org.xml.sax.helpers.XMLReaderFactory; 48 49 /** 50 * Test newTransformerHandler with a Template Handler along with a relative URI 51 * in the xslt file. 52 */ 53 public class SAXTFactoryTest009 { 54 /** 55 * Test newTransformerHandler with a Template Handler along with a relative 56 * URI in the xslt file. 57 */ 58 @Test 59 public void testcase01() { 60 String outputFile = CLASS_DIR + "saxtf009.out"; 61 String goldFile = GOLDEN_DIR + "saxtf009GF.out"; 62 String xsltFile = XML_DIR + "citiesinclude.xsl"; 63 String xmlFile = XML_DIR + "cities.xml"; 64 65 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 66 XMLReader reader = XMLReaderFactory.createXMLReader(); 67 SAXTransformerFactory saxTFactory 68 = (SAXTransformerFactory)TransformerFactory.newInstance(); 69 70 TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); 71 thandler.setSystemId("file:///" + XML_DIR); 72 reader.setContentHandler(thandler); 73 reader.parse(xsltFile); 74 TransformerHandler tfhandler= 75 saxTFactory.newTransformerHandler(thandler.getTemplates()); 76 Result result = new StreamResult(fos); 77 tfhandler.setResult(result); 78 reader.setContentHandler(tfhandler); 79 reader.parse(xmlFile); 80 assertTrue(compareWithGold(goldFile, outputFile)); 81 } catch (SAXException | IOException | TransformerConfigurationException ex) { 82 failUnexpected(ex); 83 } finally { 84 try { 85 Path outputPath = Paths.get(outputFile); 86 if(Files.exists(outputPath)) 87 Files.delete(outputPath); 88 } catch (IOException ex) { 89 failCleanup(ex, outputFile); 90 } 91 } 92 } 93 }