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.FileInputStream;
  26 import java.io.IOException;
  27 import javax.xml.transform.TransformerConfigurationException;
  28 import javax.xml.transform.TransformerFactory;
  29 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  30 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  31 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  32 import javax.xml.transform.sax.SAXTransformerFactory;
  33 import javax.xml.transform.sax.TemplatesHandler;
  34 import jaxp.library.JAXPFileBaseTest;
  35 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  36 import static org.testng.Assert.assertTrue;
  37 import org.testng.annotations.Test;
  38 import org.xml.sax.InputSource;
  39 import org.xml.sax.SAXException;
  40 import org.xml.sax.XMLFilter;
  41 import org.xml.sax.XMLReader;
  42 import org.xml.sax.helpers.XMLReaderFactory;
  43 
  44 /**
  45  * Test XMLFilter parse InputSource along with TemplatesHandler.
  46  */
  47 public class SAXTFactoryTest013 extends JAXPFileBaseTest {
  48     /**
  49      * Unit test for TemplatesHandler setter/getter.
  50      * 
  51      * @throws SAXException If any parse errors occur.
  52      * @throws IOException if the file exists but is a directory rather than
  53      *         a regular file, does not exist but cannot be created, or cannot 
  54      *         be opened for any other reason.
  55      * @throws TransformerConfigurationException If for some reason the
  56      *         TransformerHandler can not be created.
  57      */
  58     @Test
  59     public void testcase01() throws IOException, SAXException, 
  60             TransformerConfigurationException {
  61         String outputFile = CLASS_DIR + "saxtf013.out";
  62         String goldFile = GOLDEN_DIR + "saxtf013GF.out";
  63         String xsltFile = XML_DIR + "cities.xsl";
  64         String xmlFile = XML_DIR + "cities.xml";
  65         try(FileInputStream fis = new FileInputStream(xmlFile)) {
  66             // The transformer will use a SAX parser as it's reader.
  67             XMLReader reader = XMLReaderFactory.createXMLReader();
  68 
  69             SAXTransformerFactory saxTFactory
  70                     = (SAXTransformerFactory) TransformerFactory.newInstance();
  71             TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
  72             // I have put this as it was complaining about systemid
  73             thandler.setSystemId("file:///" + CLASS_DIR);
  74 
  75             reader.setContentHandler(thandler);
  76             reader.parse(xsltFile);
  77             XMLFilter filter
  78                     = saxTFactory.newXMLFilter(thandler.getTemplates());
  79             filter.setParent(reader);
  80 
  81             filter.setContentHandler(new MyContentHandler(outputFile));
  82             filter.parse(new InputSource(fis));
  83         }
  84         assertTrue(compareWithGold(goldFile, outputFile));
  85     }
  86 }