< prev index next >

test/javax/xml/jaxp/functional/javax/xml/transform/ptests/TransformerTest03.java

Print this page


   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.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Properties;
  33 import javax.xml.parsers.DocumentBuilder;
  34 import javax.xml.parsers.DocumentBuilderFactory;
  35 import javax.xml.parsers.ParserConfigurationException;
  36 import javax.xml.transform.Transformer;
  37 import javax.xml.transform.TransformerException;
  38 import javax.xml.transform.TransformerFactory;
  39 import javax.xml.transform.dom.DOMSource;
  40 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  41 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  42 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  43 import javax.xml.transform.stream.StreamResult;
  44 import javax.xml.transform.stream.StreamSource;


  45 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  46 import static jaxp.library.JAXPTestUtilities.failCleanup;
  47 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  48 import static org.testng.Assert.assertTrue;
  49 import org.testng.annotations.Test;
  50 import org.w3c.dom.Document;
  51 import org.xml.sax.SAXException;
  52 
  53 /**
  54  * Here Properties Object is populated with required properties.A transformer
  55  * is created using DOMSource. Using setOutputProperties(), Properties are set
  56  * for transformer. Then transform(StreamSource, StreamResult) is used for
  57  * transformation. This tests the setOutputProperties() method.
  58  */
  59 public class TransformerTest03 {
  60     /**
  61      * Test for Transformer.setOutputProperties method.


  62      */
  63     @Test
  64     public void testcase01() {
  65         String outputFile = CLASS_DIR + "transformer03.out";
  66         String goldFile = GOLDEN_DIR + "transformer03GF.out";
  67         String xsltFile = XML_DIR + "cities.xsl";
  68         String xmlFile = XML_DIR + "cities.xml";
  69 
  70         try (FileInputStream fis = new FileInputStream(xmlFile);
  71                 FileOutputStream fos = new FileOutputStream(outputFile)) {
  72             Properties properties = new Properties();
  73             properties.put("method", "xml");
  74             properties.put("encoding", "UTF-8");
  75             properties.put("omit-xml-declaration", "yes");
  76             properties.put("{http://xml.apache.org/xslt}indent-amount", "0");
  77             properties.put("indent", "no");
  78             properties.put("standalone", "no");
  79             properties.put("version", "1.0");
  80             properties.put("media-type", "text/xml");
  81 
  82             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  83             dbf.setNamespaceAware(true);
  84             DocumentBuilder db = dbf.newDocumentBuilder();
  85             Document document = db.parse(new File(xsltFile));
  86             DOMSource domSource = new DOMSource(document);
  87 
  88             Transformer transformer = TransformerFactory.newInstance().
  89                     newTransformer(domSource);
  90             StreamSource streamSource = new StreamSource(fis);
  91             StreamResult streamResult = new StreamResult(fos);
  92 
  93             transformer.setOutputProperties(properties);
  94             transformer.transform( streamSource, streamResult);
  95             assertTrue(compareWithGold(goldFile, outputFile));
  96         } catch (ParserConfigurationException | SAXException
  97                 | IOException | TransformerException ex){
  98             failUnexpected(ex);
  99         } finally {
 100             try {
 101                 Path outputPath = Paths.get(outputFile);
 102                 if(Files.exists(outputPath))
 103                     Files.delete(outputPath);
 104             } catch (IOException ex) {
 105                 failCleanup(ex, outputFile);
 106             }
 107         }

 108     }
 109 }
   1 /*
   2  * Copyright (c) 2003, 2015, 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.util.Properties;

  29 import javax.xml.parsers.DocumentBuilderFactory;

  30 import javax.xml.transform.Transformer;

  31 import javax.xml.transform.TransformerFactory;
  32 import javax.xml.transform.dom.DOMSource;

  33 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  34 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  35 import javax.xml.transform.stream.StreamResult;
  36 import javax.xml.transform.stream.StreamSource;
  37 import jaxp.library.JAXPFileBaseTest;
  38 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  39 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  40 import static org.testng.Assert.assertTrue;
  41 import org.testng.annotations.Test;


  42 
  43 /**
  44  * Here Properties Object is populated with required properties.A transformer
  45  * is created using DOMSource. Using setOutputProperties(), Properties are set
  46  * for transformer. Then transform(StreamSource, StreamResult) is used for
  47  * transformation. This tests the setOutputProperties() method.
  48  */
  49 public class TransformerTest03 extends JAXPFileBaseTest {
  50     /**
  51      * Test for Transformer.setOutputProperties method.
  52      * 
  53      * @throws Exception If any errors occur.
  54      */
  55     @Test
  56     public void testcase01() throws Exception {
  57         String outputFile = USER_DIR + "transformer03.out";
  58         String goldFile = GOLDEN_DIR + "transformer03GF.out";
  59         String xsltFile = XML_DIR + "cities.xsl";
  60         String xmlFile = XML_DIR + "cities.xml";
  61 
  62         try (FileInputStream fis = new FileInputStream(xmlFile);
  63                 FileOutputStream fos = new FileOutputStream(outputFile)) {
  64             Properties properties = new Properties();
  65             properties.put("method", "xml");
  66             properties.put("encoding", "UTF-8");
  67             properties.put("omit-xml-declaration", "yes");
  68             properties.put("{http://xml.apache.org/xslt}indent-amount", "0");
  69             properties.put("indent", "no");
  70             properties.put("standalone", "no");
  71             properties.put("version", "1.0");
  72             properties.put("media-type", "text/xml");
  73 
  74             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  75             dbf.setNamespaceAware(true);
  76             DOMSource domSource = new DOMSource(dbf.newDocumentBuilder().
  77                     parse(new File(xsltFile)));

  78 
  79             Transformer transformer = TransformerFactory.newInstance().
  80                     newTransformer(domSource);



  81             transformer.setOutputProperties(properties);
  82             transformer.transform(new StreamSource(fis), new StreamResult(fos));












  83         }
  84         assertTrue(compareWithGold(goldFile, outputFile));
  85     }
  86 }
< prev index next >