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