Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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