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