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.IOException; 27 import java.nio.file.Files; 28 import java.nio.file.Path; 29 import java.nio.file.Paths; 30 import javax.xml.parsers.DocumentBuilder; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.ParserConfigurationException; 33 import javax.xml.transform.TransformerConfigurationException; 34 import javax.xml.transform.TransformerFactory; 35 import javax.xml.transform.dom.DOMSource; 36 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 37 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 38 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 39 import javax.xml.transform.sax.SAXTransformerFactory; 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.w3c.dom.Document; 46 import org.w3c.dom.Node; 47 import org.xml.sax.InputSource; 48 import org.xml.sax.SAXException; 49 import org.xml.sax.XMLFilter; 50 import org.xml.sax.XMLReader; 51 import org.xml.sax.helpers.XMLReaderFactory; 52 53 /** 54 * Test XMLFilter parse InputSource along with customized ContentHandler by 55 * using SAX parser as it's reader. 56 */ 57 public class SAXTFactoryTest011 { 58 /** 59 * Unit test for contentHandler setter/getter with parent. 60 */ 61 @Test 62 public void testcase01() { 63 String outputFile = CLASS_DIR + "saxtf011.out"; 64 String goldFile = GOLDEN_DIR + "saxtf011GF.out"; 65 String xsltFile = XML_DIR + "cities.xsl"; 66 String xmlFile = XML_DIR + "cities.xml"; 67 68 try { 69 // The transformer will use a SAX parser as it's reader. 70 XMLReader reader = XMLReaderFactory.createXMLReader(); 71 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 72 dbf.setNamespaceAware(true); 73 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 74 Document document = docBuilder.parse(new File(xsltFile)); 75 Node node = (Node)document; 76 DOMSource domSource= new DOMSource(node); 77 78 SAXTransformerFactory saxTFactory 79 = (SAXTransformerFactory)TransformerFactory.newInstance(); 80 XMLFilter filter = saxTFactory.newXMLFilter(domSource); 81 82 filter.setParent(reader); 83 filter.setContentHandler(new MyContentHandler(outputFile)); 84 85 // Now, when you call transformer.parse, it will set itself as 86 // the content handler for the parser object (it's "parent"), and 87 // will then call the parse method on the parser. 88 filter.parse(new InputSource(xmlFile)); 89 assertTrue(compareWithGold(goldFile, outputFile)); 90 } catch (SAXException | IOException | TransformerConfigurationException 91 | ParserConfigurationException ex) { 92 failUnexpected(ex); 93 } finally { 94 try { 95 Path outputPath = Paths.get(outputFile); 96 if(Files.exists(outputPath)) 97 Files.delete(outputPath); 98 } catch (IOException ex) { 99 failCleanup(ex, outputFile); 100 } 101 } 102 } 103 }