Search in sources :

Example 1 with ZipPackage

use of org.apache.poi.openxml4j.opc.ZipPackage in project poi by apache.

the class PackagePropertiesUnmarshaller method unmarshall.

// TODO Load element with XMLBeans or dynamic table
// TODO Check every element/namespace for compliance
public PackagePart unmarshall(UnmarshallContext context, InputStream in) throws InvalidFormatException, IOException {
    PackagePropertiesPart coreProps = new PackagePropertiesPart(context.getPackage(), context.getPartName());
    // package.
    if (in == null) {
        if (context.getZipEntry() != null) {
            in = ((ZipPackage) context.getPackage()).getZipArchive().getInputStream(context.getZipEntry());
        } else if (context.getPackage() != null) {
            // Try to retrieve the part inputstream from the URI
            ZipEntry zipEntry = ZipHelper.getCorePropertiesZipEntry((ZipPackage) context.getPackage());
            in = ((ZipPackage) context.getPackage()).getZipArchive().getInputStream(zipEntry);
        } else
            throw new IOException("Error while trying to get the part input stream.");
    }
    Document xmlDoc;
    try {
        xmlDoc = DocumentHelper.readDocument(in);
        /* Check OPC compliance */
        // Rule M4.2, M4.3, M4.4 and M4.5/
        checkElementForOPCCompliance(xmlDoc.getDocumentElement());
    /* End OPC compliance */
    } catch (SAXException e) {
        throw new IOException(e.getMessage());
    }
    coreProps.setCategoryProperty(loadCategory(xmlDoc));
    coreProps.setContentStatusProperty(loadContentStatus(xmlDoc));
    coreProps.setContentTypeProperty(loadContentType(xmlDoc));
    coreProps.setCreatedProperty(loadCreated(xmlDoc));
    coreProps.setCreatorProperty(loadCreator(xmlDoc));
    coreProps.setDescriptionProperty(loadDescription(xmlDoc));
    coreProps.setIdentifierProperty(loadIdentifier(xmlDoc));
    coreProps.setKeywordsProperty(loadKeywords(xmlDoc));
    coreProps.setLanguageProperty(loadLanguage(xmlDoc));
    coreProps.setLastModifiedByProperty(loadLastModifiedBy(xmlDoc));
    coreProps.setLastPrintedProperty(loadLastPrinted(xmlDoc));
    coreProps.setModifiedProperty(loadModified(xmlDoc));
    coreProps.setRevisionProperty(loadRevision(xmlDoc));
    coreProps.setSubjectProperty(loadSubject(xmlDoc));
    coreProps.setTitleProperty(loadTitle(xmlDoc));
    coreProps.setVersionProperty(loadVersion(xmlDoc));
    return coreProps;
}
Also used : ZipPackage(org.apache.poi.openxml4j.opc.ZipPackage) ZipEntry(java.util.zip.ZipEntry) PackagePropertiesPart(org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipPackage (org.apache.poi.openxml4j.opc.ZipPackage)1 PackagePropertiesPart (org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart)1 Document (org.w3c.dom.Document)1 SAXException (org.xml.sax.SAXException)1