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