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