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