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