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 java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import javax.xml.parsers.DocumentBuilder;
  32 import javax.xml.parsers.DocumentBuilderFactory;
  33 import javax.xml.parsers.ParserConfigurationException;
  34 import javax.xml.transform.Result;
  35 import javax.xml.transform.TransformerConfigurationException;
  36 import javax.xml.transform.TransformerFactory;
  37 import javax.xml.transform.dom.DOMSource;


  38 import javax.xml.transform.sax.SAXTransformerFactory;
  39 import javax.xml.transform.sax.TransformerHandler;
  40 import javax.xml.transform.stream.StreamResult;


  41 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  42 import static jaxp.library.JAXPTestUtilities.failCleanup;
  43 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  44 import static org.testng.Assert.assertTrue;
  45 import org.testng.annotations.Test;
  46 import org.w3c.dom.Node;
  47 import org.xml.sax.SAXException;
  48 import org.xml.sax.XMLReader;
  49 import org.xml.sax.helpers.XMLReaderFactory;
  50 
  51 /**
  52  * Test newTransformerHandler with a DOMSource and StreamResult set.
  53  */
  54 public class SAXTFactoryTest006 extends TransformerTestConst{
  55     /**
  56      * Unit test newTransformerHandler with a DOMSource.









  57      */
  58     @Test
  59     public void testcase01() {

  60         String outputFile = CLASS_DIR + "saxtf006.out";
  61         String goldFile = GOLDEN_DIR + "saxtf006GF.out";
  62         String xsltFile = XML_DIR + "citiesinclude.xsl";
  63         String xmlFile = XML_DIR + "cities.xml";
  64 
  65         try (FileOutputStream fos = new FileOutputStream(outputFile)) {
  66             XMLReader reader = XMLReaderFactory.createXMLReader();
  67             SAXTransformerFactory saxTFactory
  68                     = (SAXTransformerFactory)TransformerFactory.newInstance();
  69 
  70             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  71             dbf.setNamespaceAware(true);
  72             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  73             Node node = (Node)docBuilder.parse(new File(xsltFile));
  74 
  75             DOMSource domSource = new DOMSource(node, "file:///" + XML_DIR);
  76             TransformerHandler handler =
  77                         saxTFactory.newTransformerHandler(domSource);
  78 
  79             Result result = new StreamResult(fos);
  80             handler.setResult(result);
  81             reader.setContentHandler(handler);
  82             reader.parse(xmlFile);
  83             assertTrue(compareWithGold(goldFile, outputFile));
  84         } catch (TransformerConfigurationException | ParserConfigurationException
  85                 | SAXException | IOException ex) {
  86             failUnexpected(ex);
  87         } finally {
  88             try {
  89                 Path outputPath = Paths.get(outputFile);
  90                 if(Files.exists(outputPath))
  91                     Files.delete(outputPath);
  92             } catch (IOException ex) {
  93                 failCleanup(ex, outputFile);
  94             }
  95         }

  96     }
  97 }
--- EOF ---