1 /* 2 * Copyright (c) 2003, 2015, 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 24 package javax.xml.transform.ptests; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FileOutputStream; 29 import javax.xml.parsers.DocumentBuilder; 30 import javax.xml.parsers.DocumentBuilderFactory; 31 import javax.xml.transform.Result; 32 import javax.xml.transform.TransformerConfigurationException; 33 import javax.xml.transform.TransformerFactory; 34 import javax.xml.transform.dom.DOMSource; 35 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 36 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 37 import javax.xml.transform.sax.SAXSource; 38 import javax.xml.transform.sax.SAXTransformerFactory; 39 import javax.xml.transform.sax.TemplatesHandler; 40 import javax.xml.transform.sax.TransformerHandler; 41 import javax.xml.transform.stream.StreamResult; 42 import javax.xml.transform.stream.StreamSource; 43 import jaxp.library.JAXPFileBaseTest; 44 import static jaxp.library.JAXPTestUtilities.USER_DIR; 45 import static jaxp.library.JAXPTestUtilities.compareWithGold; 46 import static org.testng.Assert.assertTrue; 47 import org.testng.annotations.Test; 48 import org.w3c.dom.Document; 49 import org.w3c.dom.Node; 50 import org.xml.sax.InputSource; 51 import org.xml.sax.XMLFilter; 52 import org.xml.sax.XMLReader; 53 import org.xml.sax.helpers.XMLReaderFactory; 54 55 /** 56 * Test newTransformerhandler() method which takes StreamSource as argument can 57 * be set to XMLReader. 58 */ 59 public class SAXTFactoryTest extends JAXPFileBaseTest { 60 /** 61 * Test style-sheet file. 62 */ 63 private static final String XSLT_FILE = XML_DIR + "cities.xsl"; 64 65 /** 66 * Test style-sheet file. 67 */ 68 private static final String XSLT_INCL_FILE = XML_DIR + "citiesinclude.xsl"; 69 70 /** 71 * Test XML file. 72 */ 73 private static final String XML_FILE = XML_DIR + "cities.xml"; 74 75 /** 76 * SAXTFactory.newTransformerhandler() method which takes SAXSource as 77 * argument can be set to XMLReader. SAXSource has input XML file as its 78 * input source. XMLReader has a transformer handler which write out the 79 * result to output file. Test verifies output file is same as golden file. 80 * 81 * @throws Exception If any errors occur. 82 */ 83 @Test 84 public void testcase01() throws Exception { 85 String outputFile = USER_DIR + "saxtf001.out"; 86 String goldFile = GOLDEN_DIR + "saxtf001GF.out"; 87 88 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 89 XMLReader reader = XMLReaderFactory.createXMLReader(); 90 SAXTransformerFactory saxTFactory 91 = (SAXTransformerFactory) TransformerFactory.newInstance(); 92 TransformerHandler handler = saxTFactory.newTransformerHandler(new StreamSource(XSLT_FILE)); 93 Result result = new StreamResult(fos); 94 handler.setResult(result); 95 reader.setContentHandler(handler); 96 reader.parse(XML_FILE); 97 } 98 assertTrue(compareWithGold(goldFile, outputFile)); 99 } 100 101 /** 102 * SAXTFactory.newTransformerhandler() method which takes SAXSource as 103 * argument can be set to XMLReader. SAXSource has input XML file as its 104 * input source. XMLReader has a content handler which write out the result 105 * to output file. Test verifies output file is same as golden file. 106 * 107 * @throws Exception If any errors occur. 108 */ 109 @Test 110 public void testcase02() throws Exception { 111 String outputFile = USER_DIR + "saxtf002.out"; 112 String goldFile = GOLDEN_DIR + "saxtf002GF.out"; 113 114 try (FileOutputStream fos = new FileOutputStream(outputFile); 115 FileInputStream fis = new FileInputStream(XSLT_FILE)) { 116 XMLReader reader = XMLReaderFactory.createXMLReader(); 117 SAXTransformerFactory saxTFactory 118 = (SAXTransformerFactory) TransformerFactory.newInstance(); 119 SAXSource ss = new SAXSource(); 120 ss.setInputSource(new InputSource(fis)); 121 122 TransformerHandler handler = saxTFactory.newTransformerHandler(ss); 123 Result result = new StreamResult(fos); 124 handler.setResult(result); 125 reader.setContentHandler(handler); 126 reader.parse(XML_FILE); 127 } 128 assertTrue(compareWithGold(goldFile, outputFile)); 129 } 130 131 /** 132 * Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is 133 * namespace awareness, DocumentBuilder parse xslt file as DOMSource. 134 * 135 * @throws Exception If any errors occur. 136 */ 137 @Test 138 public void testcase03() throws Exception { 139 String outputFile = USER_DIR + "saxtf003.out"; 140 String goldFile = GOLDEN_DIR + "saxtf003GF.out"; 141 142 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 143 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 144 dbf.setNamespaceAware(true); 145 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 146 Document document = docBuilder.parse(new File(XSLT_FILE)); 147 Node node = (Node)document; 148 DOMSource domSource= new DOMSource(node); 149 150 XMLReader reader = XMLReaderFactory.createXMLReader(); 151 SAXTransformerFactory saxTFactory 152 = (SAXTransformerFactory)TransformerFactory.newInstance(); 153 TransformerHandler handler = 154 saxTFactory.newTransformerHandler(domSource); 155 Result result = new StreamResult(fos); 156 handler.setResult(result); 157 reader.setContentHandler(handler); 158 reader.parse(XML_FILE); 159 } 160 assertTrue(compareWithGold(goldFile, outputFile)); 161 } 162 163 /** 164 * Negative test for newTransformerHandler when relative URI is in XML file. 165 * 166 * @throws Exception If any errors occur. 167 */ 168 @Test(expectedExceptions = TransformerConfigurationException.class) 169 public void transformerHandlerTest04() throws Exception { 170 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 171 dbf.setNamespaceAware(true); 172 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 173 Document document = docBuilder.parse(new File(XSLT_INCL_FILE)); 174 DOMSource domSource= new DOMSource(document); 175 SAXTransformerFactory saxTFactory 176 = (SAXTransformerFactory)TransformerFactory.newInstance(); 177 saxTFactory.newTransformerHandler(domSource); 178 } 179 180 /** 181 * Unit test for XMLReader parsing when relative URI is used in xsl file and 182 * SystemId was set. 183 * 184 * @throws Exception If any errors occur. 185 */ 186 @Test 187 public void testcase05() throws Exception { 188 String outputFile = USER_DIR + "saxtf005.out"; 189 String goldFile = GOLDEN_DIR + "saxtf005GF.out"; 190 191 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 192 XMLReader reader = XMLReaderFactory.createXMLReader(); 193 SAXTransformerFactory saxTFactory 194 = (SAXTransformerFactory)TransformerFactory.newInstance(); 195 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 196 dbf.setNamespaceAware(true); 197 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 198 Document document = docBuilder.parse(new File(XSLT_INCL_FILE)); 199 Node node = (Node)document; 200 DOMSource domSource= new DOMSource(node); 201 202 domSource.setSystemId("file:///" + XML_DIR); 203 204 TransformerHandler handler = 205 saxTFactory.newTransformerHandler(domSource); 206 Result result = new StreamResult(fos); 207 208 handler.setResult(result); 209 reader.setContentHandler(handler); 210 reader.parse(XML_FILE); 211 } 212 assertTrue(compareWithGold(goldFile, outputFile)); 213 } 214 215 /** 216 * Unit test newTransformerHandler with a DOMSource. 217 * 218 * @throws Exception If any errors occur. 219 */ 220 @Test 221 public void testcase06() throws Exception { 222 String outputFile = USER_DIR + "saxtf006.out"; 223 String goldFile = GOLDEN_DIR + "saxtf006GF.out"; 224 225 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 226 XMLReader reader = XMLReaderFactory.createXMLReader(); 227 SAXTransformerFactory saxTFactory 228 = (SAXTransformerFactory)TransformerFactory.newInstance(); 229 230 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 231 dbf.setNamespaceAware(true); 232 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 233 Node node = (Node)docBuilder.parse(new File(XSLT_INCL_FILE)); 234 235 DOMSource domSource = new DOMSource(node, "file:///" + XML_DIR); 236 TransformerHandler handler = 237 saxTFactory.newTransformerHandler(domSource); 238 239 Result result = new StreamResult(fos); 240 handler.setResult(result); 241 reader.setContentHandler(handler); 242 reader.parse(XML_FILE); 243 } 244 assertTrue(compareWithGold(goldFile, outputFile)); 245 } 246 247 /** 248 * Test newTransformerHandler with a Template Handler. 249 * 250 * @throws Exception If any errors occur. 251 */ 252 public void testcase08() throws Exception { 253 String outputFile = USER_DIR + "saxtf008.out"; 254 String goldFile = GOLDEN_DIR + "saxtf008GF.out"; 255 256 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 257 XMLReader reader = XMLReaderFactory.createXMLReader(); 258 SAXTransformerFactory saxTFactory 259 = (SAXTransformerFactory)TransformerFactory.newInstance(); 260 261 TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); 262 reader.setContentHandler(thandler); 263 reader.parse(XSLT_FILE); 264 TransformerHandler tfhandler 265 = saxTFactory.newTransformerHandler(thandler.getTemplates()); 266 267 Result result = new StreamResult(fos); 268 tfhandler.setResult(result); 269 270 reader.setContentHandler(tfhandler); 271 reader.parse(XML_FILE); 272 } 273 assertTrue(compareWithGold(goldFile, outputFile)); 274 } 275 276 /** 277 * Test newTransformerHandler with a Template Handler along with a relative 278 * URI in the style-sheet file. 279 * 280 * @throws Exception If any errors occur. 281 */ 282 @Test 283 public void testcase09() throws Exception { 284 String outputFile = USER_DIR + "saxtf009.out"; 285 String goldFile = GOLDEN_DIR + "saxtf009GF.out"; 286 287 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 288 XMLReader reader = XMLReaderFactory.createXMLReader(); 289 SAXTransformerFactory saxTFactory 290 = (SAXTransformerFactory)TransformerFactory.newInstance(); 291 292 TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); 293 thandler.setSystemId("file:///" + XML_DIR); 294 reader.setContentHandler(thandler); 295 reader.parse(XSLT_INCL_FILE); 296 TransformerHandler tfhandler= 297 saxTFactory.newTransformerHandler(thandler.getTemplates()); 298 Result result = new StreamResult(fos); 299 tfhandler.setResult(result); 300 reader.setContentHandler(tfhandler); 301 reader.parse(XML_FILE); 302 } 303 assertTrue(compareWithGold(goldFile, outputFile)); 304 } 305 306 /** 307 * Unit test for contentHandler setter/getter along reader as handler's 308 * parent. 309 * 310 * @throws Exception If any errors occur. 311 */ 312 @Test 313 public void testcase10() throws Exception { 314 String outputFile = USER_DIR + "saxtf010.out"; 315 String goldFile = GOLDEN_DIR + "saxtf010GF.out"; 316 // The transformer will use a SAX parser as it's reader. 317 XMLReader reader = XMLReaderFactory.createXMLReader(); 318 SAXTransformerFactory saxTFactory 319 = (SAXTransformerFactory)TransformerFactory.newInstance(); 320 XMLFilter filter = 321 saxTFactory.newXMLFilter(new StreamSource(XSLT_FILE)); 322 filter.setParent(reader); 323 filter.setContentHandler(new MyContentHandler(outputFile)); 324 325 // Now, when you call transformer.parse, it will set itself as 326 // the content handler for the parser object (it's "parent"), and 327 // will then call the parse method on the parser. 328 filter.parse(new InputSource(XML_FILE)); 329 assertTrue(compareWithGold(goldFile, outputFile)); 330 } 331 332 /** 333 * Unit test for contentHandler setter/getter with parent. 334 * 335 * @throws Exception If any errors occur. 336 */ 337 @Test 338 public void testcase11() throws Exception { 339 String outputFile = USER_DIR + "saxtf011.out"; 340 String goldFile = GOLDEN_DIR + "saxtf011GF.out"; 341 // The transformer will use a SAX parser as it's reader. 342 XMLReader reader = XMLReaderFactory.createXMLReader(); 343 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 344 dbf.setNamespaceAware(true); 345 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 346 Document document = docBuilder.parse(new File(XSLT_FILE)); 347 Node node = (Node)document; 348 DOMSource domSource= new DOMSource(node); 349 350 SAXTransformerFactory saxTFactory 351 = (SAXTransformerFactory)TransformerFactory.newInstance(); 352 XMLFilter filter = saxTFactory.newXMLFilter(domSource); 353 354 filter.setParent(reader); 355 filter.setContentHandler(new MyContentHandler(outputFile)); 356 357 // Now, when you call transformer.parse, it will set itself as 358 // the content handler for the parser object (it's "parent"), and 359 // will then call the parse method on the parser. 360 filter.parse(new InputSource(XML_FILE)); 361 assertTrue(compareWithGold(goldFile, outputFile)); 362 } 363 364 /** 365 * Unit test for contentHandler setter/getter. 366 * 367 * @throws Exception If any errors occur. 368 */ 369 @Test 370 public void testcase12() throws Exception { 371 String outputFile = USER_DIR + "saxtf012.out"; 372 String goldFile = GOLDEN_DIR + "saxtf012GF.out"; 373 // The transformer will use a SAX parser as it's reader. 374 XMLReader reader = XMLReaderFactory.createXMLReader(); 375 376 InputSource is = new InputSource(new FileInputStream(XSLT_FILE)); 377 SAXSource saxSource = new SAXSource(); 378 saxSource.setInputSource(is); 379 380 SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); 381 XMLFilter filter = saxTFactory.newXMLFilter(saxSource); 382 383 filter.setParent(reader); 384 filter.setContentHandler(new MyContentHandler(outputFile)); 385 386 // Now, when you call transformer.parse, it will set itself as 387 // the content handler for the parser object (it's "parent"), and 388 // will then call the parse method on the parser. 389 filter.parse(new InputSource(XML_FILE)); 390 assertTrue(compareWithGold(goldFile, outputFile)); 391 } 392 393 /** 394 * Unit test for TemplatesHandler setter/getter. 395 * 396 * @throws Exception If any errors occur. 397 */ 398 @Test 399 public void testcase13() throws Exception { 400 String outputFile = USER_DIR + "saxtf013.out"; 401 String goldFile = GOLDEN_DIR + "saxtf013GF.out"; 402 try(FileInputStream fis = new FileInputStream(XML_FILE)) { 403 // The transformer will use a SAX parser as it's reader. 404 XMLReader reader = XMLReaderFactory.createXMLReader(); 405 406 SAXTransformerFactory saxTFactory 407 = (SAXTransformerFactory) TransformerFactory.newInstance(); 408 TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); 409 // I have put this as it was complaining about systemid 410 thandler.setSystemId("file:///" + USER_DIR); 411 412 reader.setContentHandler(thandler); 413 reader.parse(XSLT_FILE); 414 XMLFilter filter 415 = saxTFactory.newXMLFilter(thandler.getTemplates()); 416 filter.setParent(reader); 417 418 filter.setContentHandler(new MyContentHandler(outputFile)); 419 filter.parse(new InputSource(fis)); 420 } 421 assertTrue(compareWithGold(goldFile, outputFile)); 422 } 423 }