1 /* 2 * Copyright (c) 2014, 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 24 package transform; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FileOutputStream; 29 import java.io.IOException; 30 import java.net.URL; 31 32 import javax.xml.parsers.SAXParser; 33 import javax.xml.parsers.SAXParserFactory; 34 import javax.xml.transform.Transformer; 35 import javax.xml.transform.TransformerFactory; 36 import javax.xml.transform.stream.StreamResult; 37 import javax.xml.transform.stream.StreamSource; 38 39 import org.testng.Assert; 40 import org.testng.annotations.Test; 41 import org.xml.sax.InputSource; 42 import org.xml.sax.helpers.DefaultHandler; 43 44 /* 45 * @bug 4693341 46 * @summary Test transform with external dtd. 47 */ 48 public class Bug4693341Test { 49 // save dtd file to current working directory to avoid writing into source repository 50 public void copyDTDtoWorkDir() throws IOException { 51 try (FileInputStream dtdres = new FileInputStream(getClass().getResource("Bug4693341.dtd").getPath()); 52 FileOutputStream dtdwork = new FileOutputStream("Bug4693341.dtd");) { 53 int n; 54 byte[] buffer = new byte[1024]; 55 while((n = dtdres.read(buffer)) > -1) { 56 dtdwork.write(buffer, 0, n); 57 } 58 } 59 } 60 61 @Test 62 public void test() { 63 try { 64 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 65 66 copyDTDtoWorkDir(); 67 68 File outf = new File("Bug4693341.out"); 69 StreamResult result = new StreamResult(new FileOutputStream(outf)); 70 71 String in = getClass().getResource("Bug4693341.xml").getPath(); 72 File file = new File(in); 73 StreamSource source = new StreamSource(new FileInputStream(file), ("file://" + in)); 74 75 transformer.transform(source, result); 76 77 //URL inputsource = new URL("file", "", golden); 78 URL output = new URL("file", "", outf.getPath()); 79 80 // error happens when trying to parse output 81 String systemId = output.toExternalForm(); 82 System.out.println("systemId: " + systemId); 83 InputSource is = new InputSource(systemId); 84 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 85 parser.parse(is, new DefaultHandler()); 86 } catch (Exception ex) { 87 Assert.fail(ex.getMessage()); 88 } 89 } 90 }