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 static jaxp.library.JAXPTestUtilities.setSystemProperty; 27 28 import java.io.IOException; 29 import java.io.StringReader; 30 import java.io.StringWriter; 31 32 import javax.xml.parsers.ParserConfigurationException; 33 import javax.xml.parsers.SAXParserFactory; 34 import javax.xml.transform.Transformer; 35 import javax.xml.transform.TransformerException; 36 import javax.xml.transform.TransformerFactory; 37 import javax.xml.transform.sax.SAXSource; 38 import javax.xml.transform.sax.SAXTransformerFactory; 39 import javax.xml.transform.stream.StreamResult; 40 41 import org.testng.Assert; 42 import org.testng.annotations.Listeners; 43 import org.testng.annotations.Test; 44 import org.xml.sax.InputSource; 45 import org.xml.sax.SAXException; 46 import org.xml.sax.helpers.XMLFilterImpl; 47 48 /* 49 * @test 50 * @bug 6490921 51 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 52 * @run testng/othervm -DrunSecMngr=true transform.Bug6490921 53 * @run testng/othervm transform.Bug6490921 54 * @summary Test property org.xml.sax.driver is always applied in transformer API. 55 */ 56 @Listeners({jaxp.library.BasePolicy.class}) 57 public class Bug6490921 { 58 59 public static class ReaderStub extends XMLFilterImpl { 60 static boolean used = false; 61 ReaderStub()62 public ReaderStub() throws ParserConfigurationException, SAXException { 63 super(); 64 super.setParent(SAXParserFactory.newInstance().newSAXParser().getXMLReader()); 65 } 66 parse(InputSource input)67 public void parse(InputSource input) throws SAXException, IOException { 68 used = true; 69 super.parse(input); 70 } 71 parse(String systemId)72 public void parse(String systemId) throws SAXException, IOException { 73 used = true; 74 super.parse(systemId); 75 } 76 } 77 78 @Test test01()79 public void test01() { 80 String xml = "<?xml version='1.0'?><root/>"; 81 ReaderStub.used = false; 82 setSystemProperty("org.xml.sax.driver", ""); 83 84 // Don't set 'org.xml.sax.driver' here, just use default 85 try { 86 TransformerFactory transFactory = TransformerFactory.newInstance(); 87 Transformer transformer = transFactory.newTransformer(); 88 InputSource in = new InputSource(new StringReader(xml)); 89 SAXSource source = new SAXSource(in); 90 StreamResult result = new StreamResult(new StringWriter()); 91 transformer.transform(source, result); 92 Assert.assertTrue(!printWasReaderStubCreated()); 93 } catch (Exception ex) { 94 Assert.fail(ex.getMessage()); 95 } 96 } 97 98 @Test test02()99 public void test02() { 100 String xml = "<?xml version='1.0'?><root/>"; 101 ReaderStub.used = false; 102 setSystemProperty("org.xml.sax.driver", ReaderStub.class.getName()); 103 try { 104 TransformerFactory transFactory = TransformerFactory.newInstance(); 105 Transformer transformer = transFactory.newTransformer(); 106 InputSource in = new InputSource(new StringReader(xml)); 107 SAXSource source = new SAXSource(in); 108 StreamResult result = new StreamResult(new StringWriter()); 109 transformer.transform(source, result); 110 Assert.assertTrue(printWasReaderStubCreated()); 111 } catch (Exception ex) { 112 Assert.fail(ex.getMessage()); 113 } 114 } 115 116 @Test test03()117 public void test03() { 118 String xsl = "<?xml version='1.0'?>\n" + "<xsl:stylesheet" + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + " version='1.0'>\n" 119 + " <xsl:template match='/'>Hello World!</xsl:template>\n" + "</xsl:stylesheet>\n"; 120 121 ReaderStub.used = false; 122 setSystemProperty("org.xml.sax.driver", ReaderStub.class.getName()); 123 try { 124 TransformerFactory transFactory = TransformerFactory.newInstance(); 125 if (transFactory.getFeature(SAXTransformerFactory.FEATURE) == false) { 126 System.out.println("SAXTransformerFactory not supported"); 127 } 128 InputSource in = new InputSource(new StringReader(xsl)); 129 SAXSource source = new SAXSource(in); 130 131 transFactory.newTransformer(source); 132 Assert.assertTrue(printWasReaderStubCreated()); 133 } catch (TransformerException e) { 134 Assert.fail(e.getMessage()); 135 } 136 } 137 printWasReaderStubCreated()138 private static boolean printWasReaderStubCreated() { 139 if (ReaderStub.used) { 140 System.out.println("\tReaderStub is used."); 141 return ReaderStub.used; 142 } else { 143 System.out.println("\tReaderStub is not used."); 144 return ReaderStub.used; 145 } 146 } 147 } 148