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.FileInputStream;
  27 import java.io.FileOutputStream;
  28 import java.io.IOException;
  29 import java.util.Properties;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 import javax.xml.transform.Transformer;
  33 import javax.xml.transform.TransformerException;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.dom.DOMSource;
  36 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  37 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  38 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  39 import javax.xml.transform.stream.StreamResult;
  40 import javax.xml.transform.stream.StreamSource;
  41 import jaxp.library.JAXPFileBaseTest;
  42 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  43 import static org.testng.Assert.assertTrue;
  44 import org.testng.annotations.Test;
  45 import org.xml.sax.SAXException;
  46 
  47 /**
  48  * Here Properties Object is populated with required properties.A transformer
  49  * is created using DOMSource. Using setOutputProperties(), Properties are set
  50  * for transformer. Then transform(StreamSource, StreamResult) is used for
  51  * transformation. This tests the setOutputProperties() method.
  52  */
  53 public class TransformerTest03 extends JAXPFileBaseTest {
  54     /**
  55      * Test for Transformer.setOutputProperties method.
  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 TransformerException If an unrecoverable error occurs during the
  62      *         course of the transformation.
  63      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  64      *         created which satisfies the configuration requested.
  65      */
  66     @Test
  67     public void testcase01() throws ParserConfigurationException, SAXException, 
  68             IOException, TransformerException {
  69         String outputFile = CLASS_DIR + "transformer03.out";
  70         String goldFile = GOLDEN_DIR + "transformer03GF.out";
  71         String xsltFile = XML_DIR + "cities.xsl";
  72         String xmlFile = XML_DIR + "cities.xml";
  73 
  74         try (FileInputStream fis = new FileInputStream(xmlFile);
  75                 FileOutputStream fos = new FileOutputStream(outputFile)) {
  76             Properties properties = new Properties();
  77             properties.put("method", "xml");
  78             properties.put("encoding", "UTF-8");
  79             properties.put("omit-xml-declaration", "yes");
  80             properties.put("{http://xml.apache.org/xslt}indent-amount", "0");
  81             properties.put("indent", "no");
  82             properties.put("standalone", "no");
  83             properties.put("version", "1.0");
  84             properties.put("media-type", "text/xml");
  85 
  86             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  87             dbf.setNamespaceAware(true);
  88             DOMSource domSource = new DOMSource(dbf.newDocumentBuilder().
  89                     parse(new File(xsltFile)));
  90 
  91             Transformer transformer = TransformerFactory.newInstance().
  92                     newTransformer(domSource);
  93             transformer.setOutputProperties(properties);
  94             transformer.transform(new StreamSource(fis), new StreamResult(fos));
  95         }
  96         assertTrue(compareWithGold(goldFile, outputFile));
  97     }
  98 }