[bug-62796] remove use of Stax Namespace class from PackagePropertiesMarshaller

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-10-02 19:16:00 +00:00
parent b869085731
commit 46b1833bfc

View File

@ -21,15 +21,12 @@ import java.io.OutputStream;
import java.util.Optional;
import javax.xml.XMLConstants;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.events.Namespace;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.openxml4j.opc.internal.PartMarshaller;
import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.util.StaxHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@ -37,14 +34,14 @@ import org.w3c.dom.Element;
* Package properties marshaller.
*/
public class PackagePropertiesMarshaller implements PartMarshaller {
private final static Namespace namespaceDC, namespaceCoreProperties, namespaceDcTerms, namespaceXSI;
static {
final XMLEventFactory f = StaxHelper.newXMLEventFactory();
namespaceDC = f.createNamespace("dc", PackagePropertiesPart.NAMESPACE_DC_URI);
namespaceCoreProperties = f.createNamespace("cp", PackagePropertiesPart.NAMESPACE_CP_URI);
namespaceDcTerms = f.createNamespace("dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI);
namespaceXSI = f.createNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
}
private final static NamespaceImpl namespaceDC =
new NamespaceImpl("dc", PackagePropertiesPart.NAMESPACE_DC_URI);
private final static NamespaceImpl namespaceCoreProperties =
new NamespaceImpl("cp", PackagePropertiesPart.NAMESPACE_CP_URI);;
private final static NamespaceImpl namespaceDcTerms =
new NamespaceImpl("dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI);
private final static NamespaceImpl namespaceXSI =
new NamespaceImpl("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);;
protected static final String KEYWORD_CATEGORY = "category";
@ -99,10 +96,14 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
xmlDoc = DocumentHelper.createDocument();
Element rootElem = xmlDoc.createElementNS(namespaceCoreProperties.getNamespaceURI(),
getQName("coreProperties", namespaceCoreProperties));
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceCoreProperties);
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceDC);
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceDcTerms);
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceXSI);
DocumentHelper.addNamespaceDeclaration(rootElem,
namespaceCoreProperties.getPrefix(), namespaceCoreProperties.getNamespaceURI());
DocumentHelper.addNamespaceDeclaration(rootElem,
namespaceDC.getPrefix(), namespaceDC.getNamespaceURI());
DocumentHelper.addNamespaceDeclaration(rootElem,
namespaceDcTerms.getPrefix(), namespaceDcTerms.getNamespaceURI());
DocumentHelper.addNamespaceDeclaration(rootElem,
namespaceXSI.getPrefix(), namespaceXSI.getNamespaceURI());
xmlDoc.appendChild(rootElem);
addCategory();
@ -127,15 +128,15 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
/**
* Sets the given element's text content, creating it if necessary.
*/
private Element setElementTextContent(String localName, Namespace namespace, Optional<String> property) {
private Element setElementTextContent(String localName, NamespaceImpl namespace, Optional<String> property) {
return setElementTextContent(localName, namespace, property, property.orElse(null));
}
private String getQName(String localName, Namespace namespace) {
private String getQName(String localName, NamespaceImpl namespace) {
return namespace.getPrefix().isEmpty() ? localName : namespace.getPrefix() + ':' + localName;
}
private Element setElementTextContent(String localName, Namespace namespace, Optional<?> property, String propertyValue) {
private Element setElementTextContent(String localName, NamespaceImpl namespace, Optional<?> property, String propertyValue) {
if (!property.isPresent())
return null;
@ -150,7 +151,7 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
return elem;
}
private Element setElementTextContent(String localName, Namespace namespace, Optional<?> property, String propertyValue, String xsiType) {
private Element setElementTextContent(String localName, NamespaceImpl namespace, Optional<?> property, String propertyValue, String xsiType) {
Element element = setElementTextContent(localName, namespace, property, propertyValue);
if (element != null) {
element.setAttributeNS(namespaceXSI.getNamespaceURI(), getQName("type", namespaceXSI), xsiType);
@ -232,7 +233,6 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
/**
* Add 'last printed' property if needed.
*
*/
private void addLastPrinted() {
setElementTextContent(KEYWORD_LAST_PRINTED, namespaceCoreProperties, propsPart.getLastPrintedProperty(), propsPart.getLastPrintedPropertyString());
@ -270,4 +270,20 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
private void addVersion() {
setElementTextContent(KEYWORD_VERSION, namespaceCoreProperties, propsPart.getVersionProperty());
}
private static class NamespaceImpl {
private final String prefix;
private final String namespaceURI;
NamespaceImpl(String prefix, String namespaceURI) {
this.prefix = prefix;
this.namespaceURI = namespaceURI;
}
public String getPrefix() { return prefix; }
public String getNamespaceURI() {
return namespaceURI;
}
}
}