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.FileOutputStream;
  26 import java.io.IOException;
  27 import javax.xml.transform.Result;
  28 import javax.xml.transform.TransformerConfigurationException;
  29 import javax.xml.transform.TransformerFactory;
  30 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  31 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  32 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  33 import javax.xml.transform.sax.SAXTransformerFactory;
  34 import javax.xml.transform.sax.TemplatesHandler;
  35 import javax.xml.transform.sax.TransformerHandler;
  36 import javax.xml.transform.stream.StreamResult;
  37 import jaxp.library.JAXPFileBaseTest;
  38 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  39 import static org.testng.Assert.assertTrue;
  40 import org.testng.annotations.Test;
  41 import org.xml.sax.SAXException;
  42 import org.xml.sax.XMLReader;
  43 import org.xml.sax.helpers.XMLReaderFactory;
  44 
  45 /**
  46  * Test newTransformerHandler with a Template Handler along with a relative URI
  47  * in the style-sheet file.
  48  */
  49 public class SAXTFactoryTest009 extends JAXPFileBaseTest {
  50     /**
  51      * Test newTransformerHandler with a Template Handler along with a relative
  52      * URI in the style-sheet file.
  53      * 
  54      * @throws SAXException If any parse errors occur.
  55      * @throws IOException if the file exists but is a directory rather than
  56      *         a regular file, does not exist but cannot be created, or cannot 
  57      *         be opened for any other reason.
  58      * @throws TransformerConfigurationException If for some reason the
  59      *         TransformerHandler can not be created.
  60      */
  61     @Test
  62     public void testcase01() throws IOException, SAXException, 
  63             TransformerConfigurationException {
  64         String outputFile = CLASS_DIR + "saxtf009.out";
  65         String goldFile = GOLDEN_DIR + "saxtf009GF.out";
  66         String xsltFile = XML_DIR + "citiesinclude.xsl";
  67         String xmlFile = XML_DIR + "cities.xml";
  68 
  69         try (FileOutputStream fos = new FileOutputStream(outputFile)) {
  70             XMLReader reader = XMLReaderFactory.createXMLReader();
  71             SAXTransformerFactory saxTFactory
  72                     = (SAXTransformerFactory)TransformerFactory.newInstance();
  73 
  74             TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
  75             thandler.setSystemId("file:///" + XML_DIR);
  76             reader.setContentHandler(thandler);
  77             reader.parse(xsltFile);
  78             TransformerHandler tfhandler=
  79                 saxTFactory.newTransformerHandler(thandler.getTemplates());
  80             Result result = new StreamResult(fos);
  81             tfhandler.setResult(result);
  82             reader.setContentHandler(tfhandler);
  83             reader.parse(xmlFile);
  84         }
  85         assertTrue(compareWithGold(goldFile, outputFile));
  86   }
  87 }