Try to make a few OPCPackage error messages more helpful, and slightly reform the configure code block to make it hopefully easier to read

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1540877 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2013-11-11 23:03:00 +00:00
parent b524036bd4
commit b22e5b830b
1 changed files with 22 additions and 19 deletions

View File

@ -52,8 +52,8 @@ import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMar
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.Nullable;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* Represents a container that can store multiple data objects. * Represents a container that can store multiple data objects.
@ -214,9 +214,12 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
*/ */
public static OPCPackage open(String path, PackageAccess access) public static OPCPackage open(String path, PackageAccess access)
throws InvalidFormatException { throws InvalidFormatException {
if (path == null || "".equals(path.trim()) if (path == null || "".equals(path.trim()))
|| (new File(path).exists() && new File(path).isDirectory())) throw new IllegalArgumentException("'path' must be given");
throw new IllegalArgumentException("path");
File file = new File(path);
if (file.exists() && file.isDirectory())
throw new IllegalArgumentException("path must not be a directory");
OPCPackage pack = new ZipPackage(path, access); OPCPackage pack = new ZipPackage(path, access);
if (pack.partList == null && access != PackageAccess.WRITE) { if (pack.partList == null && access != PackageAccess.WRITE) {
@ -240,8 +243,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
*/ */
public static OPCPackage open(File file, PackageAccess access) public static OPCPackage open(File file, PackageAccess access)
throws InvalidFormatException { throws InvalidFormatException {
if (file == null|| (file.exists() && file.isDirectory())) if (file == null)
throw new IllegalArgumentException("file"); throw new IllegalArgumentException("'file' must be given");
if (file == null || (file.exists() && file.isDirectory()))
throw new IllegalArgumentException("file must not be a directory");
OPCPackage pack = new ZipPackage(file, access); OPCPackage pack = new ZipPackage(file, access);
if (pack.partList == null && access != PackageAccess.WRITE) { if (pack.partList == null && access != PackageAccess.WRITE) {
@ -346,23 +351,21 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
try { try {
// Content type manager // Content type manager
pkg.contentTypeManager = new ZipContentTypeManager(null, pkg); pkg.contentTypeManager = new ZipContentTypeManager(null, pkg);
// Add default content types for .xml and .rels // Add default content types for .xml and .rels
pkg.contentTypeManager pkg.contentTypeManager.addContentType(
.addContentType( PackagingURIHelper.createPartName(
PackagingURIHelper PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_URI),
.createPartName(PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_URI), ContentTypes.RELATIONSHIPS_PART);
ContentTypes.RELATIONSHIPS_PART); pkg.contentTypeManager.addContentType(
pkg.contentTypeManager PackagingURIHelper.createPartName("/default.xml"),
.addContentType(PackagingURIHelper ContentTypes.PLAIN_OLD_XML);
.createPartName("/default.xml"),
ContentTypes.PLAIN_OLD_XML);
// Init some PackageBase properties // Initialise some PackageBase properties
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 OpenXML4J"); pkg.packageProperties.setCreatorProperty("Generated by Apache POI OpenXML4J");
pkg.packageProperties.setCreatedProperty(new Nullable<Date>( pkg.packageProperties.setCreatedProperty(new Nullable<Date>(new Date()));
new Date()));
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
// Should never happen // Should never happen
throw new IllegalStateException(e); throw new IllegalStateException(e);