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);
}
}
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);
}
}
Aggregations