Search in sources :

Example 1 with PartUnmarshaller

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

the class OPCPackage method getParts.

/**
	 * Load the parts of the archive if it has not been done yet. The
	 * relationships of each part are not loaded.
	 * 
	 * Note - Rule M4.1 states that there may only ever be one Core
	 *  Properties Part, but Office produced files will sometimes
	 *  have multiple! As Office ignores all but the first, we relax
	 *  Compliance with Rule M4.1, and ignore all others silently too. 
	 *
	 * @return All this package's parts.
	 * @throws InvalidFormatException if the package is not valid.
	 */
public ArrayList<PackagePart> getParts() throws InvalidFormatException {
    throwExceptionIfWriteOnly();
    // If the part list is null, we parse the package to retrieve all parts.
    if (partList == null) {
        /* Variables use to validate OPC Compliance */
        // Check rule M4.1 -> A format consumer shall consider more than
        // one core properties relationship for a package to be an error
        // (We just log it and move on, as real files break this!)
        boolean hasCorePropertiesPart = false;
        boolean needCorePropertiesPart = true;
        PackagePart[] parts = this.getPartsImpl();
        this.partList = new PackagePartCollection();
        for (PackagePart part : parts) {
            if (partList.containsKey(part._partName)) {
                throw new InvalidFormatException("A part with the name '" + part._partName + "' already exist : Packages shall not contain equivalent " + "part names and package implementers shall neither create " + "nor recognize packages with equivalent part names. [M1.12]");
            }
            // Check OPC compliance rule M4.1
            if (part.getContentType().equals(ContentTypes.CORE_PROPERTIES_PART)) {
                if (!hasCorePropertiesPart) {
                    hasCorePropertiesPart = true;
                } else {
                    logger.log(POILogger.WARN, "OPC Compliance error [M4.1]: " + "there is more than one core properties relationship in the package! " + "POI will use only the first, but other software may reject this file.");
                }
            }
            PartUnmarshaller partUnmarshaller = partUnmarshallers.get(part._contentType);
            if (partUnmarshaller != null) {
                UnmarshallContext context = new UnmarshallContext(this, part._partName);
                try {
                    PackagePart unmarshallPart = partUnmarshaller.unmarshall(context, part.getInputStream());
                    partList.put(unmarshallPart._partName, unmarshallPart);
                    // and ignore any subsequent ones
                    if (unmarshallPart instanceof PackagePropertiesPart && hasCorePropertiesPart && needCorePropertiesPart) {
                        this.packageProperties = (PackagePropertiesPart) unmarshallPart;
                        needCorePropertiesPart = false;
                    }
                } catch (IOException ioe) {
                    logger.log(POILogger.WARN, "Unmarshall operation : IOException for " + part._partName);
                    continue;
                } catch (InvalidOperationException invoe) {
                    throw new InvalidFormatException(invoe.getMessage(), invoe);
                }
            } else {
                try {
                    partList.put(part._partName, part);
                } catch (InvalidOperationException e) {
                    throw new InvalidFormatException(e.getMessage(), e);
                }
            }
        }
    }
    return new ArrayList<PackagePart>(partList.sortedValues());
}
Also used : UnmarshallContext(org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext) PartUnmarshaller(org.apache.poi.openxml4j.opc.internal.PartUnmarshaller) PackagePropertiesPart(org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 2 with PartUnmarshaller

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

the class OPCPackage method init.

/**
	 * Initialize the package instance.
	 */
private void init() {
    this.partMarshallers = new HashMap<ContentType, PartMarshaller>(5);
    this.partUnmarshallers = new HashMap<ContentType, PartUnmarshaller>(2);
    try {
        // Add 'default' unmarshaller
        this.partUnmarshallers.put(new ContentType(ContentTypes.CORE_PROPERTIES_PART), new PackagePropertiesUnmarshaller());
        // Add default marshaller
        this.defaultPartMarshaller = new DefaultMarshaller();
        // TODO Delocalize specialized marshallers
        this.partMarshallers.put(new ContentType(ContentTypes.CORE_PROPERTIES_PART), new ZipPackagePropertiesMarshaller());
    } catch (InvalidFormatException e) {
        // Should never happen
        throw new OpenXML4JRuntimeException("Package.init() : this exception should never happen, " + "if you read this message please send a mail to the developers team. : " + e.getMessage(), e);
    }
}
Also used : DefaultMarshaller(org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller) ContentType(org.apache.poi.openxml4j.opc.internal.ContentType) PartMarshaller(org.apache.poi.openxml4j.opc.internal.PartMarshaller) PartUnmarshaller(org.apache.poi.openxml4j.opc.internal.PartUnmarshaller) PackagePropertiesUnmarshaller(org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller) ZipPackagePropertiesMarshaller(org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JRuntimeException(org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 PartUnmarshaller (org.apache.poi.openxml4j.opc.internal.PartUnmarshaller)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 OpenXML4JRuntimeException (org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException)1 ContentType (org.apache.poi.openxml4j.opc.internal.ContentType)1 PackagePropertiesPart (org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart)1 PartMarshaller (org.apache.poi.openxml4j.opc.internal.PartMarshaller)1 DefaultMarshaller (org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller)1 ZipPackagePropertiesMarshaller (org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller)1 PackagePropertiesUnmarshaller (org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller)1 UnmarshallContext (org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext)1