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.Listeners;
  41 import org.testng.annotations.Test;
  42 import org.xml.sax.InputSource;
  43 import org.xml.sax.helpers.DefaultHandler;
  44 
  45 /*
  46  * @test
  47  * @bug 4693341
  48  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  49  * @run testng/othervm -DrunSecMngr=true transform.Bug4693341Test
  50  * @run testng/othervm transform.Bug4693341Test
  51  * @summary Test transform with external dtd.
  52  */
  53 @Listeners({jaxp.library.FilePolicy.class})
  54 public class Bug4693341Test {
  55     // save dtd file to current working directory to avoid writing into source repository
  56     public void copyDTDtoWorkDir() throws IOException {
  57         try (FileInputStream dtdres = new FileInputStream(getClass().getResource("Bug4693341.dtd").getPath());
  58              FileOutputStream dtdwork = new FileOutputStream("Bug4693341.dtd");) {
  59             int n;
  60             byte[] buffer = new byte[1024];
  61             while((n = dtdres.read(buffer)) > -1) {
  62                 dtdwork.write(buffer, 0, n);
  63             }
  64         }
  65     }
  66 
  67     @Test
  68     public void test() {
  69         try {
  70             Transformer transformer = TransformerFactory.newInstance().newTransformer();
  71 
  72             copyDTDtoWorkDir();
  73 
  74             File outf = new File("Bug4693341.out");
  75             StreamResult result = new StreamResult(new FileOutputStream(outf));
  76 
  77             String in = getClass().getResource("Bug4693341.xml").getPath();
  78             File file = new File(in);
  79             StreamSource source = new StreamSource(new FileInputStream(file), ("file://" + in));
  80 
  81             transformer.transform(source, result);
  82 
  83             //URL inputsource = new URL("file", "", golden);
  84             URL output = new URL("file", "", outf.getPath());
  85 
  86             // error happens when trying to parse output
  87             String systemId = output.toExternalForm();
  88             System.out.println("systemId: " + systemId);
  89             InputSource is = new InputSource(systemId);
  90             SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  91             parser.parse(is, new DefaultHandler());
  92         } catch (Exception ex) {
  93             Assert.fail(ex.getMessage());
  94         }
  95     }
  96 }