Example usage for org.apache.poi.openxml4j.opc OPCPackage getPackageProperties

List of usage examples for org.apache.poi.openxml4j.opc OPCPackage getPackageProperties

Introduction

In this page you can find the example usage for org.apache.poi.openxml4j.opc OPCPackage getPackageProperties.

Prototype

public PackageProperties getPackageProperties() throws InvalidFormatException 

Source Link

Document

Retrieves or creates if none exists, core package property part.

Usage

From source file:net.sourceforge.docfetcher.model.parse.MSOffice2007Parser.java

License:Open Source License

private static ParseResult doParse(File file, PackageAccess access) throws ParseException {
    OPCPackage pkg = null;
    try {/*from  w w  w .ja v  a  2  s  .c o  m*/
        pkg = OPCPackage.open(file.getPath(), access);
        String contents = extractText(pkg);

        // Open properties
        PackageProperties props = pkg.getPackageProperties();

        // Get author(s)
        String author = null;
        String defaultAuthor = props.getCreatorProperty().getValue();
        String lastAuthor = props.getLastModifiedByProperty().getValue();
        if (defaultAuthor == null) {
            if (lastAuthor != null)
                author = lastAuthor;
        } else if (lastAuthor == null) {
            author = defaultAuthor;
        } else {
            if (defaultAuthor.equals(lastAuthor))
                author = defaultAuthor;
            else
                author = defaultAuthor + ", " + lastAuthor; //$NON-NLS-1$
        }

        // Get other metadata
        String description = props.getDescriptionProperty().getValue();
        String keywords = props.getKeywordsProperty().getValue();
        String subject = props.getSubjectProperty().getValue();
        String title = props.getTitleProperty().getValue();

        return new ParseResult(contents).setTitle(title).addAuthor(author).addMiscMetadata(description)
                .addMiscMetadata(keywords).addMiscMetadata(subject);
    } catch (Exception e) {
        throw new ParseException(e);
    } finally {
        Closeables.closeQuietly(pkg);
    }
}

From source file:net.sourceforge.docfetcher.model.parse.TestParseFromZip.java

License:Open Source License

@Test
public void testZippedOffice2007() throws Exception {
    new ZipAndRun(TestFiles.docx) {
        protected void handleInputStream(InputStream in) throws Exception {
            int length = ExtractorFactory.createExtractor(in).getText().length();
            assertEquals(659, length);/*w ww. j ava2  s  .  c  o m*/
        }
    };
    new ZipAndRun(TestFiles.docx) {
        protected void handleInputStream(InputStream in) throws Exception {
            OPCPackage pkg = OPCPackage.open(in);
            pkg.getPackageProperties();
            Closeables.closeQuietly(pkg);
        }
    };
}

From source file:net.sourceforge.docfetcher.model.parse.TestParseFromZip.java

License:Open Source License

@Test(expected = IOException.class)
public void testZippedOffice2007Fail() throws Exception {
    // This will fail because we're trying to read the same InputStream twice
    new ZipAndRun(TestFiles.docx) {
        protected void handleInputStream(InputStream in) throws Exception {
            int length = ExtractorFactory.createExtractor(in).getText().length();
            assertEquals(659, length);//ww  w. j  a  va  2s.c o  m
            OPCPackage pkg = OPCPackage.open(in);
            pkg.getPackageProperties();
            Closeables.closeQuietly(pkg);
        }
    };
}

From source file:net.sourceforge.docfetcher.parse.MSOffice2007Parser.java

License:Open Source License

public Document parse(File file) throws ParseException {
    try {/* w w w . ja va2 s  .  c  o  m*/
        // Extract contents
        POITextExtractor ef = ExtractorFactory.createExtractor(file);
        StringBuffer contents = new StringBuffer(ef.getText());

        // Open up properties
        OPCPackage pkg = OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ);
        PackageProperties props = pkg.getPackageProperties();

        // Get author(s)
        String author = null;
        String defaultAuthor = props.getCreatorProperty().getValue();
        String lastAuthor = props.getLastModifiedByProperty().getValue();
        if (defaultAuthor == null) {
            if (lastAuthor != null)
                author = lastAuthor;
        } else if (lastAuthor == null) {
            author = defaultAuthor;
        } else {
            if (defaultAuthor.equals(lastAuthor))
                author = defaultAuthor;
            else
                author = defaultAuthor + ", " + lastAuthor; //$NON-NLS-1$
        }

        // Get other metadata
        String description = props.getDescriptionProperty().getValue();
        String keywords = props.getKeywordsProperty().getValue();
        String subject = props.getSubjectProperty().getValue();
        String title = props.getTitleProperty().getValue();

        // Append metadata to contents
        String[] metaData = new String[] { author, description, keywords, subject, title };
        for (String field : metaData)
            if (field != null)
                contents.append(" ").append(field); //$NON-NLS-1$
        return new Document(file, title, contents).addAuthor(author);
    } catch (Exception e) {
        throw new ParseException(file, Msg.file_not_readable.value());
    }
}

From source file:net.sourceforge.vaticanfetcher.model.parse.MSOffice2007Parser.java

License:Open Source License

@Override
protected ParseResult parse(File file, ParseContext context) throws ParseException {
    OPCPackage pkg = null;
    try {//from w w  w.  jav  a 2  s  .c o m
        pkg = OPCPackage.open(file.getPath(), PackageAccess.READ);
        String contents = extractText(pkg);

        // Open properties
        PackageProperties props = pkg.getPackageProperties();

        // Get author(s)
        String author = null;
        String defaultAuthor = props.getCreatorProperty().getValue();
        String lastAuthor = props.getLastModifiedByProperty().getValue();
        if (defaultAuthor == null) {
            if (lastAuthor != null)
                author = lastAuthor;
        } else if (lastAuthor == null) {
            author = defaultAuthor;
        } else {
            if (defaultAuthor.equals(lastAuthor))
                author = defaultAuthor;
            else
                author = defaultAuthor + ", " + lastAuthor; //$NON-NLS-1$
        }

        // Get other metadata
        String description = props.getDescriptionProperty().getValue();
        String keywords = props.getKeywordsProperty().getValue();
        String subject = props.getSubjectProperty().getValue();
        String title = props.getTitleProperty().getValue();

        return new ParseResult(contents).setTitle(title).addAuthor(author).addMiscMetadata(description)
                .addMiscMetadata(keywords).addMiscMetadata(subject);
    } catch (Exception e) {
        throw new ParseException(e);
    } finally {
        Closeables.closeQuietly(pkg);
    }
}