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