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