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