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