[bug-62438] remove nullable class (and use java.util.Optional). This closes #111

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1833459 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-06-13 12:18:32 +00:00
parent 57fe16be66
commit d101d9100c
15 changed files with 1136 additions and 972 deletions

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Date; import java.util.Date;
import java.util.Optional;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
@ -35,7 +36,6 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.StreamHelper; import org.apache.poi.openxml4j.opc.StreamHelper;
import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.openxml4j.util.Nullable;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty; import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
@ -242,60 +242,60 @@ public class POIXMLProperties {
} }
public String getCategory() { public String getCategory() {
return part.getCategoryProperty().getValue(); return part.getCategoryProperty().orElse(null);
} }
public void setCategory(String category) { public void setCategory(String category) {
part.setCategoryProperty(category); part.setCategoryProperty(category);
} }
public String getContentStatus() { public String getContentStatus() {
return part.getContentStatusProperty().getValue(); return part.getContentStatusProperty().orElse(null);
} }
public void setContentStatus(String contentStatus) { public void setContentStatus(String contentStatus) {
part.setContentStatusProperty(contentStatus); part.setContentStatusProperty(contentStatus);
} }
public String getContentType() { public String getContentType() {
return part.getContentTypeProperty().getValue(); return part.getContentTypeProperty().orElse(null);
} }
public void setContentType(String contentType) { public void setContentType(String contentType) {
part.setContentTypeProperty(contentType); part.setContentTypeProperty(contentType);
} }
public Date getCreated() { public Date getCreated() {
return part.getCreatedProperty().getValue(); return part.getCreatedProperty().orElse(null);
} }
public void setCreated(Nullable<Date> date) { public void setCreated(Optional<Date> date) {
part.setCreatedProperty(date); part.setCreatedProperty(date);
} }
public void setCreated(String date) { public void setCreated(String date) {
part.setCreatedProperty(date); part.setCreatedProperty(date);
} }
public String getCreator() { public String getCreator() {
return part.getCreatorProperty().getValue(); return part.getCreatorProperty().orElse(null);
} }
public void setCreator(String creator) { public void setCreator(String creator) {
part.setCreatorProperty(creator); part.setCreatorProperty(creator);
} }
public String getDescription() { public String getDescription() {
return part.getDescriptionProperty().getValue(); return part.getDescriptionProperty().orElse(null);
} }
public void setDescription(String description) { public void setDescription(String description) {
part.setDescriptionProperty(description); part.setDescriptionProperty(description);
} }
public String getIdentifier() { public String getIdentifier() {
return part.getIdentifierProperty().getValue(); return part.getIdentifierProperty().orElse(null);
} }
public void setIdentifier(String identifier) { public void setIdentifier(String identifier) {
part.setIdentifierProperty(identifier); part.setIdentifierProperty(identifier);
} }
public String getKeywords() { public String getKeywords() {
return part.getKeywordsProperty().getValue(); return part.getKeywordsProperty().orElse(null);
} }
public void setKeywords(String keywords) { public void setKeywords(String keywords) {
part.setKeywordsProperty(keywords); part.setKeywordsProperty(keywords);
} }
public Date getLastPrinted() { public Date getLastPrinted() {
return part.getLastPrintedProperty().getValue(); return part.getLastPrintedProperty().orElse(null);
} }
public void setLastPrinted(Nullable<Date> date) { public void setLastPrinted(Optional<Date> date) {
part.setLastPrintedProperty(date); part.setLastPrintedProperty(date);
} }
public void setLastPrinted(String date) { public void setLastPrinted(String date) {
@ -303,23 +303,23 @@ public class POIXMLProperties {
} }
/** @since POI 3.15 beta 3 */ /** @since POI 3.15 beta 3 */
public String getLastModifiedByUser() { public String getLastModifiedByUser() {
return part.getLastModifiedByProperty().getValue(); return part.getLastModifiedByProperty().orElse(null);
} }
/** @since POI 3.15 beta 3 */ /** @since POI 3.15 beta 3 */
public void setLastModifiedByUser(String user) { public void setLastModifiedByUser(String user) {
part.setLastModifiedByProperty(user); part.setLastModifiedByProperty(user);
} }
public Date getModified() { public Date getModified() {
return part.getModifiedProperty().getValue(); return part.getModifiedProperty().orElse(null);
} }
public void setModified(Nullable<Date> date) { public void setModified(Optional<Date> date) {
part.setModifiedProperty(date); part.setModifiedProperty(date);
} }
public void setModified(String date) { public void setModified(String date) {
part.setModifiedProperty(date); part.setModifiedProperty(date);
} }
public String getSubject() { public String getSubject() {
return part.getSubjectProperty().getValue(); return part.getSubjectProperty().orElse(null);
} }
public void setSubjectProperty(String subject) { public void setSubjectProperty(String subject) {
part.setSubjectProperty(subject); part.setSubjectProperty(subject);
@ -328,10 +328,10 @@ public class POIXMLProperties {
part.setTitleProperty(title); part.setTitleProperty(title);
} }
public String getTitle() { public String getTitle() {
return part.getTitleProperty().getValue(); return part.getTitleProperty().orElse(null);
} }
public String getRevision() { public String getRevision() {
return part.getRevisionProperty().getValue(); return part.getRevisionProperty().orElse(null);
} }
public void setRevision(String revision) { public void setRevision(String revision) {
try { try {

View File

@ -23,6 +23,7 @@ import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Optional;
import org.apache.poi.extractor.POITextExtractor; import org.apache.poi.extractor.POITextExtractor;
import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.ooxml.POIXMLDocument;
@ -70,11 +71,18 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
appendIfPresent(text, thing, Integer.toString(value)); appendIfPresent(text, thing, Integer.toString(value));
} }
private void appendIfPresent(StringBuilder text, String thing, Date value) { private void appendDateIfPresent(StringBuilder text, String thing, Optional<Date> value) {
if (value == null) { if (!value.isPresent()) {
return; return;
} }
appendIfPresent(text, thing, dateFormat.format(value)); appendIfPresent(text, thing, dateFormat.format(value.get()));
}
private void appendIfPresent(StringBuilder text, String thing, Optional<String> value) {
if (!value.isPresent()) {
return;
}
appendIfPresent(text, thing, value.get());
} }
private void appendIfPresent(StringBuilder text, String thing, String value) { private void appendIfPresent(StringBuilder text, String thing, String value) {
@ -103,26 +111,26 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
PackagePropertiesPart props = PackagePropertiesPart props =
document.getProperties().getCoreProperties().getUnderlyingProperties(); document.getProperties().getCoreProperties().getUnderlyingProperties();
appendIfPresent(text, "Category", props.getCategoryProperty().getValue()); appendIfPresent(text, "Category", props.getCategoryProperty());
appendIfPresent(text, "Category", props.getCategoryProperty().getValue()); appendIfPresent(text, "Category", props.getCategoryProperty());
appendIfPresent(text, "ContentStatus", props.getContentStatusProperty().getValue()); appendIfPresent(text, "ContentStatus", props.getContentStatusProperty());
appendIfPresent(text, "ContentType", props.getContentTypeProperty().getValue()); appendIfPresent(text, "ContentType", props.getContentTypeProperty());
appendIfPresent(text, "Created", props.getCreatedProperty().getValue()); appendDateIfPresent(text, "Created", props.getCreatedProperty());
appendIfPresent(text, "CreatedString", props.getCreatedPropertyString()); appendIfPresent(text, "CreatedString", props.getCreatedPropertyString());
appendIfPresent(text, "Creator", props.getCreatorProperty().getValue()); appendIfPresent(text, "Creator", props.getCreatorProperty());
appendIfPresent(text, "Description", props.getDescriptionProperty().getValue()); appendIfPresent(text, "Description", props.getDescriptionProperty());
appendIfPresent(text, "Identifier", props.getIdentifierProperty().getValue()); appendIfPresent(text, "Identifier", props.getIdentifierProperty());
appendIfPresent(text, "Keywords", props.getKeywordsProperty().getValue()); appendIfPresent(text, "Keywords", props.getKeywordsProperty());
appendIfPresent(text, "Language", props.getLanguageProperty().getValue()); appendIfPresent(text, "Language", props.getLanguageProperty());
appendIfPresent(text, "LastModifiedBy", props.getLastModifiedByProperty().getValue()); appendIfPresent(text, "LastModifiedBy", props.getLastModifiedByProperty());
appendIfPresent(text, "LastPrinted", props.getLastPrintedProperty().getValue()); appendDateIfPresent(text, "LastPrinted", props.getLastPrintedProperty());
appendIfPresent(text, "LastPrintedString", props.getLastPrintedPropertyString()); appendIfPresent(text, "LastPrintedString", props.getLastPrintedPropertyString());
appendIfPresent(text, "Modified", props.getModifiedProperty().getValue()); appendDateIfPresent(text, "Modified", props.getModifiedProperty());
appendIfPresent(text, "ModifiedString", props.getModifiedPropertyString()); appendIfPresent(text, "ModifiedString", props.getModifiedPropertyString());
appendIfPresent(text, "Revision", props.getRevisionProperty().getValue()); appendIfPresent(text, "Revision", props.getRevisionProperty());
appendIfPresent(text, "Subject", props.getSubjectProperty().getValue()); appendIfPresent(text, "Subject", props.getSubjectProperty());
appendIfPresent(text, "Title", props.getTitleProperty().getValue()); appendIfPresent(text, "Title", props.getTitleProperty());
appendIfPresent(text, "Version", props.getVersionProperty().getValue()); appendIfPresent(text, "Version", props.getVersionProperty());
return text.toString(); return text.toString();
} }

View File

@ -100,9 +100,6 @@ public final class PackageHelper {
p = pkg.getPart(relName); p = pkg.getPart(relName);
part_tgt.addRelationship(p.getPartName(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId()); part_tgt.addRelationship(p.getPartName(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
PackagePart dest; PackagePart dest;
if(!tgt.containPart(p.getPartName())){ if(!tgt.containPart(p.getPartName())){
dest = tgt.createPart(p.getPartName(), p.getContentType()); dest = tgt.createPart(p.getPartName(), p.getContentType());
@ -121,17 +118,17 @@ public final class PackageHelper {
* @param tgt target properties * @param tgt target properties
*/ */
private static void copyProperties(PackageProperties src, PackageProperties tgt) { private static void copyProperties(PackageProperties src, PackageProperties tgt) {
tgt.setCategoryProperty(src.getCategoryProperty().getValue()); tgt.setCategoryProperty(src.getCategoryProperty());
tgt.setContentStatusProperty(src.getContentStatusProperty().getValue()); tgt.setContentStatusProperty(src.getContentStatusProperty());
tgt.setContentTypeProperty(src.getContentTypeProperty().getValue()); tgt.setContentTypeProperty(src.getContentTypeProperty());
tgt.setCreatorProperty(src.getCreatorProperty().getValue()); tgt.setCreatorProperty(src.getCreatorProperty());
tgt.setDescriptionProperty(src.getDescriptionProperty().getValue()); tgt.setDescriptionProperty(src.getDescriptionProperty());
tgt.setIdentifierProperty(src.getIdentifierProperty().getValue()); tgt.setIdentifierProperty(src.getIdentifierProperty());
tgt.setKeywordsProperty(src.getKeywordsProperty().getValue()); tgt.setKeywordsProperty(src.getKeywordsProperty());
tgt.setLanguageProperty(src.getLanguageProperty().getValue()); tgt.setLanguageProperty(src.getLanguageProperty());
tgt.setRevisionProperty(src.getRevisionProperty().getValue()); tgt.setRevisionProperty(src.getRevisionProperty());
tgt.setSubjectProperty(src.getSubjectProperty().getValue()); tgt.setSubjectProperty(src.getSubjectProperty());
tgt.setTitleProperty(src.getTitleProperty().getValue()); tgt.setTitleProperty(src.getTitleProperty());
tgt.setVersionProperty(src.getVersionProperty().getValue()); tgt.setVersionProperty(src.getVersionProperty());
} }
} }

View File

@ -27,12 +27,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -51,7 +46,6 @@ import org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller;
import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller; import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller;
import org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller; import org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller;
import org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext; import org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext;
import org.apache.poi.openxml4j.util.Nullable;
import org.apache.poi.openxml4j.util.ZipEntrySource; import org.apache.poi.openxml4j.util.ZipEntrySource;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.NotImplemented; import org.apache.poi.util.NotImplemented;
@ -394,7 +388,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
pkg.packageProperties = new PackagePropertiesPart(pkg, pkg.packageProperties = new PackagePropertiesPart(pkg,
PackagingURIHelper.CORE_PROPERTIES_PART_NAME); PackagingURIHelper.CORE_PROPERTIES_PART_NAME);
pkg.packageProperties.setCreatorProperty("Generated by Apache POI OpenXML4J"); pkg.packageProperties.setCreatorProperty("Generated by Apache POI OpenXML4J");
pkg.packageProperties.setCreatedProperty(new Nullable<>(new Date())); pkg.packageProperties.setCreatedProperty(Optional.of(new Date()));
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
// Should never happen // Should never happen
throw new IllegalStateException(e); throw new IllegalStateException(e);

View File

@ -18,8 +18,7 @@
package org.apache.poi.openxml4j.opc; package org.apache.poi.openxml4j.opc;
import java.util.Date; import java.util.Date;
import java.util.Optional;
import org.apache.poi.openxml4j.util.Nullable;
/** /**
* Represents the core properties of an OPC package. * Represents the core properties of an OPC package.
@ -44,8 +43,9 @@ public interface PackageProperties {
/** /**
* Set the category of the content of this package. * Set the category of the content of this package.
* @return property value
*/ */
Nullable<String> getCategoryProperty(); Optional<String> getCategoryProperty();
/** /**
* Set the category of the content of this package. * Set the category of the content of this package.
@ -53,20 +53,34 @@ public interface PackageProperties {
void setCategoryProperty(String category); void setCategoryProperty(String category);
/** /**
* Set the status of the content. * Set the category of the content of this package.
* @since 4.0.0
*/ */
Nullable<String> getContentStatusProperty(); void setCategoryProperty(Optional<String> category);
/**
* Set the status of the content.
* @return property value
*/
Optional<String> getContentStatusProperty();
/** /**
* Get the status of the content. * Get the status of the content.
*/ */
void setContentStatusProperty(String contentStatus); void setContentStatusProperty(String contentStatus);
/**
* Get the status of the content.
* @since 4.0.0
*/
void setContentStatusProperty(Optional<String> contentStatus);
/** /**
* Get the type of content represented, generally defined by a specific use * Get the type of content represented, generally defined by a specific use
* and intended audience. * and intended audience.
* @return property value
*/ */
Nullable<String> getContentTypeProperty(); Optional<String> getContentTypeProperty();
/** /**
* Set the type of content represented, generally defined by a specific use * Set the type of content represented, generally defined by a specific use
@ -75,9 +89,17 @@ public interface PackageProperties {
void setContentTypeProperty(String contentType); void setContentTypeProperty(String contentType);
/** /**
* Get the date of creation of the resource. * Set the type of content represented, generally defined by a specific use
* and intended audience.
* @since 4.0.0
*/ */
Nullable<Date> getCreatedProperty(); void setContentTypeProperty(Optional<String> contentType);
/**
* Get the date of creation of the resource.
* @return property value
*/
Optional<Date> getCreatedProperty();
/** /**
* Set the date of creation of the resource. * Set the date of creation of the resource.
@ -87,13 +109,14 @@ public interface PackageProperties {
/** /**
* Set the date of creation of the resource. * Set the date of creation of the resource.
*/ */
void setCreatedProperty(Nullable<Date> created); void setCreatedProperty(Optional<Date> created);
/** /**
* Get the entity primarily responsible for making the content of the * Get the entity primarily responsible for making the content of the
* resource. * resource.
* @return property value
*/ */
Nullable<String> getCreatorProperty(); Optional<String> getCreatorProperty();
/** /**
* Set the entity primarily responsible for making the content of the * Set the entity primarily responsible for making the content of the
@ -101,10 +124,17 @@ public interface PackageProperties {
*/ */
void setCreatorProperty(String creator); void setCreatorProperty(String creator);
/**
* Set the entity primarily responsible for making the content of the
* resource.
* @since 4.0.0
*/
void setCreatorProperty(Optional<String> creator);
/** /**
* Get the explanation of the content of the resource. * Get the explanation of the content of the resource.
*/ */
Nullable<String> getDescriptionProperty(); Optional<String> getDescriptionProperty();
/** /**
* Set the explanation of the content of the resource. * Set the explanation of the content of the resource.
@ -112,21 +142,35 @@ public interface PackageProperties {
void setDescriptionProperty(String description); void setDescriptionProperty(String description);
/** /**
* Get an unambiguous reference to the resource within a given context. * Set the explanation of the content of the resource.
* @since 4.0.0
*/ */
Nullable<String> getIdentifierProperty(); void setDescriptionProperty(Optional<String> description);
/**
* Get an unambiguous reference to the resource within a given context.
* @return property value
*/
Optional<String> getIdentifierProperty();
/** /**
* Set an unambiguous reference to the resource within a given context. * Set an unambiguous reference to the resource within a given context.
*/ */
void setIdentifierProperty(String identifier); void setIdentifierProperty(String identifier);
/**
* Set an unambiguous reference to the resource within a given context.
* @since 4.0.0
*/
void setIdentifierProperty(Optional<String> identifier);
/** /**
* Get a delimited set of keywords to support searching and indexing. This * Get a delimited set of keywords to support searching and indexing. This
* is typically a list of terms that are not available elsewhere in the * is typically a list of terms that are not available elsewhere in the
* properties * properties
* @return property value
*/ */
Nullable<String> getKeywordsProperty(); Optional<String> getKeywordsProperty();
/** /**
* Set a delimited set of keywords to support searching and indexing. This * Set a delimited set of keywords to support searching and indexing. This
@ -136,19 +180,34 @@ public interface PackageProperties {
void setKeywordsProperty(String keywords); void setKeywordsProperty(String keywords);
/** /**
* Get the language of the intellectual content of the resource. * Set a delimited set of keywords to support searching and indexing. This
* is typically a list of terms that are not available elsewhere in the
* properties
* @since 4.0.0
*/ */
Nullable<String> getLanguageProperty(); void setKeywordsProperty(Optional<String> keywords);
/**
* Get the language of the intellectual content of the resource.
* @return property value
*/
Optional<String> getLanguageProperty();
/** /**
* Set the language of the intellectual content of the resource. * Set the language of the intellectual content of the resource.
*/ */
void setLanguageProperty(String language); void setLanguageProperty(String language);
/**
* Set the language of the intellectual content of the resource.
* @since 4.0.0
*/
void setLanguageProperty(Optional<String> language);
/** /**
* Get the user who performed the last modification. * Get the user who performed the last modification.
*/ */
Nullable<String> getLastModifiedByProperty(); Optional<String> getLastModifiedByProperty();
/** /**
* Set the user who performed the last modification. * Set the user who performed the last modification.
@ -156,9 +215,16 @@ public interface PackageProperties {
void setLastModifiedByProperty(String lastModifiedBy); void setLastModifiedByProperty(String lastModifiedBy);
/** /**
* Get the date and time of the last printing. * Set the user who performed the last modification.
* @since 4.0.0
*/ */
Nullable<Date> getLastPrintedProperty(); void setLastModifiedByProperty(Optional<String> lastModifiedBy);
/**
* Get the date and time of the last printing.
* @return property value
*/
Optional<Date> getLastPrintedProperty();
/** /**
* Set the date and time of the last printing. * Set the date and time of the last printing.
@ -168,12 +234,13 @@ public interface PackageProperties {
/** /**
* Set the date and time of the last printing. * Set the date and time of the last printing.
*/ */
void setLastPrintedProperty(Nullable<Date> lastPrinted); void setLastPrintedProperty(Optional<Date> lastPrinted);
/** /**
* Get the date on which the resource was changed. * Get the date on which the resource was changed.
* @return property value
*/ */
Nullable<Date> getModifiedProperty(); Optional<Date> getModifiedProperty();
/** /**
* Set the date on which the resource was changed. * Set the date on which the resource was changed.
@ -183,12 +250,13 @@ public interface PackageProperties {
/** /**
* Set the date on which the resource was changed. * Set the date on which the resource was changed.
*/ */
void setModifiedProperty(Nullable<Date> modified); void setModifiedProperty(Optional<Date> modified);
/** /**
* Get the revision number. * Get the revision number.
* @return property value
*/ */
Nullable<String> getRevisionProperty(); Optional<String> getRevisionProperty();
/** /**
* Set the revision number. * Set the revision number.
@ -196,9 +264,16 @@ public interface PackageProperties {
void setRevisionProperty(String revision); void setRevisionProperty(String revision);
/** /**
* Get the topic of the content of the resource. * Set the revision number.
* @since 4.0.0
*/ */
Nullable<String> getSubjectProperty(); void setRevisionProperty(Optional<String> revision);
/**
* Get the topic of the content of the resource.
* @return property value
*/
Optional<String> getSubjectProperty();
/** /**
* Set the topic of the content of the resource. * Set the topic of the content of the resource.
@ -206,9 +281,16 @@ public interface PackageProperties {
void setSubjectProperty(String subject); void setSubjectProperty(String subject);
/** /**
* Get the name given to the resource. * Set the topic of the content of the resource.
* @since 4.0.0
*/ */
Nullable<String> getTitleProperty(); void setSubjectProperty(Optional<String> subject);
/**
* Get the name given to the resource.
* @return property value
*/
Optional<String> getTitleProperty();
/** /**
* Set the name given to the resource. * Set the name given to the resource.
@ -216,12 +298,25 @@ public interface PackageProperties {
void setTitleProperty(String title); void setTitleProperty(String title);
/** /**
* Get the version number. * Set the name given to the resource.
* @since 4.0.0
*/ */
Nullable<String> getVersionProperty(); void setTitleProperty(Optional<String> title);
/**
* Get the version number.
* @return property value
*/
Optional<String> getVersionProperty();
/** /**
* Set the version number. * Set the version number.
*/ */
void setVersionProperty(String version); void setVersionProperty(String version);
/**
* Set the version number.
* @since 4.0.0
*/
void setVersionProperty(Optional<String> version);
} }

View File

@ -23,6 +23,7 @@ import java.text.ParsePosition;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Optional;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -34,7 +35,6 @@ import org.apache.poi.openxml4j.opc.PackageNamespaces;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageProperties; import org.apache.poi.openxml4j.opc.PackageProperties;
import org.apache.poi.openxml4j.util.Nullable;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
/** /**
@ -42,8 +42,7 @@ import org.apache.poi.util.LocaleUtil;
* *
* @author Julien Chable * @author Julien Chable
*/ */
public final class PackagePropertiesPart extends PackagePart implements public final class PackagePropertiesPart extends PackagePart implements PackageProperties {
PackageProperties {
public final static String NAMESPACE_DC_URI = PackageProperties.NAMESPACE_DC; public final static String NAMESPACE_DC_URI = PackageProperties.NAMESPACE_DC;
@ -95,7 +94,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* value might be used by an application's user interface to facilitate * value might be used by an application's user interface to facilitate
* navigation of a large set of documents. end example] * navigation of a large set of documents. end example]
*/ */
protected Nullable<String> category = new Nullable<>(); protected Optional<String> category = Optional.empty();
/** /**
* The status of the content. * The status of the content.
@ -103,7 +102,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* [Example: Values might include "Draft", "Reviewed", and "Final". end * [Example: Values might include "Draft", "Reviewed", and "Final". end
* example] * example]
*/ */
protected Nullable<String> contentStatus = new Nullable<>(); protected Optional<String> contentStatus = Optional.empty();
/** /**
* The type of content represented, generally defined by a specific use and * The type of content represented, generally defined by a specific use and
@ -113,17 +112,17 @@ public final class PackagePropertiesPart extends PackagePart implements
* "Exam". end example] [Note: This property is distinct from MIME content * "Exam". end example] [Note: This property is distinct from MIME content
* types as defined in RFC 2616. end note] * types as defined in RFC 2616. end note]
*/ */
protected Nullable<String> contentType = new Nullable<>(); protected Optional<String> contentType = Optional.empty();
/** /**
* Date of creation of the resource. * Date of creation of the resource.
*/ */
protected Nullable<Date> created = new Nullable<>(); protected Optional<Date> created = Optional.empty();
/** /**
* An entity primarily responsible for making the content of the resource. * An entity primarily responsible for making the content of the resource.
*/ */
protected Nullable<String> creator = new Nullable<>(); protected Optional<String> creator = Optional.empty();
/** /**
* An explanation of the content of the resource. * An explanation of the content of the resource.
@ -132,19 +131,19 @@ public final class PackagePropertiesPart extends PackagePart implements
* to a graphical representation of content, and a free-text account of the * to a graphical representation of content, and a free-text account of the
* content. end example] * content. end example]
*/ */
protected Nullable<String> description = new Nullable<>(); protected Optional<String> description = Optional.empty();
/** /**
* An unambiguous reference to the resource within a given context. * An unambiguous reference to the resource within a given context.
*/ */
protected Nullable<String> identifier = new Nullable<>(); protected Optional<String> identifier = Optional.empty();
/** /**
* A delimited set of keywords to support searching and indexing. This is * A delimited set of keywords to support searching and indexing. This is
* typically a list of terms that are not available elsewhere in the * typically a list of terms that are not available elsewhere in the
* properties. * properties.
*/ */
protected Nullable<String> keywords = new Nullable<>(); protected Optional<String> keywords = Optional.empty();
/** /**
* The language of the intellectual content of the resource. * The language of the intellectual content of the resource.
@ -152,7 +151,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* [Note: IETF RFC 3066 provides guidance on encoding to represent * [Note: IETF RFC 3066 provides guidance on encoding to represent
* languages. end note] * languages. end note]
*/ */
protected Nullable<String> language = new Nullable<>(); protected Optional<String> language = Optional.empty();
/** /**
* The user who performed the last modification. The identification is * The user who performed the last modification. The identification is
@ -161,17 +160,17 @@ public final class PackagePropertiesPart extends PackagePart implements
* [Example: A name, email address, or employee ID. end example] It is * [Example: A name, email address, or employee ID. end example] It is
* recommended that this value be as concise as possible. * recommended that this value be as concise as possible.
*/ */
protected Nullable<String> lastModifiedBy = new Nullable<>(); protected Optional<String> lastModifiedBy = Optional.empty();
/** /**
* The date and time of the last printing. * The date and time of the last printing.
*/ */
protected Nullable<Date> lastPrinted = new Nullable<>(); protected Optional<Date> lastPrinted = Optional.empty();
/** /**
* Date on which the resource was changed. * Date on which the resource was changed.
*/ */
protected Nullable<Date> modified = new Nullable<>(); protected Optional<Date> modified = Optional.empty();
/** /**
* The revision number. * The revision number.
@ -179,22 +178,22 @@ public final class PackagePropertiesPart extends PackagePart implements
* [Example: This value might indicate the number of saves or revisions, * [Example: This value might indicate the number of saves or revisions,
* provided the application updates it after each revision. end example] * provided the application updates it after each revision. end example]
*/ */
protected Nullable<String> revision = new Nullable<>(); protected Optional<String> revision = Optional.empty();
/** /**
* The topic of the content of the resource. * The topic of the content of the resource.
*/ */
protected Nullable<String> subject = new Nullable<>(); protected Optional<String> subject = Optional.empty();
/** /**
* The name given to the resource. * The name given to the resource.
*/ */
protected Nullable<String> title = new Nullable<>(); protected Optional<String> title = Optional.empty();
/** /**
* The version number. This value is set by the user or by the application. * The version number. This value is set by the user or by the application.
*/ */
protected Nullable<String> version = new Nullable<>(); protected Optional<String> version = Optional.empty();
/* /*
* Getters and setters * Getters and setters
@ -205,7 +204,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getCategoryProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getCategoryProperty()
*/ */
public Nullable<String> getCategoryProperty() { public Optional<String> getCategoryProperty() {
return category; return category;
} }
@ -214,7 +213,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getContentStatusProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getContentStatusProperty()
*/ */
public Nullable<String> getContentStatusProperty() { public Optional<String> getContentStatusProperty() {
return contentStatus; return contentStatus;
} }
@ -223,7 +222,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getContentTypeProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getContentTypeProperty()
*/ */
public Nullable<String> getContentTypeProperty() { public Optional<String> getContentTypeProperty() {
return contentType; return contentType;
} }
@ -232,7 +231,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getCreatedProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getCreatedProperty()
*/ */
public Nullable<Date> getCreatedProperty() { public Optional<Date> getCreatedProperty() {
return created; return created;
} }
@ -250,7 +249,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getCreatorProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getCreatorProperty()
*/ */
public Nullable<String> getCreatorProperty() { public Optional<String> getCreatorProperty() {
return creator; return creator;
} }
@ -259,7 +258,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getDescriptionProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getDescriptionProperty()
*/ */
public Nullable<String> getDescriptionProperty() { public Optional<String> getDescriptionProperty() {
return description; return description;
} }
@ -268,7 +267,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getIdentifierProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getIdentifierProperty()
*/ */
public Nullable<String> getIdentifierProperty() { public Optional<String> getIdentifierProperty() {
return identifier; return identifier;
} }
@ -277,7 +276,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getKeywordsProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getKeywordsProperty()
*/ */
public Nullable<String> getKeywordsProperty() { public Optional<String> getKeywordsProperty() {
return keywords; return keywords;
} }
@ -286,7 +285,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getLanguageProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getLanguageProperty()
*/ */
public Nullable<String> getLanguageProperty() { public Optional<String> getLanguageProperty() {
return language; return language;
} }
@ -295,7 +294,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getLastModifiedByProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getLastModifiedByProperty()
*/ */
public Nullable<String> getLastModifiedByProperty() { public Optional<String> getLastModifiedByProperty() {
return lastModifiedBy; return lastModifiedBy;
} }
@ -304,7 +303,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getLastPrintedProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getLastPrintedProperty()
*/ */
public Nullable<Date> getLastPrintedProperty() { public Optional<Date> getLastPrintedProperty() {
return lastPrinted; return lastPrinted;
} }
@ -322,7 +321,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getModifiedProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getModifiedProperty()
*/ */
public Nullable<Date> getModifiedProperty() { public Optional<Date> getModifiedProperty() {
return modified; return modified;
} }
@ -332,10 +331,10 @@ public final class PackagePropertiesPart extends PackagePart implements
* @return A string representation of the modified date. * @return A string representation of the modified date.
*/ */
public String getModifiedPropertyString() { public String getModifiedPropertyString() {
if (modified.hasValue()) { if (modified.isPresent()) {
return getDateValue(modified); return getDateValue(modified);
} }
return getDateValue(new Nullable<>(new Date())); return getDateValue(Optional.of(new Date()));
} }
/** /**
@ -343,7 +342,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getRevisionProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getRevisionProperty()
*/ */
public Nullable<String> getRevisionProperty() { public Optional<String> getRevisionProperty() {
return revision; return revision;
} }
@ -352,7 +351,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getSubjectProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getSubjectProperty()
*/ */
public Nullable<String> getSubjectProperty() { public Optional<String> getSubjectProperty() {
return subject; return subject;
} }
@ -361,7 +360,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getTitleProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getTitleProperty()
*/ */
public Nullable<String> getTitleProperty() { public Optional<String> getTitleProperty() {
return title; return title;
} }
@ -370,7 +369,7 @@ public final class PackagePropertiesPart extends PackagePart implements
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#getVersionProperty() * @see org.apache.poi.openxml4j.opc.PackageProperties#getVersionProperty()
*/ */
public Nullable<String> getVersionProperty() { public Optional<String> getVersionProperty() {
return version; return version;
} }
@ -383,6 +382,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.category = setStringValue(category); this.category = setStringValue(category);
} }
/**
* Set the category.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setCategoryProperty(java.util.Optional)
*/
public void setCategoryProperty(Optional<String> category) { this.category = category; }
/** /**
* Set the content status. * Set the content status.
* *
@ -392,6 +398,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.contentStatus = setStringValue(contentStatus); this.contentStatus = setStringValue(contentStatus);
} }
/**
* Set the content status.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setContentStatusProperty(java.util.Optional)
*/
public void setContentStatusProperty(Optional<String> contentStatus) { this.contentStatus = contentStatus; }
/** /**
* Set the content type. * Set the content type.
* *
@ -401,10 +414,17 @@ public final class PackagePropertiesPart extends PackagePart implements
this.contentType = setStringValue(contentType); this.contentType = setStringValue(contentType);
} }
/**
* Set the content type.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setContentTypeProperty(java.util.Optional)
*/
public void setContentTypeProperty(Optional<String> contentType) { this.contentType = contentType; }
/** /**
* Set the created date. * Set the created date.
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#setCreatedProperty(org.apache.poi.openxml4j.util.Nullable) * @see org.apache.poi.openxml4j.opc.PackageProperties#setCreatedProperty(java.util.Optional)
*/ */
public void setCreatedProperty(String created) { public void setCreatedProperty(String created) {
try { try {
@ -417,10 +437,10 @@ public final class PackagePropertiesPart extends PackagePart implements
/** /**
* Set the created date. * Set the created date.
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#setCreatedProperty(org.apache.poi.openxml4j.util.Nullable) * @see org.apache.poi.openxml4j.opc.PackageProperties#setCreatedProperty(java.util.Optional)
*/ */
public void setCreatedProperty(Nullable<Date> created) { public void setCreatedProperty(Optional<Date> created) {
if (created.hasValue()) if (created.isPresent())
this.created = created; this.created = created;
} }
@ -433,6 +453,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.creator = setStringValue(creator); this.creator = setStringValue(creator);
} }
/**
* Set the creator.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setCreatorProperty(java.util.Optional)
*/
public void setCreatorProperty(Optional<String> creator) { this.creator = creator; }
/** /**
* Set the description. * Set the description.
* *
@ -442,6 +469,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.description = setStringValue(description); this.description = setStringValue(description);
} }
/**
* Set the description.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setDescriptionProperty(java.util.Optional)
*/
public void setDescriptionProperty(Optional<String> description) { this.description = description; }
/** /**
* Set identifier. * Set identifier.
* *
@ -451,6 +485,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.identifier = setStringValue(identifier); this.identifier = setStringValue(identifier);
} }
/**
* Set identifier.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setIdentifierProperty(java.util.Optional)
*/
public void setIdentifierProperty(Optional<String> identifier) { this.identifier = identifier; }
/** /**
* Set keywords. * Set keywords.
* *
@ -460,6 +501,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.keywords = setStringValue(keywords); this.keywords = setStringValue(keywords);
} }
/**
* Set keywords.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setKeywordsProperty(java.util.Optional)
*/
public void setKeywordsProperty(Optional<String> keywords) { this.keywords = keywords; }
/** /**
* Set language. * Set language.
* *
@ -469,6 +517,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.language = setStringValue(language); this.language = setStringValue(language);
} }
/**
* Set language.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setLanguageProperty(java.util.Optional)
*/
public void setLanguageProperty(Optional<String> language) { this.language = language; }
/** /**
* Set last modifications author. * Set last modifications author.
* *
@ -478,10 +533,19 @@ public final class PackagePropertiesPart extends PackagePart implements
this.lastModifiedBy = setStringValue(lastModifiedBy); this.lastModifiedBy = setStringValue(lastModifiedBy);
} }
/**
* Set last modifications author.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setLastModifiedByProperty(java.util.Optional)
*/
public void setLastModifiedByProperty(Optional<String> lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
/** /**
* Set last printed date. * Set last printed date.
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#setLastPrintedProperty(org.apache.poi.openxml4j.util.Nullable) * @see org.apache.poi.openxml4j.opc.PackageProperties#setLastPrintedProperty(java.util.Optional)
*/ */
public void setLastPrintedProperty(String lastPrinted) { public void setLastPrintedProperty(String lastPrinted) {
try { try {
@ -495,17 +559,17 @@ public final class PackagePropertiesPart extends PackagePart implements
/** /**
* Set last printed date. * Set last printed date.
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#setLastPrintedProperty(org.apache.poi.openxml4j.util.Nullable) * @see org.apache.poi.openxml4j.opc.PackageProperties#setLastPrintedProperty(java.util.Optional)
*/ */
public void setLastPrintedProperty(Nullable<Date> lastPrinted) { public void setLastPrintedProperty(Optional<Date> lastPrinted) {
if (lastPrinted.hasValue()) if (lastPrinted.isPresent())
this.lastPrinted = lastPrinted; this.lastPrinted = lastPrinted;
} }
/** /**
* Set last modification date. * Set last modification date.
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#setModifiedProperty(org.apache.poi.openxml4j.util.Nullable) * @see org.apache.poi.openxml4j.opc.PackageProperties#setModifiedProperty(java.util.Optional)
*/ */
public void setModifiedProperty(String modified) { public void setModifiedProperty(String modified) {
try { try {
@ -519,13 +583,20 @@ public final class PackagePropertiesPart extends PackagePart implements
/** /**
* Set last modification date. * Set last modification date.
* *
* @see org.apache.poi.openxml4j.opc.PackageProperties#setModifiedProperty(org.apache.poi.openxml4j.util.Nullable) * @see org.apache.poi.openxml4j.opc.PackageProperties#setModifiedProperty(java.util.Optional)
*/ */
public void setModifiedProperty(Nullable<Date> modified) { public void setModifiedProperty(Optional<Date> modified) {
if (modified.hasValue()) if (modified.isPresent())
this.modified = modified; this.modified = modified;
} }
/**
* Set revision.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setRevisionProperty(java.util.Optional)
*/
public void setRevisionProperty(Optional<String> revision) { this.revision = revision; }
/** /**
* Set revision. * Set revision.
* *
@ -534,7 +605,6 @@ public final class PackagePropertiesPart extends PackagePart implements
public void setRevisionProperty(String revision) { public void setRevisionProperty(String revision) {
this.revision = setStringValue(revision); this.revision = setStringValue(revision);
} }
/** /**
* Set subject. * Set subject.
* *
@ -544,6 +614,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.subject = setStringValue(subject); this.subject = setStringValue(subject);
} }
/**
* Set subject.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setSubjectProperty(java.util.Optional)
*/
public void setSubjectProperty(Optional<String> subject) { this.subject = subject; }
/** /**
* Set title. * Set title.
* *
@ -553,6 +630,13 @@ public final class PackagePropertiesPart extends PackagePart implements
this.title = setStringValue(title); this.title = setStringValue(title);
} }
/**
* Set title.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setTitleProperty(java.util.Optional)
*/
public void setTitleProperty(Optional<String> title) { this.title = title; }
/** /**
* Set version. * Set version.
* *
@ -563,24 +647,31 @@ public final class PackagePropertiesPart extends PackagePart implements
} }
/** /**
* Convert a strig value into a Nullable<String> * Set version.
*
* @see org.apache.poi.openxml4j.opc.PackageProperties#setVersionProperty(java.util.Optional)
*/ */
private Nullable<String> setStringValue(String s) { public void setVersionProperty(Optional<String> version) { this.version = version; }
/**
* Convert a string value into a Optional<String>
*/
private Optional<String> setStringValue(String s) {
if (s == null || s.isEmpty()) { if (s == null || s.isEmpty()) {
return new Nullable<>(); return Optional.empty();
} }
return new Nullable<>(s); return Optional.of(s);
} }
/** /**
* Convert a string value represented a date into a Nullable<Date>. * Convert a string value represented a date into a Optional<Date>.
* *
* @throws InvalidFormatException * @throws InvalidFormatException
* Throws if the date format isnot valid. * Throws if the date format isnot valid.
*/ */
private Nullable<Date> setDateValue(String dateStr) throws InvalidFormatException { private Optional<Date> setDateValue(String dateStr) throws InvalidFormatException {
if (dateStr == null || dateStr.isEmpty()) { if (dateStr == null || dateStr.isEmpty()) {
return new Nullable<>(); return Optional.empty();
} }
Matcher m = TIME_ZONE_PAT.matcher(dateStr); Matcher m = TIME_ZONE_PAT.matcher(dateStr);
@ -592,7 +683,7 @@ public final class PackagePropertiesPart extends PackagePart implements
df.setTimeZone(LocaleUtil.TIMEZONE_UTC); df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
Date d = df.parse(dateTzStr, new ParsePosition(0)); Date d = df.parse(dateTzStr, new ParsePosition(0));
if (d != null) { if (d != null) {
return new Nullable<>(d); return Optional.of(d);
} }
} }
} }
@ -602,7 +693,7 @@ public final class PackagePropertiesPart extends PackagePart implements
df.setTimeZone(LocaleUtil.TIMEZONE_UTC); df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
Date d = df.parse(dateTzStr, new ParsePosition(0)); Date d = df.parse(dateTzStr, new ParsePosition(0));
if (d != null) { if (d != null) {
return new Nullable<>(d); return Optional.of(d);
} }
} }
//if you're here, no pattern matched, throw exception //if you're here, no pattern matched, throw exception
@ -622,25 +713,20 @@ public final class PackagePropertiesPart extends PackagePart implements
} }
/** /**
* Convert a Nullable<Date> into a String. * Convert a Optional<Date> into a String.
* *
* @param d * @param d
* The Date to convert. * The Date to convert.
* @return The formated date or null. * @return The formated date or null.
* @see java.text.SimpleDateFormat * @see java.text.SimpleDateFormat
*/ */
private String getDateValue(Nullable<Date> d) { private String getDateValue(Optional<Date> d) {
if (d == null) { if (d == null || !d.isPresent()) {
return ""; return "";
} }
Date date = d.getValue();
if (date == null) {
return "";
}
SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATEFORMAT, Locale.ROOT); SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATEFORMAT, Locale.ROOT);
df.setTimeZone(LocaleUtil.TIMEZONE_UTC); df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
return df.format(date); return df.format(d.get());
} }
@Override @Override

View File

@ -18,6 +18,7 @@
package org.apache.poi.openxml4j.opc.internal.marshallers; package org.apache.poi.openxml4j.opc.internal.marshallers;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Optional;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventFactory;
@ -27,7 +28,6 @@ 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.openxml4j.util.Nullable;
import org.apache.poi.ooxml.util.DocumentHelper; import org.apache.poi.ooxml.util.DocumentHelper;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -126,16 +126,16 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
/** /**
* 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, Nullable<String> property) { private Element setElementTextContent(String localName, Namespace namespace, Optional<String> property) {
return setElementTextContent(localName, namespace, property, property.getValue()); return setElementTextContent(localName, namespace, property, property.orElse(null));
} }
private String getQName(String localName, Namespace namespace) { private String getQName(String localName, Namespace namespace) {
return namespace.getPrefix().isEmpty() ? localName : namespace.getPrefix() + ':' + localName; return namespace.getPrefix().isEmpty() ? localName : namespace.getPrefix() + ':' + localName;
} }
private Element setElementTextContent(String localName, Namespace namespace, Nullable<?> property, String propertyValue) { private Element setElementTextContent(String localName, Namespace namespace, Optional<?> property, String propertyValue) {
if (!property.hasValue()) if (!property.isPresent())
return null; return null;
Element root = xmlDoc.getDocumentElement(); Element root = xmlDoc.getDocumentElement();
@ -149,7 +149,7 @@ public class PackagePropertiesMarshaller implements PartMarshaller {
return elem; return elem;
} }
private Element setElementTextContent(String localName, Namespace namespace, Nullable<?> property, String propertyValue, String xsiType) { private Element setElementTextContent(String localName, Namespace 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);

View File

@ -27,9 +27,9 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Optional;
import org.apache.poi.ooxml.POIXMLProperties.CoreProperties; import org.apache.poi.ooxml.POIXMLProperties.CoreProperties;
import org.apache.poi.openxml4j.util.Nullable;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
@ -189,7 +189,7 @@ public final class TestPOIXMLProperties {
Date dateCreated = LocaleUtil.getLocaleCalendar(2010, 6, 15, 10, 0, 0).getTime(); Date dateCreated = LocaleUtil.getLocaleCalendar(2010, 6, 15, 10, 0, 0).getTime();
cp.setCreated(new Nullable<>(dateCreated)); cp.setCreated(Optional.of(dateCreated));
assertEquals(dateCreated, cp.getCreated()); assertEquals(dateCreated, cp.getCreated());
XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc); XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc);

View File

@ -17,11 +17,6 @@
package org.apache.poi.openxml4j.opc; package org.apache.poi.openxml4j.opc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -30,17 +25,19 @@ import java.text.ParsePosition;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Optional;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.openxml4j.util.Nullable;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test; import org.junit.Test;
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty; import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
import static org.junit.Assert.*;
public final class TestPackageCoreProperties { public final class TestPackageCoreProperties {
/** /**
* Test package core properties getters. * Test package core properties getters.
@ -77,28 +74,28 @@ public final class TestPackageCoreProperties {
//test various date formats //test various date formats
props.setCreatedProperty("2007-05-12T08:00:00Z"); props.setCreatedProperty("2007-05-12T08:00:00Z");
assertEquals(dateToInsert, props.getCreatedProperty().getValue()); assertEquals(dateToInsert, props.getCreatedProperty().get());
props.setCreatedProperty("2007-05-12T08:00:00"); //no Z, assume Z props.setCreatedProperty("2007-05-12T08:00:00"); //no Z, assume Z
assertEquals(dateToInsert, props.getCreatedProperty().getValue()); assertEquals(dateToInsert, props.getCreatedProperty().get());
props.setCreatedProperty("2007-05-12T08:00:00.123Z");//millis props.setCreatedProperty("2007-05-12T08:00:00.123Z");//millis
assertEquals(msdf.parse("2007-05-12T08:00:00.123Z"), props.getCreatedProperty().getValue()); assertEquals(msdf.parse("2007-05-12T08:00:00.123Z"), props.getCreatedProperty().get());
props.setCreatedProperty("2007-05-12T10:00:00+0200"); props.setCreatedProperty("2007-05-12T10:00:00+0200");
assertEquals(dateToInsert, props.getCreatedProperty().getValue()); assertEquals(dateToInsert, props.getCreatedProperty().get());
props.setCreatedProperty("2007-05-12T10:00:00+02:00");//colon in tz props.setCreatedProperty("2007-05-12T10:00:00+02:00");//colon in tz
assertEquals(dateToInsert, props.getCreatedProperty().getValue()); assertEquals(dateToInsert, props.getCreatedProperty().get());
props.setCreatedProperty("2007-05-12T06:00:00-0200"); props.setCreatedProperty("2007-05-12T06:00:00-0200");
assertEquals(dateToInsert, props.getCreatedProperty().getValue()); assertEquals(dateToInsert, props.getCreatedProperty().get());
props.setCreatedProperty("2015-07-27"); props.setCreatedProperty("2015-07-27");
assertEquals(msdf.parse("2015-07-27T00:00:00.000Z"), props.getCreatedProperty().getValue()); assertEquals(msdf.parse("2015-07-27T00:00:00.000Z"), props.getCreatedProperty().get());
props.setCreatedProperty("2007-05-12T10:00:00.123+0200"); props.setCreatedProperty("2007-05-12T10:00:00.123+0200");
assertEquals(msdf.parse("2007-05-12T08:00:00.123Z"), props.getCreatedProperty().getValue()); assertEquals(msdf.parse("2007-05-12T08:00:00.123Z"), props.getCreatedProperty().get());
props.setCategoryProperty("MyCategory"); props.setCategoryProperty("MyCategory");
props.setContentStatusProperty("MyContentStatus"); props.setContentStatusProperty("MyContentStatus");
@ -109,8 +106,8 @@ public final class TestPackageCoreProperties {
props.setKeywordsProperty("MyKeywords"); props.setKeywordsProperty("MyKeywords");
props.setLanguageProperty("MyLanguage"); props.setLanguageProperty("MyLanguage");
props.setLastModifiedByProperty("Julien Chable"); props.setLastModifiedByProperty("Julien Chable");
props.setLastPrintedProperty(new Nullable<>(dateToInsert)); props.setLastPrintedProperty(Optional.of(dateToInsert));
props.setModifiedProperty(new Nullable<>(dateToInsert)); props.setModifiedProperty(Optional.of(dateToInsert));
props.setRevisionProperty("2"); props.setRevisionProperty("2");
props.setTitleProperty("MyTitle"); props.setTitleProperty("MyTitle");
props.setSubjectProperty("MySubject"); props.setSubjectProperty("MySubject");
@ -134,22 +131,22 @@ public final class TestPackageCoreProperties {
// Gets the core properties // Gets the core properties
PackageProperties props = p.getPackageProperties(); PackageProperties props = p.getPackageProperties();
assertEquals("MyCategory", props.getCategoryProperty().getValue()); assertEquals("MyCategory", props.getCategoryProperty().get());
assertEquals("MyContentStatus", props.getContentStatusProperty().getValue()); assertEquals("MyContentStatus", props.getContentStatusProperty().get());
assertEquals("MyContentType", props.getContentTypeProperty().getValue()); assertEquals("MyContentType", props.getContentTypeProperty().get());
assertEquals(expectedDate, props.getCreatedProperty().getValue()); assertEquals(expectedDate, props.getCreatedProperty().get());
assertEquals("MyCreator", props.getCreatorProperty().getValue()); assertEquals("MyCreator", props.getCreatorProperty().get());
assertEquals("MyDescription", props.getDescriptionProperty().getValue()); assertEquals("MyDescription", props.getDescriptionProperty().get());
assertEquals("MyIdentifier", props.getIdentifierProperty().getValue()); assertEquals("MyIdentifier", props.getIdentifierProperty().get());
assertEquals("MyKeywords", props.getKeywordsProperty().getValue()); assertEquals("MyKeywords", props.getKeywordsProperty().get());
assertEquals("MyLanguage", props.getLanguageProperty().getValue()); assertEquals("MyLanguage", props.getLanguageProperty().get());
assertEquals("Julien Chable", props.getLastModifiedByProperty().getValue()); assertEquals("Julien Chable", props.getLastModifiedByProperty().get());
assertEquals(expectedDate, props.getLastPrintedProperty().getValue()); assertEquals(expectedDate, props.getLastPrintedProperty().get());
assertEquals(expectedDate, props.getModifiedProperty().getValue()); assertEquals(expectedDate, props.getModifiedProperty().get());
assertEquals("2", props.getRevisionProperty().getValue()); assertEquals("2", props.getRevisionProperty().get());
assertEquals("MySubject", props.getSubjectProperty().getValue()); assertEquals("MySubject", props.getSubjectProperty().get());
assertEquals("MyTitle", props.getTitleProperty().getValue()); assertEquals("MyTitle", props.getTitleProperty().get());
assertEquals("2", props.getVersionProperty().getValue()); assertEquals("2", props.getVersionProperty().get());
} }
@Test @Test
@ -164,48 +161,48 @@ public final class TestPackageCoreProperties {
// created // created
assertEquals("", props.getCreatedPropertyString()); assertEquals("", props.getCreatedPropertyString());
assertNull(props.getCreatedProperty().getValue()); assertFalse(props.getCreatedProperty().isPresent());
props.setCreatedProperty((String)null); props.setCreatedProperty((String)null);
assertEquals("", props.getCreatedPropertyString()); assertEquals("", props.getCreatedPropertyString());
assertNull(props.getCreatedProperty().getValue()); assertFalse(props.getCreatedProperty().isPresent());
props.setCreatedProperty(new Nullable<>()); props.setCreatedProperty(Optional.empty());
assertEquals("", props.getCreatedPropertyString()); assertEquals("", props.getCreatedPropertyString());
assertNull(props.getCreatedProperty().getValue()); assertFalse(props.getCreatedProperty().isPresent());
props.setCreatedProperty(new Nullable<>(date)); props.setCreatedProperty(Optional.of(date));
assertEquals(strDate, props.getCreatedPropertyString()); assertEquals(strDate, props.getCreatedPropertyString());
assertEquals(date, props.getCreatedProperty().getValue()); assertEquals(date, props.getCreatedProperty().get());
props.setCreatedProperty(strDate); props.setCreatedProperty(strDate);
assertEquals(strDate, props.getCreatedPropertyString()); assertEquals(strDate, props.getCreatedPropertyString());
assertEquals(date, props.getCreatedProperty().getValue()); assertEquals(date, props.getCreatedProperty().get());
// lastPrinted // lastPrinted
assertEquals("", props.getLastPrintedPropertyString()); assertEquals("", props.getLastPrintedPropertyString());
assertNull(props.getLastPrintedProperty().getValue()); assertFalse(props.getLastPrintedProperty().isPresent());
props.setLastPrintedProperty((String)null); props.setLastPrintedProperty((String)null);
assertEquals("", props.getLastPrintedPropertyString()); assertEquals("", props.getLastPrintedPropertyString());
assertNull(props.getLastPrintedProperty().getValue()); assertFalse(props.getLastPrintedProperty().isPresent());
props.setLastPrintedProperty(new Nullable<>()); props.setLastPrintedProperty(Optional.empty());
assertEquals("", props.getLastPrintedPropertyString()); assertEquals("", props.getLastPrintedPropertyString());
assertNull(props.getLastPrintedProperty().getValue()); assertFalse(props.getLastPrintedProperty().isPresent());
props.setLastPrintedProperty(new Nullable<>(date)); props.setLastPrintedProperty(Optional.of(date));
assertEquals(strDate, props.getLastPrintedPropertyString()); assertEquals(strDate, props.getLastPrintedPropertyString());
assertEquals(date, props.getLastPrintedProperty().getValue()); assertEquals(date, props.getLastPrintedProperty().get());
props.setLastPrintedProperty(strDate); props.setLastPrintedProperty(strDate);
assertEquals(strDate, props.getLastPrintedPropertyString()); assertEquals(strDate, props.getLastPrintedPropertyString());
assertEquals(date, props.getLastPrintedProperty().getValue()); assertEquals(date, props.getLastPrintedProperty().get());
// modified // modified
assertNull(props.getModifiedProperty().getValue()); assertFalse(props.getModifiedProperty().isPresent());
props.setModifiedProperty((String)null); props.setModifiedProperty((String)null);
assertNull(props.getModifiedProperty().getValue()); assertFalse(props.getModifiedProperty().isPresent());
props.setModifiedProperty(new Nullable<>()); props.setModifiedProperty(Optional.empty());
assertNull(props.getModifiedProperty().getValue()); assertFalse(props.getModifiedProperty().isPresent());
props.setModifiedProperty(new Nullable<>(date)); props.setModifiedProperty(Optional.of(date));
assertEquals(strDate, props.getModifiedPropertyString()); assertEquals(strDate, props.getModifiedPropertyString());
assertEquals(date, props.getModifiedProperty().getValue()); assertEquals(date, props.getModifiedProperty().get());
props.setModifiedProperty(strDate); props.setModifiedProperty(strDate);
assertEquals(strDate, props.getModifiedPropertyString()); assertEquals(strDate, props.getModifiedPropertyString());
assertEquals(date, props.getModifiedProperty().getValue()); assertEquals(date, props.getModifiedProperty().get());
// Tidy // Tidy
pkg.close(); pkg.close();
@ -216,7 +213,7 @@ public final class TestPackageCoreProperties {
// Open the package // Open the package
OPCPackage pkg1 = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("51444.xlsx")); OPCPackage pkg1 = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("51444.xlsx"));
PackageProperties props1 = pkg1.getPackageProperties(); PackageProperties props1 = pkg1.getPackageProperties();
assertEquals(null, props1.getTitleProperty().getValue()); assertFalse(props1.getTitleProperty().isPresent());
props1.setTitleProperty("Bug 51444 fixed"); props1.setTitleProperty("Bug 51444 fixed");
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
pkg1.save(out); pkg1.save(out);
@ -253,7 +250,7 @@ public final class TestPackageCoreProperties {
PackagePropertiesPart props = (PackagePropertiesPart)p.getPackageProperties(); PackagePropertiesPart props = (PackagePropertiesPart)p.getPackageProperties();
// Check // Check
assertEquals("Stefan Kopf", props.getCreatorProperty().getValue()); assertEquals("Stefan Kopf", props.getCreatorProperty().get());
p.close(); p.close();
} }
@ -287,20 +284,20 @@ public final class TestPackageCoreProperties {
df.setTimeZone(LocaleUtil.TIMEZONE_UTC); df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
// Check text properties first // Check text properties first
assertEquals("Lorem Ipsum", props.getTitleProperty().getValue()); assertEquals("Lorem Ipsum", props.getTitleProperty().get());
assertEquals("Apache POI", props.getCreatorProperty().getValue()); assertEquals("Apache POI", props.getCreatorProperty().get());
// Created at has a +3 timezone and milliseconds // Created at has a +3 timezone and milliseconds
// 2006-10-13T18:06:00.123+03:00 // 2006-10-13T18:06:00.123+03:00
// = 2006-10-13T15:06:00.123+00:00 // = 2006-10-13T15:06:00.123+00:00
assertEquals("2006-10-13T15:06:00Z", props.getCreatedPropertyString()); assertEquals("2006-10-13T15:06:00Z", props.getCreatedPropertyString());
assertEquals("2006-10-13T15:06:00.123Z", df.format(props.getCreatedProperty().getValue())); assertEquals("2006-10-13T15:06:00.123Z", df.format(props.getCreatedProperty().get()));
// Modified at has a -13 timezone but no milliseconds // Modified at has a -13 timezone but no milliseconds
// 2007-06-20T07:59:00-13:00 // 2007-06-20T07:59:00-13:00
// = 2007-06-20T20:59:00-13:00 // = 2007-06-20T20:59:00-13:00
assertEquals("2007-06-20T20:59:00Z", props.getModifiedPropertyString()); assertEquals("2007-06-20T20:59:00Z", props.getModifiedPropertyString());
assertEquals("2007-06-20T20:59:00.000Z", df.format(props.getModifiedProperty().getValue())); assertEquals("2007-06-20T20:59:00.000Z", df.format(props.getModifiedProperty().get()));
// Ensure we can change them with other timezones and still read back OK // Ensure we can change them with other timezones and still read back OK
@ -312,16 +309,16 @@ public final class TestPackageCoreProperties {
pkg = OPCPackage.open(new ByteArrayInputStream(baos.toByteArray())); pkg = OPCPackage.open(new ByteArrayInputStream(baos.toByteArray()));
// Check text properties first - should be unchanged // Check text properties first - should be unchanged
assertEquals("Lorem Ipsum", props.getTitleProperty().getValue()); assertEquals("Lorem Ipsum", props.getTitleProperty().get());
assertEquals("Apache POI", props.getCreatorProperty().getValue()); assertEquals("Apache POI", props.getCreatorProperty().get());
// Check the updated times // Check the updated times
// 2007-06-20T20:57:00+13:00 // 2007-06-20T20:57:00+13:00
// = 2007-06-20T07:57:00Z // = 2007-06-20T07:57:00Z
assertEquals("2007-06-20T07:57:00.000Z", df.format(props.getCreatedProperty().getValue())); assertEquals("2007-06-20T07:57:00.000Z", df.format(props.getCreatedProperty().get()));
// 2007-06-20T20:59:00.123-13:00 // 2007-06-20T20:59:00.123-13:00
// = 2007-06-21T09:59:00.123Z // = 2007-06-21T09:59:00.123Z
assertEquals("2007-06-21T09:59:00.123Z", df.format(props.getModifiedProperty().getValue())); assertEquals("2007-06-21T09:59:00.123Z", df.format(props.getModifiedProperty().get()));
} }
} }

View File

@ -17,12 +17,6 @@
package org.apache.poi.openxml4j.opc.compliance; package org.apache.poi.openxml4j.opc.compliance;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -47,6 +41,8 @@ import org.junit.Test;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import static org.junit.Assert.*;
/** /**
* Test core properties Open Packaging Convention compliance. * Test core properties Open Packaging Convention compliance.
* *
@ -254,7 +250,7 @@ public final class TestOPCComplianceCoreProperties {
assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size()); assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties()); assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty()); assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue()); assertFalse(pkg.getPackageProperties().getLanguageProperty().isPresent());
// Save and re-load // Save and re-load
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -268,7 +264,7 @@ public final class TestOPCComplianceCoreProperties {
assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size()); assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties()); assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty()); assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue()); assertFalse(pkg.getPackageProperties().getLanguageProperty().isPresent());
pkg.close(); pkg.close();
// Open a new copy of it // Open a new copy of it
@ -286,7 +282,7 @@ public final class TestOPCComplianceCoreProperties {
assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size()); assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties()); assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty()); assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue()); assertFalse(pkg.getPackageProperties().getLanguageProperty().isPresent());
} }
/** /**
@ -314,7 +310,7 @@ public final class TestOPCComplianceCoreProperties {
assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size()); assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties()); assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty()); assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue()); assertFalse(pkg.getPackageProperties().getLanguageProperty().isPresent());
// Save and close // Save and close
pkg.close(); pkg.close();
@ -326,7 +322,7 @@ public final class TestOPCComplianceCoreProperties {
assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size()); assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties()); assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty()); assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue()); assertFalse(pkg.getPackageProperties().getLanguageProperty().isPresent());
// Finish and tidy // Finish and tidy
pkg.revert(); pkg.revert();

View File

@ -16,12 +16,6 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.poifs.crypt; package org.apache.poi.poifs.crypt;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -36,7 +30,6 @@ import javax.crypto.Cipher;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.opc.ContentTypes; import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.agile.AgileDecryptor; import org.apache.poi.poifs.crypt.agile.AgileDecryptor;
import org.apache.poi.poifs.crypt.agile.AgileEncryptionHeader; import org.apache.poi.poifs.crypt.agile.AgileEncryptionHeader;
import org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier; import org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier;
@ -54,6 +47,8 @@ import org.junit.Assume;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*;
public class TestEncryptor { public class TestEncryptor {
@Test @Test
public void binaryRC4Encryption() throws Exception { public void binaryRC4Encryption() throws Exception {
@ -295,7 +290,7 @@ public class TestEncryptor {
assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size()); assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties()); assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty()); assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue()); assertFalse(pkg.getPackageProperties().getLanguageProperty().isPresent());
// Encrypt it // Encrypt it
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
@ -327,7 +322,7 @@ public class TestEncryptor {
assertEquals(1, inpPkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size()); assertEquals(1, inpPkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(inpPkg.getPackageProperties()); assertNotNull(inpPkg.getPackageProperties());
assertNotNull(inpPkg.getPackageProperties().getLanguageProperty()); assertNotNull(inpPkg.getPackageProperties().getLanguageProperty());
assertNull(inpPkg.getPackageProperties().getLanguageProperty().getValue()); assertFalse(inpPkg.getPackageProperties().getLanguageProperty().isPresent());
} }
} }

View File

@ -16,12 +16,6 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xslf; package org.apache.poi.xslf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.awt.Color; import java.awt.Color;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.io.IOException; import java.io.IOException;
@ -45,6 +39,8 @@ import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProp
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry; import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry; import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry;
import static org.junit.Assert.*;
public class TestXSLFSlideShow { public class TestXSLFSlideShow {
private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
private OPCPackage pack; private OPCPackage pack;
@ -130,7 +126,7 @@ public class TestXSLFSlideShow {
CoreProperties cprops = xml.getProperties().getCoreProperties(); CoreProperties cprops = xml.getProperties().getCoreProperties();
assertNull(cprops.getTitle()); assertNull(cprops.getTitle());
assertNull(cprops.getUnderlyingProperties().getSubjectProperty().getValue()); assertFalse(cprops.getUnderlyingProperties().getSubjectProperty().isPresent());
xml.close(); xml.close();
} }

View File

@ -137,7 +137,7 @@ public class TestXMLSlideShow extends BaseTestSlideShow {
assertEquals(0, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines()); assertEquals(0, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines());
assertEquals(null, xml.getProperties().getCoreProperties().getTitle()); assertEquals(null, xml.getProperties().getCoreProperties().getTitle());
assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); assertFalse(xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().isPresent());
xml.close(); xml.close();
} }

View File

@ -324,14 +324,14 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
assertNotNull(opcProps); assertNotNull(opcProps);
opcProps.setTitleProperty("Testing Bugzilla #47460"); opcProps.setTitleProperty("Testing Bugzilla #47460");
assertEquals("Apache POI", opcProps.getCreatorProperty().getValue()); assertEquals("Apache POI", opcProps.getCreatorProperty().get());
opcProps.setCreatorProperty("poi-dev@poi.apache.org"); opcProps.setCreatorProperty("poi-dev@poi.apache.org");
XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(workbook); XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(workbook);
assertEquals("Apache POI", wbBack.getProperties().getExtendedProperties().getUnderlyingProperties().getApplication()); assertEquals("Apache POI", wbBack.getProperties().getExtendedProperties().getUnderlyingProperties().getApplication());
opcProps = wbBack.getProperties().getCoreProperties().getUnderlyingProperties(); opcProps = wbBack.getProperties().getCoreProperties().getUnderlyingProperties();
assertEquals("Testing Bugzilla #47460", opcProps.getTitleProperty().getValue()); assertEquals("Testing Bugzilla #47460", opcProps.getTitleProperty().get());
assertEquals("poi-dev@poi.apache.org", opcProps.getCreatorProperty().getValue()); assertEquals("poi-dev@poi.apache.org", opcProps.getCreatorProperty().get());
wbBack.close(); wbBack.close();
} }
} }

View File

@ -71,24 +71,24 @@ public final class TestXWPFDocument {
@Test @Test
public void testOpen() throws Exception { public void testOpen() throws Exception {
// Simple file // Simple file
XWPFDocument xml1 = XWPFTestDataSamples.openSampleDocument("sample.docx"); try (XWPFDocument xml1 = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
// Check it has key parts // Check it has key parts
assertNotNull(xml1.getDocument()); assertNotNull(xml1.getDocument());
assertNotNull(xml1.getDocument().getBody()); assertNotNull(xml1.getDocument().getBody());
assertNotNull(xml1.getStyle()); assertNotNull(xml1.getStyle());
xml1.close(); }
// Complex file // Complex file
XWPFDocument xml2 = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); try (XWPFDocument xml2 = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx")) {
assertNotNull(xml2.getDocument()); assertNotNull(xml2.getDocument());
assertNotNull(xml2.getDocument().getBody()); assertNotNull(xml2.getDocument().getBody());
assertNotNull(xml2.getStyle()); assertNotNull(xml2.getStyle());
xml2.close(); }
} }
@Test @Test
public void testMetadataBasics() throws IOException { public void testMetadataBasics() throws IOException {
XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); try (XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
assertNotNull(xml.getProperties().getCoreProperties()); assertNotNull(xml.getProperties().getCoreProperties());
assertNotNull(xml.getProperties().getExtendedProperties()); assertNotNull(xml.getProperties().getExtendedProperties());
@ -97,8 +97,8 @@ public final class TestXWPFDocument {
assertEquals(10, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines()); assertEquals(10, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines());
assertEquals(null, xml.getProperties().getCoreProperties().getTitle()); assertEquals(null, xml.getProperties().getCoreProperties().getTitle());
assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); assertFalse(xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().isPresent());
xml.close(); }
} }
@Test @Test
@ -112,7 +112,7 @@ public final class TestXWPFDocument {
assertEquals(0, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines()); assertEquals(0, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines());
assertEquals(" ", xml.getProperties().getCoreProperties().getTitle()); assertEquals(" ", xml.getProperties().getCoreProperties().getTitle());
assertEquals(" ", xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); assertEquals(" ", xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().get());
xml.close(); xml.close();
} }