aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2023-09-14 11:23:05 +0000
committerPJ Fanning <fanningpj@apache.org>2023-09-14 11:23:05 +0000
commit542daa2ab873be4334afc1efcbc8c677974425fc (patch)
tree5c563f7082577052a3ac127d88a5af8248ba5ed2 /poi-ooxml
parente9b860f6d4e295900220225823198d6fae89d2c0 (diff)
downloadpoi-542daa2ab873be4334afc1efcbc8c677974425fc.tar.gz
poi-542daa2ab873be4334afc1efcbc8c677974425fc.zip
[bug-67396] StreamHelper does not set standalone=yes when built-in javax Transformer is used
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912305 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/StreamHelper.java8
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestStreamHelper.java38
2 files changed, 43 insertions, 3 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/StreamHelper.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/StreamHelper.java
index 39eee4d1d3..c8d32ce4a4 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/StreamHelper.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/StreamHelper.java
@@ -24,16 +24,17 @@ import java.io.OutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
-import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.Internal;
import org.apache.poi.util.XMLHelper;
import org.w3c.dom.Document;
+@Internal
public final class StreamHelper {
private StreamHelper() {
@@ -53,8 +54,10 @@ public final class StreamHelper {
public static boolean saveXmlInStream(Document xmlContent,
OutputStream outStream) {
try {
+ // https://bz.apache.org/bugzilla/show_bug.cgi?id=67396
+ xmlContent.setXmlStandalone(true);
Transformer trans = XMLHelper.newTransformer();
- Source xmlSource = new DOMSource(xmlContent);
+ DOMSource xmlSource = new DOMSource(xmlContent);
// prevent close of stream by transformer:
Result outputTarget = new StreamResult(new FilterOutputStream(
outStream) {
@@ -69,7 +72,6 @@ public final class StreamHelper {
out.flush(); // only flush, don't close!
}
});
- // xmlContent.setXmlStandalone(true);
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// don't indent xml documents, the indent will cause errors in calculating the xml signature
// because of different handling of linebreaks in Windows/Unix
diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestStreamHelper.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestStreamHelper.java
new file mode 100644
index 0000000000..2b0effc6b4
--- /dev/null
+++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestStreamHelper.java
@@ -0,0 +1,38 @@
+package org.apache.poi.openxml4j.opc;
+
+import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
+import org.apache.poi.ooxml.util.DocumentHelper;
+import org.junit.jupiter.api.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_WORDPROCESSINGML;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class TestStreamHelper {
+ @Test
+ void testStandaloneFlag() throws IOException {
+ Document doc = DocumentHelper.createDocument();
+ Element elDocument = doc.createElementNS(NS_WORDPROCESSINGML, "w:document");
+ doc.appendChild(elDocument);
+ Element elBody = doc.createElementNS(NS_WORDPROCESSINGML, "w:body");
+ elDocument.appendChild(elBody);
+ Element elParagraph = doc.createElementNS(NS_WORDPROCESSINGML, "w:p");
+ elBody.appendChild(elParagraph);
+ Element elRun = doc.createElementNS(NS_WORDPROCESSINGML, "w:r");
+ elParagraph.appendChild(elRun);
+ Element elText = doc.createElementNS(NS_WORDPROCESSINGML, "w:t");
+ elRun.appendChild(elText);
+ elText.setTextContent("Hello Open XML !");
+
+ try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
+ StreamHelper.saveXmlInStream(doc, bos);
+ String xml = bos.toString(StandardCharsets.UTF_8);
+ assertTrue(xml.contains("standalone=\"yes\""), "xml contains standalone=yes?");
+ assertTrue(xml.contains("encoding=\"UTF-8\""), "xml contains encoding=UTF-8?");
+ }
+ }
+}