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 } | 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.FilePermission; 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 jaxp.library.JAXPBaseTest; 44 import static org.testng.Assert.assertEquals; 45 import org.testng.annotations.AfterGroups; 46 import org.testng.annotations.BeforeGroups; 47 import org.testng.annotations.Test; 48 import org.w3c.dom.Document; 49 import org.xml.sax.InputSource; 50 import org.xml.sax.SAXException; 51 52 /** 53 * URIResolver should be invoked when transform happens. 54 */ 55 public class URIResolverTest extends JAXPBaseTest implements URIResolver { 56 /** 57 * System ID constant. 58 */ 59 private final static String SYSTEM_ID = "file:///" + XML_DIR; 60 61 /** 62 * XML file include link file. 63 */ 64 private final static String XSL_INCLUDE_FILE = XML_DIR + "citiesinclude.xsl"; 65 66 /** 67 * XML file import link file. 68 */ 69 private final static String XSL_IMPORT_FILE = XML_DIR + "citiesimport.xsl"; 70 71 /** 72 * TEMP XML file. 73 */ 74 private final static String XSL_TEMP_FILE = "temp/cities.xsl"; 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 * Default constructor for testng invocation. 88 */ 89 public URIResolverTest(){ 90 validateHref = null; 91 validateBase = null; 92 } 93 94 /** 95 * Constructor for setting expected Href and expected Base URI. 96 * @param validateHref expected Href 97 * @param validateBase expected Base URI 98 */ 99 public URIResolverTest(String validateHref, String validateBase){ 100 this.validateHref = validateHref; 101 this.validateBase = validateBase; 102 } 103 104 /** 105 * Called by the processor when it encounters an xsl:include, xsl:import, 106 * or document() function. 107 * @param href An href attribute, which may be relative or absolute. 108 * @param base The base URI against which the first argument will be made 109 * absolute if the absolute URI is required. 110 * @return null always. 111 */ 112 @Override 113 public Source resolve(String href, String base) { 114 assertEquals(href, validateHref); 115 assertEquals(base, validateBase); 116 return null; 117 } 118 119 /** 120 * Save system property for restoring. 121 */ 122 @BeforeGroups (groups = {"readLocalFiles"}) 123 public void setFilePermissions() { 124 setPermissions(new FilePermission(XML_DIR + "-", "read")); 125 } 126 127 /** 128 * Restore the system property. 129 */ 130 @AfterGroups (groups = {"readLocalFiles"}) 131 public void restoreFilePermissions() { 132 setPermissions(); 133 } 134 135 /** 136 * This is to test the URIResolver.resolve() method when a transformer is 137 * created using StreamSource. style-sheet file has xsl:include in it. 138 * 139 * @throws TransformerConfigurationException If for some reason the 140 * TransformerHandler can not be created. 141 * @throws IOException if the file exists but is a directory rather than 142 * a regular file, does not exist but cannot be created, or cannot 143 * be opened for any other reason. 144 */ 145 @Test (groups = {"readLocalFiles"}) 146 public static void resolver01() throws TransformerConfigurationException, 147 IOException { 148 try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)) { 149 TransformerFactory tfactory = TransformerFactory.newInstance(); 150 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 151 tfactory.setURIResolver(resolver); 152 153 StreamSource streamSource = new StreamSource(fis); 154 streamSource.setSystemId(SYSTEM_ID); 155 tfactory.newTransformer(streamSource); 156 } 157 } 158 159 /** 160 * This is to test the URIResolver.resolve() method when a transformer is 161 * created using DOMSource. style-sheet file has xsl:include in it. 162 * 163 * @throws SAXException If any parse errors occur. 164 * @throws IOException if the file exists but is a directory rather than 165 * a regular file, does not exist but cannot be created, or cannot 166 * be opened for any other reason. 167 * @throws TransformerConfigurationException If for some reason the 168 * TransformerHandler can not be created. 169 * @throws ParserConfigurationException if a DocumentBuilder cannot be 170 * created which satisfies the configuration requested. 171 */ 172 @Test (groups = {"readLocalFiles"}) 173 public static void resolver02() throws ParserConfigurationException, 174 SAXException, TransformerConfigurationException, IOException { 175 TransformerFactory tfactory = TransformerFactory.newInstance(); 176 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 177 tfactory.setURIResolver(resolver); 178 179 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 180 dbf.setNamespaceAware(true); 181 DocumentBuilder db = dbf.newDocumentBuilder(); 182 Document document = db.parse(XSL_INCLUDE_FILE); 183 DOMSource domSource = new DOMSource(document, SYSTEM_ID); 184 185 tfactory.newTransformer(domSource); 186 } 187 188 /** 189 * This is to test the URIResolver.resolve() method when a transformer is 190 * created using SAXSource. style-sheet file has xsl:include in it. 191 * 192 * @throws IOException if the file exists but is a directory rather than 193 * a regular file, does not exist but cannot be created, or cannot 194 * be opened for any other reason. 195 * @throws TransformerConfigurationException If for some reason the 196 * TransformerHandler can not be created. 197 */ 198 @Test (groups = {"readLocalFiles"}) 199 public static void resolver03() throws TransformerConfigurationException, 200 IOException { 201 try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)){ 202 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 203 TransformerFactory tfactory = TransformerFactory.newInstance(); 204 tfactory.setURIResolver(resolver); 205 InputSource is = new InputSource(fis); 206 is.setSystemId(SYSTEM_ID); 207 SAXSource saxSource = new SAXSource(is); 208 tfactory.newTransformer(saxSource); 209 } 210 } 211 212 /** 213 * This is to test the URIResolver.resolve() method when a transformer is 214 * created using StreamSource. style-sheet file has xsl:import in it. 215 * 216 * @throws IOException if the file exists but is a directory rather than 217 * a regular file, does not exist but cannot be created, or cannot 218 * be opened for any other reason. 219 * @throws TransformerConfigurationException If for some reason the 220 * TransformerHandler can not be created. 221 */ 222 @Test (groups = {"readLocalFiles"}) 223 public static void resolver04() throws TransformerConfigurationException, 224 IOException { 225 try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)) { 226 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 227 TransformerFactory tfactory = TransformerFactory.newInstance(); 228 tfactory.setURIResolver(resolver); 229 StreamSource streamSource = new StreamSource(fis); 230 streamSource.setSystemId(SYSTEM_ID); 231 tfactory.newTransformer(streamSource); 232 } 233 } 234 235 /** 236 * This is to test the URIResolver.resolve() method when a transformer is 237 * created using DOMSource. style-sheet file has xsl:import in it. 238 * 239 * @throws SAXException If any parse errors occur. 240 * @throws IOException if the file exists but is a directory rather than 241 * a regular file, does not exist but cannot be created, or cannot 242 * be opened for any other reason. 243 * @throws TransformerConfigurationException If for some reason the 244 * TransformerHandler can not be created. 245 * @throws ParserConfigurationException if a DocumentBuilder cannot be 246 * created which satisfies the configuration requested. 247 */ 248 @Test (groups = {"readLocalFiles"}) 249 public static void resolver05() throws TransformerConfigurationException, 250 IOException, SAXException, ParserConfigurationException { 251 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 252 TransformerFactory tfactory = TransformerFactory.newInstance(); 253 tfactory.setURIResolver(resolver); 254 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 255 dbf.setNamespaceAware(true); 256 DocumentBuilder db = dbf.newDocumentBuilder(); 257 Document document = db.parse(new File(XSL_IMPORT_FILE)); 258 DOMSource domSource = new DOMSource(document, SYSTEM_ID); 259 tfactory.newTransformer(domSource); 260 } 261 262 /** 263 * This is to test the URIResolver.resolve() method when a transformer is 264 * created using SAXSource. style-sheet file has xsl:import in it. 265 * 266 * @throws IOException if the file exists but is a directory rather than 267 * a regular file, does not exist but cannot be created, or cannot 268 * be opened for any other reason. 269 * @throws TransformerConfigurationException If for some reason the 270 * TransformerHandler can not be created. 271 */ 272 @Test (groups = {"readLocalFiles"}) 273 public static void resolver06() throws IOException, TransformerConfigurationException { 274 try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)){ 275 URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); 276 TransformerFactory tfactory = TransformerFactory.newInstance(); 277 tfactory.setURIResolver(resolver); 278 InputSource is = new InputSource(fis); 279 is.setSystemId(SYSTEM_ID); 280 SAXSource saxSource = new SAXSource(is); 281 tfactory.newTransformer(saxSource); 282 } 283 } 284 285 /** 286 * This is to test the URIResolver.resolve() method when there is an error 287 * in the file. 288 * 289 * @throws SAXException If any parse errors occur. 290 * @throws IOException if the file exists but is a directory rather than 291 * a regular file, does not exist but cannot be created, or cannot 292 * be opened for any other reason. 293 * @throws TransformerException If an unrecoverable error occurs during the 294 * course of the transformation.. 295 * @throws ParserConfigurationException if a DocumentBuilder cannot be 296 * created which satisfies the configuration requested. 297 */ 298 @Test (groups = {"readLocalFiles"}) 299 public static void docResolver01() throws TransformerException, 300 IOException, SAXException, ParserConfigurationException { 301 try (FileInputStream fis = new FileInputStream(XML_DIR + "doctest.xsl")) { 302 URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID); 303 StreamSource streamSource = new StreamSource(fis); 304 streamSource.setSystemId(SYSTEM_ID); 305 306 Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource); 307 transformer.setURIResolver(resolver); 308 309 File f = new File(XML_DIR + "myFake.xml"); 310 Document document = DocumentBuilderFactory.newInstance(). 311 newDocumentBuilder().parse(f); 312 313 // Use a Transformer for output 314 DOMSource source = new DOMSource(document); 315 StreamResult result = new StreamResult(System.err); 316 transformer.transform(source, result); 317 } 318 } 319 } |