1 /* 2 * Copyright (c) 2003, 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 package javax.xml.transform.ptests; 24 25 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 26 import static org.testng.Assert.assertEquals; 27 import static org.testng.Assert.assertNotNull; 28 29 import java.io.File; 30 import java.io.FileInputStream; 31 32 import javax.xml.parsers.DocumentBuilder; 33 import javax.xml.parsers.DocumentBuilderFactory; 34 import javax.xml.transform.Source; 35 import javax.xml.transform.Transformer; 36 import javax.xml.transform.TransformerFactory; 37 import javax.xml.transform.URIResolver; 38 import javax.xml.transform.dom.DOMSource; 39 import javax.xml.transform.sax.SAXSource; 40 import javax.xml.transform.stream.StreamResult; 41 import javax.xml.transform.stream.StreamSource; 42 43 import org.testng.annotations.Listeners; 44 import org.testng.annotations.Test; 45 import org.w3c.dom.Document; 46 import org.xml.sax.InputSource; 47 48 /** 49 * URIResolver should be invoked when transform happens. 50 */ 51 /* 52 * @test 53 * @library /javax/xml/jaxp/libs 54 * @run testng/othervm -DrunSecMngr=true javax.xml.transform.ptests.URIResolverTest 55 * @run testng/othervm javax.xml.transform.ptests.URIResolverTest 56 */ 57 @Listeners({jaxp.library.FilePolicy.class}) 58 public class URIResolverTest implements URIResolver { 59 /** 60 * System ID constant. 61 */ 62 private final static String SYSTEM_ID = "file:///" + XML_DIR; 63 64 /** 65 * XML file include link file. 66 */ 67 private final static String XSL_INCLUDE_FILE = XML_DIR + "citiesinclude.xsl"; 68 69 /** 70 * XML file import link file. 71 */ 72 private final static String XSL_IMPORT_FILE = XML_DIR + "citiesimport.xsl"; 73 74 /** 75 * TEMP XML file. 76 */ 77 private final static String XSL_TEMP_FILE = "temp/cities.xsl"; 78 79 /** 80 * expected HREF. 81 */ 82 private final String validateHref; 83 84 /** 85 * expected Base URI. 86 */ 87 private final String validateBase; 88 89 /** 90 * Default constructor for testng invocation. 91 */ 92 public URIResolverTest(){ 93 validateHref = null; 94 validateBase = null; 95 } 96 97 /** 98 * Constructor for setting expected Href and expected Base URI. 99 * @param validateHref expected Href 100 * @param validateBase expected Base URI 101 */ 102 public URIResolverTest(String validateHref, String validateBase){ 103 this.validateHref = validateHref; 104 this.validateBase = validateBase; 105 } 106 107 /** 108 * Called by the processor when it encounters an xsl:include, xsl:import, 109 * or document() function. 110 * @param href An href attribute, which may be relative or absolute. 111 * @param base The base URI against which the first argument will be made 112 * absolute if the absolute URI is required. 113 * @return null always. 114 */ 115 @Override 116 public Source resolve(String href, String base) { 117 assertEquals(href, validateHref); 118 assertEquals(base, validateBase); 119 return null; 120 } 121 122 /** 123 * This is to test the URIResolver.resolve() method when a transformer is 124 * created using StreamSource. style-sheet file has xsl:include in it. 125 * 126 * @throws Exception If any errors occur. 127 */ 128 @Test 129 public static void resolver01() throws Exception { 130 try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)) { 131 TransformerFactory tfactory = TransformerFactory.newInstance(); 132 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 133 tfactory.setURIResolver(resolver); 134 135 StreamSource streamSource = new StreamSource(fis); 136 streamSource.setSystemId(SYSTEM_ID); 137 assertNotNull(tfactory.newTransformer(streamSource)); 138 } 139 } 140 141 /** 142 * This is to test the URIResolver.resolve() method when a transformer is 143 * created using DOMSource. style-sheet file has xsl:include in it. 144 * 145 * @throws Exception If any errors occur. 146 */ 147 @Test 148 public static void resolver02() throws Exception { 149 TransformerFactory tfactory = TransformerFactory.newInstance(); 150 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 151 tfactory.setURIResolver(resolver); 152 153 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 154 dbf.setNamespaceAware(true); 155 DocumentBuilder db = dbf.newDocumentBuilder(); 156 Document document = db.parse(XSL_INCLUDE_FILE); 157 DOMSource domSource = new DOMSource(document, SYSTEM_ID); 158 159 assertNotNull(tfactory.newTransformer(domSource)); 160 } 161 162 /** 163 * This is to test the URIResolver.resolve() method when a transformer is 164 * created using SAXSource. style-sheet file has xsl:include in it. 165 * 166 * @throws Exception If any errors occur. 167 */ 168 @Test 169 public static void resolver03() throws Exception { 170 try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)){ 171 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 172 TransformerFactory tfactory = TransformerFactory.newInstance(); 173 tfactory.setURIResolver(resolver); 174 InputSource is = new InputSource(fis); 175 is.setSystemId(SYSTEM_ID); 176 SAXSource saxSource = new SAXSource(is); 177 assertNotNull(tfactory.newTransformer(saxSource)); 178 } 179 } 180 181 /** 182 * This is to test the URIResolver.resolve() method when a transformer is 183 * created using StreamSource. style-sheet file has xsl:import in it. 184 * 185 * @throws Exception If any errors occur. 186 */ 187 @Test 188 public static void resolver04() throws Exception { 189 try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)) { 190 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 191 TransformerFactory tfactory = TransformerFactory.newInstance(); 192 tfactory.setURIResolver(resolver); 193 StreamSource streamSource = new StreamSource(fis); 194 streamSource.setSystemId(SYSTEM_ID); 195 assertNotNull(tfactory.newTransformer(streamSource)); 196 } 197 } 198 199 /** 200 * This is to test the URIResolver.resolve() method when a transformer is 201 * created using DOMSource. style-sheet file has xsl:import in it. 202 * 203 * @throws Exception If any errors occur. 204 */ 205 @Test 206 public static void resolver05() throws Exception { 207 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 208 TransformerFactory tfactory = TransformerFactory.newInstance(); 209 tfactory.setURIResolver(resolver); 210 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 211 dbf.setNamespaceAware(true); 212 DocumentBuilder db = dbf.newDocumentBuilder(); 213 Document document = db.parse(new File(XSL_IMPORT_FILE)); 214 DOMSource domSource = new DOMSource(document, SYSTEM_ID); 215 assertNotNull(tfactory.newTransformer(domSource)); 216 } 217 218 /** 219 * This is to test the URIResolver.resolve() method when a transformer is 220 * created using SAXSource. style-sheet file has xsl:import in it. 221 * 222 * @throws Exception If any errors occur. 223 */ 224 @Test 225 public static void resolver06() throws Exception { 226 try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)){ 227 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 228 TransformerFactory tfactory = TransformerFactory.newInstance(); 229 tfactory.setURIResolver(resolver); 230 InputSource is = new InputSource(fis); 231 is.setSystemId(SYSTEM_ID); 232 SAXSource saxSource = new SAXSource(is); 233 assertNotNull(tfactory.newTransformer(saxSource)); 234 } 235 } 236 237 /** 238 * This is to test the URIResolver.resolve() method when there is an error 239 * in the file. 240 * 241 * @throws Exception If any errors occur. 242 */ 243 @Test 244 public static void docResolver01() throws Exception { 245 try (FileInputStream fis = new FileInputStream(XML_DIR + "doctest.xsl")) { 246 URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID); 247 StreamSource streamSource = new StreamSource(fis); 248 streamSource.setSystemId(SYSTEM_ID); 249 250 Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource); 251 transformer.setURIResolver(resolver); 252 253 File f = new File(XML_DIR + "myFake.xml"); 254 Document document = DocumentBuilderFactory.newInstance(). 255 newDocumentBuilder().parse(f); 256 257 // Use a Transformer for output 258 DOMSource source = new DOMSource(document); 259 StreamResult result = new StreamResult(System.err); 260 // No exception is expected because resolver resolve wrong URI. 261 transformer.transform(source, result); 262 } 263 } 264 } 265 266