1 /*
   2  * Copyright (c) 2014, 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 
  24 package transform;
  25 
  26 import java.io.StringReader;
  27 import java.io.StringWriter;
  28 import java.util.Properties;
  29 
  30 import javax.xml.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.transform.OutputKeys;
  33 import javax.xml.transform.Transformer;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.dom.DOMResult;
  36 import javax.xml.transform.dom.DOMSource;
  37 import javax.xml.transform.stream.StreamResult;
  38 import javax.xml.transform.stream.StreamSource;
  39 
  40 import org.w3c.dom.Document;
  41 import org.w3c.dom.DocumentFragment;
  42 
  43 import org.testng.Assert;
  44 import org.testng.annotations.Listeners;
  45 import org.testng.annotations.Test;
  46 
  47 /*
  48  * @summary Test XSL extension for RTF works, for https://issues.apache.org/jira/i#browse/XALANJ-2290.
  49  */
  50 @Listeners({jaxp.library.FilePolicy.class})
  51 public class Issue2290Test {
  52 
  53     @Test
  54     public final void testTransform() throws Exception {
  55         DocumentFragment outNode = null;
  56         DocumentBuilder docBuilder = null;
  57         Document outDoc = null;
  58         // TransformerImpl transformer = null;
  59         StringReader execReaderXML = null;
  60         Properties propFormat = null;
  61         StringWriter sw = null;
  62 
  63         try {
  64             // template = TransformerFactory.newInstance().newTemplates(new
  65             // StreamSource("D:/Work/Apache/TestVar.xsl"));
  66             // transformer = (TransformerImpl) template.newTransformer();
  67             Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(getClass().getResourceAsStream("Issue2290.xsl")));
  68             System.out.print("Created Transformer");
  69             execReaderXML = new StringReader("<?xml version=\"1.0\"?> <doc>Stuff</doc>");
  70 
  71 
  72             docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  73             outDoc = docBuilder.newDocument();
  74             outNode = outDoc.createDocumentFragment();
  75             System.out.println("Created Fragment");
  76             System.out.println("execute transformer.");
  77             // transformer.transform(new StreamSource(execReaderXML),new
  78             // DOMResult(outNode));
  79             t.transform(new StreamSource(execReaderXML), new DOMResult(outNode));
  80             System.out.println("Finsished transformer.");
  81             sw = new StringWriter();
  82 
  83             StreamResult sr = new StreamResult(sw);
  84             t = TransformerFactory.newInstance().newTransformer();
  85             t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  86             t.transform(new DOMSource(outNode), sr);
  87             System.out.println(sw.toString());
  88         } catch (Exception e) {
  89             Assert.fail(e.toString());
  90         } finally {
  91         }
  92 
  93     }
  94 }