Skip to content

Commit

Permalink
8266019: StreamResult(File) writes to incorrect file path if # is par…
Browse files Browse the repository at this point in the history
…t of the file path

Reviewed-by: dfuchs
  • Loading branch information
JoeWang-Java committed Jun 3, 2021
1 parent b955865 commit 460ce55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,9 +28,10 @@
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,
Expand Down Expand Up @@ -95,10 +96,12 @@ public StreamResult(String systemId) {
* @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());
}
}

/**
Expand Down
31 changes: 29 additions & 2 deletions test/jaxp/javax/xml/jaxp/unittest/transform/ResultTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -66,6 +74,25 @@ public void setup() {
}
}

/**
* @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
Expand Down

1 comment on commit 460ce55

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.