Search in sources :

Example 1 with ZipContentTypeManager

use of org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager in project poi by apache.

the class ZipPackage method getPartsImpl.

/**
     * Retrieves the parts from this package. We assume that the package has not
     * been yet inspect to retrieve all the parts, this method will open the
     * archive and look for all parts contain inside it. If the package part
     * list is not empty, it will be emptied.
     *
     * @return All parts contain in this package.
     * @throws InvalidFormatException if the package is not valid.
     */
@Override
protected PackagePart[] getPartsImpl() throws InvalidFormatException {
    if (this.partList == null) {
        // The package has just been created, we create an empty part
        // list.
        this.partList = new PackagePartCollection();
    }
    if (this.zipArchive == null) {
        return this.partList.sortedValues().toArray(new PackagePart[this.partList.size()]);
    }
    // First we need to parse the content type part
    Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.getName().equalsIgnoreCase(ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
            try {
                this.contentTypeManager = new ZipContentTypeManager(getZipArchive().getInputStream(entry), this);
            } catch (IOException e) {
                throw new InvalidFormatException(e.getMessage(), e);
            }
            break;
        }
    }
    // At this point, we should have loaded the content type part
    if (this.contentTypeManager == null) {
        // Is it a different Zip-based format?
        int numEntries = 0;
        boolean hasMimetype = false;
        boolean hasSettingsXML = false;
        entries = this.zipArchive.getEntries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            if (MIMETYPE.equals(name)) {
                hasMimetype = true;
            }
            if (SETTINGS_XML.equals(name)) {
                hasSettingsXML = true;
            }
            numEntries++;
        }
        if (hasMimetype && hasSettingsXML) {
            throw new ODFNotOfficeXmlFileException("The supplied data appears to be in ODF (Open Document) Format. " + "Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit");
        }
        if (numEntries == 0) {
            throw new NotOfficeXmlFileException("No valid entries or contents found, this is not a valid OOXML " + "(Office Open XML) file");
        }
        // Fallback exception
        throw new InvalidFormatException("Package should contain a content type part [M1.13]");
    }
    // Now create all the relationships
    // (Need to create relationships before other
    //  parts, otherwise we might create a part before
    //  its relationship exists, and then it won't tie up)
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        PackagePartName partName = buildPartName(entry);
        if (partName == null) {
            continue;
        }
        // Only proceed for Relationships at this stage
        String contentType = contentTypeManager.getContentType(partName);
        if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
            try {
                PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
                partList.put(partName, part);
            } catch (InvalidOperationException e) {
                throw new InvalidFormatException(e.getMessage(), e);
            }
        }
    }
    // Then we can go through all the other parts
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        PackagePartName partName = buildPartName(entry);
        if (partName == null) {
            continue;
        }
        String contentType = contentTypeManager.getContentType(partName);
        if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
        // Already handled
        } else if (contentType != null) {
            try {
                PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
                partList.put(partName, part);
            } catch (InvalidOperationException e) {
                throw new InvalidFormatException(e.getMessage(), e);
            }
        } else {
            throw new InvalidFormatException("The part " + partName.getURI().getPath() + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
        }
    }
    return partList.sortedValues().toArray(new PackagePart[partList.size()]);
}
Also used : ODFNotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException) NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ZipContentTypeManager(org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) ODFNotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException)

Example 2 with ZipContentTypeManager

use of org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager in project poi by apache.

the class OPCPackage method configurePackage.

private static void configurePackage(OPCPackage pkg) {
    try {
        // Content type manager
        pkg.contentTypeManager = new ZipContentTypeManager(null, pkg);
        // Add default content types for .xml and .rels
        pkg.contentTypeManager.addContentType(PackagingURIHelper.createPartName(PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_URI), ContentTypes.RELATIONSHIPS_PART);
        pkg.contentTypeManager.addContentType(PackagingURIHelper.createPartName("/default.xml"), ContentTypes.PLAIN_OLD_XML);
        // Initialise some PackageBase properties
        pkg.packageProperties = new PackagePropertiesPart(pkg, PackagingURIHelper.CORE_PROPERTIES_PART_NAME);
        pkg.packageProperties.setCreatorProperty("Generated by Apache POI OpenXML4J");
        pkg.packageProperties.setCreatedProperty(new Nullable<Date>(new Date()));
    } catch (InvalidFormatException e) {
        // Should never happen
        throw new IllegalStateException(e);
    }
}
Also used : PackagePropertiesPart(org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart) ZipContentTypeManager(org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Date(java.util.Date)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 ZipContentTypeManager (org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager)2 IOException (java.io.IOException)1 Date (java.util.Date)1 ZipEntry (java.util.zip.ZipEntry)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 NotOfficeXmlFileException (org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException)1 ODFNotOfficeXmlFileException (org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException)1 MemoryPackagePart (org.apache.poi.openxml4j.opc.internal.MemoryPackagePart)1 PackagePropertiesPart (org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart)1