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.File;
  26 import java.io.FileOutputStream;
  27 import java.io.IOException;



  28 import javax.xml.parsers.DocumentBuilder;
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.parsers.ParserConfigurationException;
  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.SAXTransformerFactory;
  38 import javax.xml.transform.sax.TransformerHandler;
  39 import javax.xml.transform.stream.StreamResult;
  40 import jaxp.library.JAXPFileBaseTest;
  41 import static jaxp.library.JAXPTestUtilities.CLASS_DIR;
  42 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  43 import static org.testng.Assert.assertTrue;
  44 import org.testng.annotations.Test;
  45 import org.w3c.dom.Node;
  46 import org.xml.sax.SAXException;
  47 import org.xml.sax.XMLReader;
  48 import org.xml.sax.helpers.XMLReaderFactory;
  49 
  50 /**
  51  * Test newTransformerHandler with a DOMSource and StreamResult set.
  52  */
  53 public class SAXTFactoryTest006 extends JAXPFileBaseTest {
  54     /**
  55      * Unit test newTransformerHandler with a DOMSource.
  56      * 
  57      * @throws SAXException If any parse errors occur.
  58      * @throws IOException if the file exists but is a directory rather than
  59      *         a regular file, does not exist but cannot be created, or cannot 
  60      *         be opened for any other reason.
  61      * @throws TransformerConfigurationException If for some reason the
  62      *         TransformerHandler can not be created.
  63      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  64      *         created which satisfies the configuration requested.
  65      */
  66     @Test
  67     public void testcase01() throws SAXException, IOException, 
  68             ParserConfigurationException, TransformerConfigurationException {
  69         String outputFile = CLASS_DIR + "saxtf006.out";
  70         String goldFile = GOLDEN_DIR + "saxtf006GF.out";
  71         String xsltFile = XML_DIR + "citiesinclude.xsl";
  72         String xmlFile = XML_DIR + "cities.xml";
  73 
  74         try (FileOutputStream fos = new FileOutputStream(outputFile)) {
  75             XMLReader reader = XMLReaderFactory.createXMLReader();
  76             SAXTransformerFactory saxTFactory
  77                     = (SAXTransformerFactory)TransformerFactory.newInstance();
  78 
  79             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  80             dbf.setNamespaceAware(true);
  81             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  82             Node node = (Node)docBuilder.parse(new File(xsltFile));
  83 
  84             DOMSource domSource = new DOMSource(node, "file:///" + XML_DIR);
  85             TransformerHandler handler =
  86                         saxTFactory.newTransformerHandler(domSource);
  87 
  88             Result result = new StreamResult(fos);
  89             handler.setResult(result);
  90             reader.setContentHandler(handler);
  91             reader.parse(xmlFile);












  92         }
  93         assertTrue(compareWithGold(goldFile, outputFile));
  94     }
  95 }
--- EOF ---