1 /*
   2  * Copyright (c) 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 
  24 /**
  25  * @test
  26  * @bug 8062518
  27  * @compile ExtFunc.java
  28  * @run testng TestExternalDocument
  29  * @summary Test that nodes from external documents can be accessed with xsl
  30  * extension functions
  31  */
  32 
  33 import java.io.StringReader;
  34 import java.io.StringWriter;
  35 import javax.xml.transform.Source;
  36 import javax.xml.transform.Transformer;
  37 import javax.xml.transform.TransformerException;
  38 import javax.xml.transform.TransformerFactory;
  39 import javax.xml.transform.URIResolver;
  40 import javax.xml.transform.stream.StreamResult;
  41 import javax.xml.transform.stream.StreamSource;
  42 
  43 import org.testng.annotations.Test;
  44 
  45 public class TestExternalDocument {
  46 
  47     final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>Doc</Test>";
  48     final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  49             + "<xsl:transform version=\"1.0\""
  50             + " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" "
  51             + " xmlns:cfunc=\"http://xml.apache.org/xalan/java/\">"
  52             + "<xsl:template match=\"/\">"
  53             + "<xsl:element name=\"root\">"
  54             + "<xsl:variable name=\"other_doc\" select=\"document('input')\"/>"
  55             + "<!-- Source -->"
  56             + "<xsl:value-of select=\"cfunc:ExtFunc.test(/Test)\"/>"
  57             + "<!-- document() -->"
  58             + "<xsl:value-of select=\"cfunc:ExtFunc.test($other_doc/Test)\"/>"
  59             + "</xsl:element></xsl:template></xsl:transform>";
  60 
  61     @Test
  62     public void testAccessExternalDocument() throws Exception {
  63 
  64         //Prepare sources for transormation
  65         Source src = new StreamSource(new StringReader(xml));
  66         Source xslsrc = new StreamSource(new StringReader(xsl));
  67 
  68         //Create factory and transformer
  69         TransformerFactory tf = TransformerFactory.newInstance();
  70         Transformer t = tf.newTransformer( xslsrc );
  71         t.setErrorListener(tf.getErrorListener());
  72 
  73         //Set URI Resolver to return the newly constructed xml
  74         // stream source object from xml test string
  75         t.setURIResolver(new URIResolver() {
  76             @Override
  77             public Source resolve(String href, String base)
  78                     throws TransformerException {
  79                 return new StreamSource(new StringReader(xml));
  80             }
  81         });
  82 
  83         //Prepare output stream
  84         StringWriter xmlResultString = new StringWriter();
  85         StreamResult xmlResultStream = new StreamResult(xmlResultString);
  86         //Transform the xml
  87         t.transform(src, xmlResultStream);
  88         //If the document can't be accessed and the bug is in place then
  89         // reported exception will be thrown during transformation
  90         System.out.println("Transformation result:"+xmlResultString.toString().trim());
  91     }
  92 }