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.IOException; |
|
26 import java.nio.file.Files; |
|
27 import java.nio.file.Path; |
|
28 import java.nio.file.Paths; |
|
29 import javax.xml.transform.TransformerConfigurationException; |
|
30 import javax.xml.transform.TransformerFactory; |
|
31 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; |
|
32 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; |
|
33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; |
|
34 import javax.xml.transform.sax.SAXTransformerFactory; |
|
35 import javax.xml.transform.stream.StreamSource; |
|
36 import static jaxp.library.JAXPTestUtilities.compareWithGold; |
|
37 import static jaxp.library.JAXPTestUtilities.failCleanup; |
|
38 import static jaxp.library.JAXPTestUtilities.failUnexpected; |
|
39 import static org.testng.Assert.assertTrue; |
|
40 import org.testng.annotations.Test; |
|
41 import org.xml.sax.InputSource; |
|
42 import org.xml.sax.SAXException; |
|
43 import org.xml.sax.XMLFilter; |
|
44 import org.xml.sax.XMLReader; |
|
45 import org.xml.sax.helpers.XMLReaderFactory; |
|
46 |
|
47 /** |
|
48 * Test XMLFilter parse InputSource along with customized ContentHandler. |
|
49 */ |
|
50 public class SAXTFactoryTest010 { |
|
51 /** |
|
52 * Unit test for contentHandler setter/getter along reader as handler's |
|
53 * parent. |
|
54 */ |
|
55 @Test |
|
56 public void testcase01() { |
|
57 String outputFile = CLASS_DIR + "saxtf010.out"; |
|
58 String goldFile = GOLDEN_DIR + "saxtf010GF.out"; |
|
59 String xsltFile = XML_DIR + "cities.xsl"; |
|
60 String xmlFile = XML_DIR + "cities.xml"; |
|
61 |
|
62 try { |
|
63 // The transformer will use a SAX parser as it's reader. |
|
64 XMLReader reader = XMLReaderFactory.createXMLReader(); |
|
65 |
|
66 SAXTransformerFactory saxTFactory |
|
67 = (SAXTransformerFactory)TransformerFactory.newInstance(); |
|
68 XMLFilter filter = |
|
69 saxTFactory.newXMLFilter(new StreamSource(xsltFile)); |
|
70 |
|
71 filter.setParent(reader); |
|
72 filter.setContentHandler(new MyContentHandler(outputFile)); |
|
73 |
|
74 // Now, when you call transformer.parse, it will set itself as |
|
75 // the content handler for the parser object (it's "parent"), and |
|
76 // will then call the parse method on the parser. |
|
77 filter.parse(new InputSource(xmlFile)); |
|
78 assertTrue(compareWithGold(goldFile, outputFile)); |
|
79 } catch (SAXException | IOException | TransformerConfigurationException ex) { |
|
80 failUnexpected(ex); |
|
81 } finally { |
|
82 try { |
|
83 Path outputPath = Paths.get(outputFile); |
|
84 if(Files.exists(outputPath)) |
|
85 Files.delete(outputPath); |
|
86 } catch (IOException ex) { |
|
87 failCleanup(ex, outputFile); |
|
88 } |
|
89 } |
|
90 } |
|
91 } |
|