1 /*
   2  * Copyright (c) 2003, 2016, 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 static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  26 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  27 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  28 
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.IOException;
  32 import java.util.Arrays;
  33 import java.util.Properties;
  34 
  35 import javax.xml.parsers.DocumentBuilder;
  36 import javax.xml.parsers.DocumentBuilderFactory;
  37 import javax.xml.parsers.ParserConfigurationException;
  38 import javax.xml.transform.Transformer;
  39 import javax.xml.transform.TransformerException;
  40 import javax.xml.transform.TransformerFactory;
  41 import javax.xml.transform.dom.DOMSource;
  42 import javax.xml.transform.stream.StreamResult;
  43 import javax.xml.transform.stream.StreamSource;
  44 
  45 import org.testng.annotations.Listeners;
  46 import org.testng.annotations.Test;
  47 import org.w3c.dom.Document;
  48 import org.xml.sax.SAXException;
  49 
  50 /**
  51  * Test a StreamResult using a file name that contains URL characters that need
  52  * to be encoded.
  53  */
  54 /*
  55  * @test
  56  * @library /javax/xml/jaxp/libs
  57  * @run testng/othervm -DrunSecMngr=true javax.xml.transform.ptests.StreamResultTest
  58  * @run testng/othervm javax.xml.transform.ptests.StreamResultTest
  59  */
  60 @Listeners({jaxp.library.FilePolicy.class})
  61 public class StreamResultTest {
  62     /**
  63      * Unit test for StreamResult.
  64      */
  65     @Test
  66     public void testcase01() {
  67         // Set Transformer properties
  68         Properties transformProperties = new Properties();
  69         transformProperties.put("method", "xml");
  70         transformProperties.put("encoding", "UTF-8");
  71         transformProperties.put("omit-xml-declaration", "yes");
  72         transformProperties.put("{http://xml.apache.org/xslt}indent-amount", "0");
  73         transformProperties.put("indent", "no");
  74         transformProperties.put("standalone", "no");
  75         transformProperties.put("version", "1.0");
  76         transformProperties.put("media-type", "text/xml");
  77 
  78         String[] fileNames = {
  79             "StreamResult01.out",
  80             "StreamResult 02.out",
  81             "StreamResult#03.out"
  82         };
  83 
  84         String xslFile = XML_DIR + "cities.xsl";
  85         String xmlFile = XML_DIR + "cities.xml";
  86 
  87         Arrays.stream(fileNames).forEach(file -> {
  88             try {
  89                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  90                 dbf.setNamespaceAware(true);
  91                 DocumentBuilder db = dbf.newDocumentBuilder();
  92                 Document document = db.parse(new File(xslFile));
  93                 DOMSource domSource = new DOMSource(document);
  94                 StreamSource streamSource = new StreamSource(new FileInputStream(xmlFile));
  95 
  96                 File streamResultFile = new File(USER_DIR + file);
  97                 StreamResult streamResult = new StreamResult(streamResultFile);
  98 
  99                 Transformer transformer = TransformerFactory.newInstance().newTransformer(domSource);
 100                 transformer.setOutputProperties(transformProperties);
 101                 transformer.transform(streamSource, streamResult);
 102             } catch (SAXException | IOException | ParserConfigurationException
 103                     | TransformerException ex) {
 104                 failUnexpected(ex);
 105             }
 106         });
 107     }
 108 }