use jaxp transformer instead of dom serializer to try fix old-xerces test failure

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842974 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-10-05 19:52:24 +00:00
parent b326a5cd01
commit 268c9230cd
1 changed files with 45 additions and 32 deletions

View File

@ -18,7 +18,9 @@
package org.apache.poi.xssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.EnumMap;
import java.util.Map;
@ -33,8 +35,10 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* Table style names defined in the OOXML spec.
@ -349,6 +353,7 @@ public enum XSSFBuiltinTableStyle {
/**
* NOTE: only checks by name, not definition.
*
* @param style
* @return true if the style represents a built-in style, false if it is null or a custom style
*/
@ -361,6 +366,7 @@ public enum XSSFBuiltinTableStyle {
return false;
}
}
/**
* Only init once - thus the synchronized. Lazy, after creating instances,
* and only when a style is actually needed, to avoid overhead for uses
@ -414,27 +420,34 @@ public enum XSSFBuiltinTableStyle {
}
}
private static String styleXML(Node dxfsNode, Node tableStyleNode) {
private static String styleXML(Node dxfsNode, Node tableStyleNode) throws IOException, TransformerException {
// built-ins doc uses 1-based dxf indexing, Excel uses 0 based.
// add a dummy node to adjust properly.
dxfsNode.insertBefore(dxfsNode.getOwnerDocument().createElement("dxf"), dxfsNode.getFirstChild());
DOMImplementationLS lsImpl = (DOMImplementationLS)dxfsNode.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
lsSerializer.getDomConfig().setParameter("xml-declaration", false);
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n")
.append("<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" "
+ "xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" "
+ "xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" "
+ "xmlns:x16r2=\"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main\" "
+ "mc:Ignorable=\"x14ac x16r2\">\n");
sb.append(lsSerializer.writeToString(dxfsNode));
sb.append(lsSerializer.writeToString(tableStyleNode));
.append("<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" ")
.append("xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" ")
.append("xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" ")
.append("xmlns:x16r2=\"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main\" ")
.append("mc:Ignorable=\"x14ac x16r2\">\n");
sb.append(writeToString(dxfsNode));
sb.append(writeToString(tableStyleNode));
sb.append("</styleSheet>");
return sb.toString();
}
private static String writeToString(Node node) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
try (StringWriter sw = new StringWriter()){
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
}
}
/**
* implementation for built-in styles
*/