8266019: StreamResult(File) writes to incorrect file path if # is part of the file path

Reviewed-by: dfuchs
This commit is contained in:
Joe Wang 2021-06-03 21:26:32 +00:00
parent b95586559c
commit 460ce5553c
2 changed files with 38 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,9 +28,10 @@ package javax.xml.transform.stream;
import javax.xml.transform.Result;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.net.MalformedURLException;
/**
* <p>Acts as an holder for a transformation result,
@ -95,10 +96,12 @@ public class StreamResult implements Result {
* @param f Must a non-null File reference.
*/
public StreamResult(File f) {
//convert file to appropriate URI, f.toURI().toASCIIString()
//converts the URI to string as per rule specified in
//RFC 2396,
setSystemId(f.toURI().toASCIIString());
try {
outputStream = new FileOutputStream(f);
} catch (FileNotFoundException ex) {
// fall back to the original implementation for compatibility
setSystemId(f.toURI().toASCIIString());
}
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,32 +24,40 @@
package transform;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
/*
* @test
* @bug 8238183
* @bug 8238183 8266019
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm transform.ResultTest
* @summary Verifies that the output of a transformation is well-formed when
* StAXResult is used.
*/
public class ResultTest {
public static final String TEST_DIR = System.getProperty("test.classes", ".");
// The XML contains a comment before the root element
final static String XML =
"<?xml version=\"1.0\" ?>\n"
@ -66,6 +74,25 @@ public class ResultTest {
}
}
/**
* @bug 8266019
* Verifies that a StreamResult created with a File is processed correctly.
*
* @throws Exception if test fails
*/
@Test
public void testStreamResult() throws Exception {
File f = new File(TEST_DIR + "/output/#/dom.xml");
f.getParentFile().mkdirs();
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
dom.appendChild(dom.createElement("root"));
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.transform(new DOMSource(dom), new StreamResult(f));
}
/**
* Transforms the XML using a StAXResult with a StreamWriter.
* @throws Exception if the process fails