mirror of https://github.com/apache/poi.git
[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
This commit is contained in:
parent
e9b860f6d4
commit
542daa2ab8
|
@ -24,16 +24,17 @@ import java.io.OutputStream;
|
||||||
|
|
||||||
import javax.xml.transform.OutputKeys;
|
import javax.xml.transform.OutputKeys;
|
||||||
import javax.xml.transform.Result;
|
import javax.xml.transform.Result;
|
||||||
import javax.xml.transform.Source;
|
|
||||||
import javax.xml.transform.Transformer;
|
import javax.xml.transform.Transformer;
|
||||||
import javax.xml.transform.TransformerException;
|
import javax.xml.transform.TransformerException;
|
||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
|
||||||
import org.apache.poi.util.IOUtils;
|
import org.apache.poi.util.IOUtils;
|
||||||
|
import org.apache.poi.util.Internal;
|
||||||
import org.apache.poi.util.XMLHelper;
|
import org.apache.poi.util.XMLHelper;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
|
@Internal
|
||||||
public final class StreamHelper {
|
public final class StreamHelper {
|
||||||
|
|
||||||
private StreamHelper() {
|
private StreamHelper() {
|
||||||
|
@ -53,8 +54,10 @@ public final class StreamHelper {
|
||||||
public static boolean saveXmlInStream(Document xmlContent,
|
public static boolean saveXmlInStream(Document xmlContent,
|
||||||
OutputStream outStream) {
|
OutputStream outStream) {
|
||||||
try {
|
try {
|
||||||
|
// https://bz.apache.org/bugzilla/show_bug.cgi?id=67396
|
||||||
|
xmlContent.setXmlStandalone(true);
|
||||||
Transformer trans = XMLHelper.newTransformer();
|
Transformer trans = XMLHelper.newTransformer();
|
||||||
Source xmlSource = new DOMSource(xmlContent);
|
DOMSource xmlSource = new DOMSource(xmlContent);
|
||||||
// prevent close of stream by transformer:
|
// prevent close of stream by transformer:
|
||||||
Result outputTarget = new StreamResult(new FilterOutputStream(
|
Result outputTarget = new StreamResult(new FilterOutputStream(
|
||||||
outStream) {
|
outStream) {
|
||||||
|
@ -69,7 +72,6 @@ public final class StreamHelper {
|
||||||
out.flush(); // only flush, don't close!
|
out.flush(); // only flush, don't close!
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// xmlContent.setXmlStandalone(true);
|
|
||||||
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||||
// don't indent xml documents, the indent will cause errors in calculating the xml signature
|
// don't indent xml documents, the indent will cause errors in calculating the xml signature
|
||||||
// because of different handling of linebreaks in Windows/Unix
|
// because of different handling of linebreaks in Windows/Unix
|
||||||
|
|
|
@ -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?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue