Skip to content
Permalink
Browse files
8266019: StreamResult(File) writes to incorrect file path if # is par…
…t of the file path

Reviewed-by: dfuchs
  • Loading branch information
JoeWang-Java committed Jun 3, 2021
1 parent b955865 commit 460ce5553c587715ef8244bee7e363b3587d2d0f
@@ -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 @@
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 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());
}
}

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

1 comment on commit 460ce55

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 460ce55 Jun 3, 2021

Please sign in to comment.