[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 java.util.Optional;
import javax.xml.XMLConstants; 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.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.openxml4j.opc.internal.PartMarshaller; import org.apache.poi.openxml4j.opc.internal.PartMarshaller;
import org.apache.poi.ooxml.util.DocumentHelper; import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.util.StaxHelper;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -37,105 +34,109 @@ import org.w3c.dom.Element;
* Package properties marshaller. * Package properties marshaller.
*/ */
public class PackagePropertiesMarshaller implements PartMarshaller { public class PackagePropertiesMarshaller implements PartMarshaller {
private final static Namespace namespaceDC, namespaceCoreProperties, namespaceDcTerms, namespaceXSI; private final static NamespaceImpl namespaceDC =
static { new NamespaceImpl("dc", PackagePropertiesPart.NAMESPACE_DC_URI);
final XMLEventFactory f = StaxHelper.newXMLEventFactory(); private final static NamespaceImpl namespaceCoreProperties =
namespaceDC = f.createNamespace("dc", PackagePropertiesPart.NAMESPACE_DC_URI); new NamespaceImpl("cp", PackagePropertiesPart.NAMESPACE_CP_URI);;
namespaceCoreProperties = f.createNamespace("cp", PackagePropertiesPart.NAMESPACE_CP_URI); private final static NamespaceImpl namespaceDcTerms =
namespaceDcTerms = f.createNamespace("dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI); new NamespaceImpl("dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI);
namespaceXSI = f.createNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); private final static NamespaceImpl namespaceXSI =
} new NamespaceImpl("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);;
protected static final String KEYWORD_CATEGORY = "category"; protected static final String KEYWORD_CATEGORY = "category";
protected static final String KEYWORD_CONTENT_STATUS = "contentStatus"; protected static final String KEYWORD_CONTENT_STATUS = "contentStatus";
protected static final String KEYWORD_CONTENT_TYPE = "contentType"; protected static final String KEYWORD_CONTENT_TYPE = "contentType";
protected static final String KEYWORD_CREATED = "created"; protected static final String KEYWORD_CREATED = "created";
protected static final String KEYWORD_CREATOR = "creator"; protected static final String KEYWORD_CREATOR = "creator";
protected static final String KEYWORD_DESCRIPTION = "description"; protected static final String KEYWORD_DESCRIPTION = "description";
protected static final String KEYWORD_IDENTIFIER = "identifier"; protected static final String KEYWORD_IDENTIFIER = "identifier";
protected static final String KEYWORD_KEYWORDS = "keywords"; protected static final String KEYWORD_KEYWORDS = "keywords";
protected static final String KEYWORD_LANGUAGE = "language"; protected static final String KEYWORD_LANGUAGE = "language";
protected static final String KEYWORD_LAST_MODIFIED_BY = "lastModifiedBy"; protected static final String KEYWORD_LAST_MODIFIED_BY = "lastModifiedBy";
protected static final String KEYWORD_LAST_PRINTED = "lastPrinted"; protected static final String KEYWORD_LAST_PRINTED = "lastPrinted";
protected static final String KEYWORD_MODIFIED = "modified"; protected static final String KEYWORD_MODIFIED = "modified";
protected static final String KEYWORD_REVISION = "revision"; protected static final String KEYWORD_REVISION = "revision";
protected static final String KEYWORD_SUBJECT = "subject"; protected static final String KEYWORD_SUBJECT = "subject";
protected static final String KEYWORD_TITLE = "title"; protected static final String KEYWORD_TITLE = "title";
protected static final String KEYWORD_VERSION = "version"; protected static final String KEYWORD_VERSION = "version";
PackagePropertiesPart propsPart; PackagePropertiesPart propsPart;
// The document // The document
Document xmlDoc; Document xmlDoc;
/** /**
* Marshall package core properties to an XML document. Always return * Marshall package core properties to an XML document. Always return
* <code>true</code>. * <code>true</code>.
*/ */
@Override @Override
public boolean marshall(PackagePart part, OutputStream out) public boolean marshall(PackagePart part, OutputStream out)
throws OpenXML4JException { throws OpenXML4JException {
if (!(part instanceof PackagePropertiesPart)) if (!(part instanceof PackagePropertiesPart))
throw new IllegalArgumentException( throw new IllegalArgumentException(
"'part' must be a PackagePropertiesPart instance."); "'part' must be a PackagePropertiesPart instance.");
propsPart = (PackagePropertiesPart) part; propsPart = (PackagePropertiesPart) part;
// Configure the document // Configure the document
xmlDoc = DocumentHelper.createDocument(); xmlDoc = DocumentHelper.createDocument();
Element rootElem = xmlDoc.createElementNS(namespaceCoreProperties.getNamespaceURI(), Element rootElem = xmlDoc.createElementNS(namespaceCoreProperties.getNamespaceURI(),
getQName("coreProperties", namespaceCoreProperties)); getQName("coreProperties", namespaceCoreProperties));
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceCoreProperties); DocumentHelper.addNamespaceDeclaration(rootElem,
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceDC); namespaceCoreProperties.getPrefix(), namespaceCoreProperties.getNamespaceURI());
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceDcTerms); DocumentHelper.addNamespaceDeclaration(rootElem,
DocumentHelper.addNamespaceDeclaration(rootElem, namespaceXSI); namespaceDC.getPrefix(), namespaceDC.getNamespaceURI());
DocumentHelper.addNamespaceDeclaration(rootElem,
namespaceDcTerms.getPrefix(), namespaceDcTerms.getNamespaceURI());
DocumentHelper.addNamespaceDeclaration(rootElem,
namespaceXSI.getPrefix(), namespaceXSI.getNamespaceURI());
xmlDoc.appendChild(rootElem); xmlDoc.appendChild(rootElem);
addCategory(); addCategory();
addContentStatus(); addContentStatus();
addContentType(); addContentType();
addCreated(); addCreated();
addCreator(); addCreator();
addDescription(); addDescription();
addIdentifier(); addIdentifier();
addKeywords(); addKeywords();
addLanguage(); addLanguage();
addLastModifiedBy(); addLastModifiedBy();
addLastPrinted(); addLastPrinted();
addModified(); addModified();
addRevision(); addRevision();
addSubject(); addSubject();
addTitle(); addTitle();
addVersion(); addVersion();
return true; return true;
} }
/** /**
* Sets the given element's text content, creating it if necessary. * 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)); 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; 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()) if (!property.isPresent())
return null; return null;
@ -150,7 +151,7 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
return elem; 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); Element element = setElementTextContent(localName, namespace, property, propertyValue);
if (element != null) { if (element != null) {
element.setAttributeNS(namespaceXSI.getNamespaceURI(), getQName("type", namespaceXSI), xsiType); element.setAttributeNS(namespaceXSI.getNamespaceURI(), getQName("type", namespaceXSI), xsiType);
@ -160,114 +161,129 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
/** /**
* Add category property element if needed. * Add category property element if needed.
*/ */
private void addCategory() { private void addCategory() {
setElementTextContent(KEYWORD_CATEGORY, namespaceCoreProperties, propsPart.getCategoryProperty()); setElementTextContent(KEYWORD_CATEGORY, namespaceCoreProperties, propsPart.getCategoryProperty());
} }
/**
* Add content status property element if needed.
*/
private void addContentStatus() {
setElementTextContent(KEYWORD_CONTENT_STATUS, namespaceCoreProperties, propsPart.getContentStatusProperty());
}
/**
* Add content type property element if needed.
*/
private void addContentType() {
setElementTextContent(KEYWORD_CONTENT_TYPE, namespaceCoreProperties, propsPart.getContentTypeProperty());
}
/**
* Add created property element if needed.
*/
private void addCreated() {
setElementTextContent(KEYWORD_CREATED, namespaceDcTerms, propsPart.getCreatedProperty(),
propsPart.getCreatedPropertyString(), "dcterms:W3CDTF");
}
/**
* Add creator property element if needed.
*/
private void addCreator() {
setElementTextContent(KEYWORD_CREATOR, namespaceDC, propsPart.getCreatorProperty());
}
/**
* Add description property element if needed.
*/
private void addDescription() {
setElementTextContent(KEYWORD_DESCRIPTION, namespaceDC, propsPart.getDescriptionProperty());
}
/**
* Add identifier property element if needed.
*/
private void addIdentifier() {
setElementTextContent(KEYWORD_IDENTIFIER, namespaceDC, propsPart.getIdentifierProperty());
}
/** /**
* Add keywords property element if needed. * Add content status property element if needed.
*/ */
private void addKeywords() { private void addContentStatus() {
setElementTextContent(KEYWORD_CONTENT_STATUS, namespaceCoreProperties, propsPart.getContentStatusProperty());
}
/**
* Add content type property element if needed.
*/
private void addContentType() {
setElementTextContent(KEYWORD_CONTENT_TYPE, namespaceCoreProperties, propsPart.getContentTypeProperty());
}
/**
* Add created property element if needed.
*/
private void addCreated() {
setElementTextContent(KEYWORD_CREATED, namespaceDcTerms, propsPart.getCreatedProperty(),
propsPart.getCreatedPropertyString(), "dcterms:W3CDTF");
}
/**
* Add creator property element if needed.
*/
private void addCreator() {
setElementTextContent(KEYWORD_CREATOR, namespaceDC, propsPart.getCreatorProperty());
}
/**
* Add description property element if needed.
*/
private void addDescription() {
setElementTextContent(KEYWORD_DESCRIPTION, namespaceDC, propsPart.getDescriptionProperty());
}
/**
* Add identifier property element if needed.
*/
private void addIdentifier() {
setElementTextContent(KEYWORD_IDENTIFIER, namespaceDC, propsPart.getIdentifierProperty());
}
/**
* Add keywords property element if needed.
*/
private void addKeywords() {
setElementTextContent(KEYWORD_KEYWORDS, namespaceCoreProperties, propsPart.getKeywordsProperty()); setElementTextContent(KEYWORD_KEYWORDS, namespaceCoreProperties, propsPart.getKeywordsProperty());
} }
/** /**
* Add language property element if needed. * Add language property element if needed.
*/ */
private void addLanguage() { private void addLanguage() {
setElementTextContent(KEYWORD_LANGUAGE, namespaceDC, propsPart.getLanguageProperty()); setElementTextContent(KEYWORD_LANGUAGE, namespaceDC, propsPart.getLanguageProperty());
} }
/** /**
* Add 'last modified by' property if needed. * Add 'last modified by' property if needed.
*/ */
private void addLastModifiedBy() { private void addLastModifiedBy() {
setElementTextContent(KEYWORD_LAST_MODIFIED_BY, namespaceCoreProperties, propsPart.getLastModifiedByProperty()); setElementTextContent(KEYWORD_LAST_MODIFIED_BY, namespaceCoreProperties, propsPart.getLastModifiedByProperty());
} }
/** /**
* Add 'last printed' property if needed. * Add 'last printed' property if needed.
* */
*/ private void addLastPrinted() {
private void addLastPrinted() {
setElementTextContent(KEYWORD_LAST_PRINTED, namespaceCoreProperties, propsPart.getLastPrintedProperty(), propsPart.getLastPrintedPropertyString()); setElementTextContent(KEYWORD_LAST_PRINTED, namespaceCoreProperties, propsPart.getLastPrintedProperty(), propsPart.getLastPrintedPropertyString());
} }
/** /**
* Add modified property element if needed. * Add modified property element if needed.
*/ */
private void addModified() { private void addModified() {
setElementTextContent(KEYWORD_MODIFIED, namespaceDcTerms, propsPart.getModifiedProperty(), setElementTextContent(KEYWORD_MODIFIED, namespaceDcTerms, propsPart.getModifiedProperty(),
propsPart.getModifiedPropertyString(), "dcterms:W3CDTF"); propsPart.getModifiedPropertyString(), "dcterms:W3CDTF");
} }
/** /**
* Add revision property if needed. * Add revision property if needed.
*/ */
private void addRevision() { private void addRevision() {
setElementTextContent(KEYWORD_REVISION, namespaceCoreProperties, propsPart.getRevisionProperty()); setElementTextContent(KEYWORD_REVISION, namespaceCoreProperties, propsPart.getRevisionProperty());
} }
/** /**
* Add subject property if needed. * Add subject property if needed.
*/ */
private void addSubject() { private void addSubject() {
setElementTextContent(KEYWORD_SUBJECT, namespaceDC, propsPart.getSubjectProperty()); setElementTextContent(KEYWORD_SUBJECT, namespaceDC, propsPart.getSubjectProperty());
} }
/** /**
* Add title property if needed. * Add title property if needed.
*/ */
private void addTitle() { private void addTitle() {
setElementTextContent(KEYWORD_TITLE, namespaceDC, propsPart.getTitleProperty()); setElementTextContent(KEYWORD_TITLE, namespaceDC, propsPart.getTitleProperty());
} }
private void addVersion() { private void addVersion() {
setElementTextContent(KEYWORD_VERSION, namespaceCoreProperties, propsPart.getVersionProperty()); 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;
}
}
} }