Skip to content

Commit

Permalink
[bug-67396] StreamHelper does not set standalone=yes when built-in ja…
Browse files Browse the repository at this point in the history
…vax Transformer is used

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912305 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Sep 14, 2023
1 parent e9b860f commit 542daa2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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() {
Expand All @@ -53,8 +54,10 @@ private 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) {
Expand All @@ -69,7 +72,6 @@ public void close() throws IOException {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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?");
}
}
}

0 comments on commit 542daa2

Please sign in to comment.