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.IOException;
  27 import java.io.InputStream;
  28 
  29 import javax.xml.parsers.DocumentBuilder;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 import javax.xml.transform.Source;
  33 import javax.xml.transform.Templates;
  34 import javax.xml.transform.Transformer;
  35 import javax.xml.transform.TransformerConfigurationException;
  36 import javax.xml.transform.TransformerException;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.transform.URIResolver;
  39 import javax.xml.transform.dom.DOMSource;
  40 import javax.xml.transform.stax.StAXResult;
  41 import javax.xml.transform.stax.StAXSource;
  42 import javax.xml.transform.stream.StreamSource;
  43 
  44 import org.testng.Assert;
  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  * @summary Test TransformerFactory.
  52  */
  53 @Listeners({jaxp.library.FilePolicy.class})
  54 public class TransformerFactoryTest {
  55 
  56     private static URIResolver resolver = new URIResolver() {
  57 
  58         private int n = 0;
  59 
  60         public Source resolve(String href, String base) throws TransformerException {
  61 
  62             System.out.println("resolving: " + href);
  63 
  64             if (n++ > 10) {
  65                 Assert.fail("Nesting too deep when resolving: " + href);
  66             }
  67 
  68             return new StreamSource(this.getClass().getResourceAsStream(href));
  69         }
  70     };
  71 
  72     private static Document load(InputStream in) throws IOException {
  73 
  74         Document document = null;
  75 
  76         try {
  77             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  78             dbf.setNamespaceAware(true);
  79             DocumentBuilder db = dbf.newDocumentBuilder();
  80             document = db.parse(in);
  81         } catch (ParserConfigurationException parserConfigurationException) {
  82             parserConfigurationException.printStackTrace();
  83             Assert.fail(parserConfigurationException.toString());
  84         } catch (SAXException saxException) {
  85             saxException.printStackTrace();
  86             Assert.fail(saxException.toString());
  87         }
  88 
  89         return document;
  90     }
  91 
  92     /**
  93      * <p>
  94      * Test stylesheets that import other stylesheets.
  95      * </p>
  96      *
  97      * <p>
  98      * Inspired by: CR 6236727-2125981 XSLTC never stops resolving imported
  99      * stylesheets when outer stylesheet is a DOMSource
 100      * </p>
 101      */
 102     @Test
 103     public final void testImport() {
 104 
 105         TransformerFactory tff = TransformerFactory.newInstance();
 106         tff.setURIResolver(resolver);
 107         Templates tt = null;
 108         Transformer tf = null;
 109 
 110         // work-a-round is to use a StreamSource.
 111         // test should complete
 112         System.out.println("StreamSource: pre-Transformer creation");
 113         System.out.flush(); // in case import hangs
 114         try {
 115             InputStream xin = this.getClass().getResourceAsStream("outer.xsl");
 116             tt = tff.newTemplates(new StreamSource(xin));
 117             tf = tt.newTransformer();
 118         } catch (TransformerConfigurationException ex) {
 119             ex.printStackTrace();
 120             Assert.fail(ex.toString());
 121         }
 122         System.out.println("StreamSource: post-Transformer creation");
 123 
 124         // CR is that DOMSource never stops resolving
 125         System.out.println("DOMSource: pre-Transformer creation");
 126         System.out.flush(); // in case import hangs
 127         try {
 128             InputStream xin = this.getClass().getResourceAsStream("outer.xsl");
 129             tt = tff.newTemplates(new DOMSource(load(xin)));
 130             tf = tt.newTransformer();
 131         } catch (TransformerConfigurationException ex) {
 132             ex.printStackTrace();
 133             Assert.fail(ex.toString());
 134         } catch (IOException ioException) {
 135             ioException.printStackTrace();
 136             Assert.fail(ioException.toString());
 137         }
 138         System.out.println("DOMSource: post-Transformer creation");
 139     }
 140 
 141     /**
 142      * Refer to 6631168 : StAXSource & StAXResult support in JavaSE6
 143      */
 144     @Test
 145     public final void testFeatures() {
 146         TransformerFactory tff = TransformerFactory.newInstance();
 147         Assert.assertTrue(tff.getFeature(StAXSource.FEATURE));
 148         Assert.assertTrue(tff.getFeature(StAXResult.FEATURE));
 149     }
 150 
 151 }