Search in sources :

Example 1 with PartMarshaller

use of org.apache.poi.openxml4j.opc.internal.PartMarshaller 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)

Example 2 with PartMarshaller

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

the class ZipPackage method saveImpl.

/**
	 * Save this package into the specified stream
	 *
	 *
	 * @param outputStream
	 *            The stream use to save this package.
	 *
	 * @see #save(OutputStream)
	 */
@Override
public void saveImpl(OutputStream outputStream) {
    // Check that the document was open in write mode
    throwExceptionIfReadOnly();
    final ZipOutputStream zos;
    try {
        if (!(outputStream instanceof ZipOutputStream)) {
            zos = new ZipOutputStream(outputStream);
        } else {
            zos = (ZipOutputStream) outputStream;
        }
        // we save it as well
        if (this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size() == 0 && this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376).size() == 0) {
            LOG.log(POILogger.DEBUG, "Save core properties part");
            // Ensure that core properties are added if missing
            getPackageProperties();
            // Add core properties to part list ...
            addPackagePart(this.packageProperties);
            // ... and to add its relationship ...
            this.relationships.addRelationship(this.packageProperties.getPartName().getURI(), TargetMode.INTERNAL, PackageRelationshipTypes.CORE_PROPERTIES, null);
            // ... and the content if it has not been added yet.
            if (!this.contentTypeManager.isContentTypeRegister(ContentTypes.CORE_PROPERTIES_PART)) {
                this.contentTypeManager.addContentType(this.packageProperties.getPartName(), ContentTypes.CORE_PROPERTIES_PART);
            }
        }
        // Save package relationships part.
        LOG.log(POILogger.DEBUG, "Save package relationships");
        ZipPartMarshaller.marshallRelationshipPart(this.getRelationships(), PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME, zos);
        // Save content type part.
        LOG.log(POILogger.DEBUG, "Save content types part");
        this.contentTypeManager.save(zos);
        // Save parts.
        for (PackagePart part : getParts()) {
            // the source part that will do the job.
            if (part.isRelationshipPart()) {
                continue;
            }
            final PackagePartName ppn = part.getPartName();
            LOG.log(POILogger.DEBUG, "Save part '" + ZipHelper.getZipItemNameFromOPCName(ppn.getName()) + "'");
            PartMarshaller marshaller = partMarshallers.get(part._contentType);
            String errMsg = "The part " + ppn.getURI() + " failed to be saved in the stream with marshaller ";
            if (marshaller != null) {
                if (!marshaller.marshall(part, zos)) {
                    throw new OpenXML4JException(errMsg + marshaller);
                }
            } else {
                if (!defaultPartMarshaller.marshall(part, zos)) {
                    throw new OpenXML4JException(errMsg + defaultPartMarshaller);
                }
            }
        }
        zos.close();
    } catch (OpenXML4JRuntimeException e) {
        // no need to wrap this type of Exception
        throw e;
    } catch (Exception e) {
        throw new OpenXML4JRuntimeException("Fail to save: an error occurs while saving the package : " + e.getMessage(), e);
    }
}
Also used : OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) PartMarshaller(org.apache.poi.openxml4j.opc.internal.PartMarshaller) ZipPartMarshaller(org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller) ZipOutputStream(java.util.zip.ZipOutputStream) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) ODFNotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException) NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) IOException(java.io.IOException) OpenXML4JRuntimeException(org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) FileNotFoundException(java.io.FileNotFoundException) OpenXML4JRuntimeException(org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 OpenXML4JRuntimeException (org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException)2 PartMarshaller (org.apache.poi.openxml4j.opc.internal.PartMarshaller)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ZipOutputStream (java.util.zip.ZipOutputStream)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 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)1 ContentType (org.apache.poi.openxml4j.opc.internal.ContentType)1 MemoryPackagePart (org.apache.poi.openxml4j.opc.internal.MemoryPackagePart)1 PartUnmarshaller (org.apache.poi.openxml4j.opc.internal.PartUnmarshaller)1 DefaultMarshaller (org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller)1 ZipPackagePropertiesMarshaller (org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller)1 ZipPartMarshaller (org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller)1 PackagePropertiesUnmarshaller (org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller)1