Search in sources :

Example 6 with PackagePropertiesPart

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

Example 7 with PackagePropertiesPart

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

the class POIXMLPropertiesTextExtractor method getCorePropertiesText.

/**
	 * Returns the core document properties, eg author
	 * 
	 * @return the core document properties
	 */
@SuppressWarnings("resource")
public String getCorePropertiesText() {
    POIXMLDocument document = getDocument();
    if (document == null) {
        // event based extractor does not have a document
        return "";
    }
    StringBuffer text = new StringBuffer();
    PackagePropertiesPart props = document.getProperties().getCoreProperties().getUnderlyingProperties();
    appendIfPresent(text, "Category", props.getCategoryProperty().getValue());
    appendIfPresent(text, "Category", props.getCategoryProperty().getValue());
    appendIfPresent(text, "ContentStatus", props.getContentStatusProperty().getValue());
    appendIfPresent(text, "ContentType", props.getContentTypeProperty().getValue());
    appendIfPresent(text, "Created", props.getCreatedProperty().getValue());
    appendIfPresent(text, "CreatedString", props.getCreatedPropertyString());
    appendIfPresent(text, "Creator", props.getCreatorProperty().getValue());
    appendIfPresent(text, "Description", props.getDescriptionProperty().getValue());
    appendIfPresent(text, "Identifier", props.getIdentifierProperty().getValue());
    appendIfPresent(text, "Keywords", props.getKeywordsProperty().getValue());
    appendIfPresent(text, "Language", props.getLanguageProperty().getValue());
    appendIfPresent(text, "LastModifiedBy", props.getLastModifiedByProperty().getValue());
    appendIfPresent(text, "LastPrinted", props.getLastPrintedProperty().getValue());
    appendIfPresent(text, "LastPrintedString", props.getLastPrintedPropertyString());
    appendIfPresent(text, "Modified", props.getModifiedProperty().getValue());
    appendIfPresent(text, "ModifiedString", props.getModifiedPropertyString());
    appendIfPresent(text, "Revision", props.getRevisionProperty().getValue());
    appendIfPresent(text, "Subject", props.getSubjectProperty().getValue());
    appendIfPresent(text, "Title", props.getTitleProperty().getValue());
    appendIfPresent(text, "Version", props.getVersionProperty().getValue());
    return text.toString();
}
Also used : PackagePropertiesPart(org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart)

Example 8 with PackagePropertiesPart

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

Example 9 with PackagePropertiesPart

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

the class TestPackageCoreProperties method testCoreProperties_bug51374.

@Test
public void testCoreProperties_bug51374() throws Exception {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT);
    df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
    String strDate = "2007-05-12T08:00:00Z";
    Date date = df.parse(strDate);
    OPCPackage pkg = new ZipPackage();
    PackagePropertiesPart props = (PackagePropertiesPart) pkg.getPackageProperties();
    // created
    assertEquals("", props.getCreatedPropertyString());
    assertNull(props.getCreatedProperty().getValue());
    props.setCreatedProperty((String) null);
    assertEquals("", props.getCreatedPropertyString());
    assertNull(props.getCreatedProperty().getValue());
    props.setCreatedProperty(new Nullable<Date>());
    assertEquals("", props.getCreatedPropertyString());
    assertNull(props.getCreatedProperty().getValue());
    props.setCreatedProperty(new Nullable<Date>(date));
    assertEquals(strDate, props.getCreatedPropertyString());
    assertEquals(date, props.getCreatedProperty().getValue());
    props.setCreatedProperty(strDate);
    assertEquals(strDate, props.getCreatedPropertyString());
    assertEquals(date, props.getCreatedProperty().getValue());
    // lastPrinted
    assertEquals("", props.getLastPrintedPropertyString());
    assertNull(props.getLastPrintedProperty().getValue());
    props.setLastPrintedProperty((String) null);
    assertEquals("", props.getLastPrintedPropertyString());
    assertNull(props.getLastPrintedProperty().getValue());
    props.setLastPrintedProperty(new Nullable<Date>());
    assertEquals("", props.getLastPrintedPropertyString());
    assertNull(props.getLastPrintedProperty().getValue());
    props.setLastPrintedProperty(new Nullable<Date>(date));
    assertEquals(strDate, props.getLastPrintedPropertyString());
    assertEquals(date, props.getLastPrintedProperty().getValue());
    props.setLastPrintedProperty(strDate);
    assertEquals(strDate, props.getLastPrintedPropertyString());
    assertEquals(date, props.getLastPrintedProperty().getValue());
    // modified
    assertNull(props.getModifiedProperty().getValue());
    props.setModifiedProperty((String) null);
    assertNull(props.getModifiedProperty().getValue());
    props.setModifiedProperty(new Nullable<Date>());
    assertNull(props.getModifiedProperty().getValue());
    props.setModifiedProperty(new Nullable<Date>(date));
    assertEquals(strDate, props.getModifiedPropertyString());
    assertEquals(date, props.getModifiedProperty().getValue());
    props.setModifiedProperty(strDate);
    assertEquals(strDate, props.getModifiedPropertyString());
    assertEquals(date, props.getModifiedProperty().getValue());
    // Tidy
    pkg.close();
}
Also used : PackagePropertiesPart(org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 10 with PackagePropertiesPart

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

the class TestXSSFWorkbook method workbookProperties.

/**
     *  Test setting of core properties such as Title and Author
     * @throws IOException
     */
@Test
public void workbookProperties() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    try {
        POIXMLProperties props = workbook.getProperties();
        assertNotNull(props);
        //the Application property must be set for new workbooks, see Bugzilla #47559
        assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication());
        PackagePropertiesPart opcProps = props.getCoreProperties().getUnderlyingProperties();
        assertNotNull(opcProps);
        opcProps.setTitleProperty("Testing Bugzilla #47460");
        assertEquals("Apache POI", opcProps.getCreatorProperty().getValue());
        opcProps.setCreatorProperty("poi-dev@poi.apache.org");
        XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(workbook);
        assertEquals("Apache POI", wbBack.getProperties().getExtendedProperties().getUnderlyingProperties().getApplication());
        opcProps = wbBack.getProperties().getCoreProperties().getUnderlyingProperties();
        assertEquals("Testing Bugzilla #47460", opcProps.getTitleProperty().getValue());
        assertEquals("poi-dev@poi.apache.org", opcProps.getCreatorProperty().getValue());
        wbBack.close();
    } finally {
        workbook.close();
    }
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) PackagePropertiesPart(org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart) Test(org.junit.Test)

Aggregations

PackagePropertiesPart (org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart)10 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 POIXMLProperties (org.apache.poi.POIXMLProperties)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 ZipPackage (org.apache.poi.openxml4j.opc.ZipPackage)1 PartUnmarshaller (org.apache.poi.openxml4j.opc.internal.PartUnmarshaller)1 ZipContentTypeManager (org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager)1 UnmarshallContext (org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 SAXException (org.xml.sax.SAXException)1