Search in sources :

Example 16 with OPCPackage

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

the class POIXMLDocument method write.

/**
     * Write out this document to an Outputstream.
     *
     * Note - if the Document was opened from a {@link File} rather
     *  than an {@link InputStream}, you <b>must</b> write out to
     *  a different file, overwriting via an OutputStream isn't possible.
     *  
     * If {@code stream} is a {@link java.io.FileOutputStream} on a networked drive
     * or has a high cost/latency associated with each written byte,
     * consider wrapping the OutputStream in a {@link java.io.BufferedOutputStream}
     * to improve write performance.
     * 
     * @param stream - the java OutputStream you wish to write the file to
     *
     * @exception IOException if anything can't be written.
     */
@SuppressWarnings("resource")
public final void write(OutputStream stream) throws IOException {
    OPCPackage p = getPackage();
    if (p == null) {
        throw new IOException("Cannot write data, document seems to have been closed already");
    }
    //force all children to commit their changes into the underlying OOXML Package
    // TODO Shouldn't they be committing to the new one instead?
    Set<PackagePart> context = new HashSet<PackagePart>();
    onSave(context);
    context.clear();
    //save extended and custom properties
    getProperties().commit();
    p.save(stream);
}
Also used : IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) HashSet(java.util.HashSet)

Example 17 with OPCPackage

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

the class XWPFPictureData method equals.

@Override
public boolean equals(Object obj) {
    /*
         * In case two objects ARE equal, but its not the same instance, this
         * implementation will always run through the whole
         * byte-array-comparison before returning true. If this will turn into a
         * performance issue, two possible approaches are available:<br>
         * a) Use the checksum only and take the risk that two images might have
         * the same CRC32 sum, although they are not the same.<br>
         * b) Use a second (or third) checksum algorithm to minimise the chance
         * that two images have the same checksums but are not equal (e.g.
         * CRC32, MD5 and SHA-1 checksums, additionally compare the
         * data-byte-array lengths).
         */
    if (obj == this) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof XWPFPictureData)) {
        return false;
    }
    XWPFPictureData picData = (XWPFPictureData) obj;
    PackagePart foreignPackagePart = picData.getPackagePart();
    PackagePart ownPackagePart = this.getPackagePart();
    if ((foreignPackagePart != null && ownPackagePart == null) || (foreignPackagePart == null && ownPackagePart != null)) {
        return false;
    }
    if (ownPackagePart != null) {
        OPCPackage foreignPackage = foreignPackagePart.getPackage();
        OPCPackage ownPackage = ownPackagePart.getPackage();
        if ((foreignPackage != null && ownPackage == null) || (foreignPackage == null && ownPackage != null)) {
            return false;
        }
        if (ownPackage != null) {
            if (!ownPackage.equals(foreignPackage)) {
                return false;
            }
        }
    }
    Long foreignChecksum = picData.getChecksum();
    Long localChecksum = getChecksum();
    if (!(localChecksum.equals(foreignChecksum))) {
        return false;
    }
    return Arrays.equals(this.getData(), picData.getData());
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 18 with OPCPackage

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

the class TestXMLPropertiesTextExtractor method testWithSomeNulls.

/**
	 * Bug #49386 - some properties, especially
	 *  dates can be null
	 */
public void testWithSomeNulls() throws Exception {
    OPCPackage pkg = OPCPackage.open(_slSamples.openResourceAsStream("49386-null_dates.pptx"));
    XSLFSlideShow sl = new XSLFSlideShow(pkg);
    POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(sl);
    ext.getText();
    String text = ext.getText();
    // With date is null
    assertFalse(text.contains("Created ="));
    // Via string is blank
    assertContains(text, "CreatedString = ");
    assertContains(text, "LastModifiedBy = IT Client Services");
    ext.close();
}
Also used : XSLFSlideShow(org.apache.poi.xslf.usermodel.XSLFSlideShow) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 19 with OPCPackage

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

the class TestXMLPropertiesTextExtractor method testGetFromMainExtractor.

public void testGetFromMainExtractor() throws Exception {
    OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    XSSFExcelExtractor ext = new XSSFExcelExtractor(wb);
    POIXMLPropertiesTextExtractor textExt = ext.getMetadataTextExtractor();
    // Check basics
    assertNotNull(textExt);
    assertTrue(textExt.getText().length() > 0);
    // Check some of the content
    String text = textExt.getText();
    String cText = textExt.getCorePropertiesText();
    assertContains(text, "LastModifiedBy = Yury Batrakov");
    assertContains(cText, "LastModifiedBy = Yury Batrakov");
    textExt.close();
    ext.close();
}
Also used : XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 20 with OPCPackage

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

the class TestXMLPropertiesTextExtractor method testCore.

public void testCore() throws Exception {
    OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(wb);
    ext.getText();
    // Now check
    String text = ext.getText();
    String cText = ext.getCorePropertiesText();
    assertContains(text, "LastModifiedBy = Yury Batrakov");
    assertContains(cText, "LastModifiedBy = Yury Batrakov");
    ext.close();
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Aggregations

OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)116 Test (org.junit.Test)54 InputStream (java.io.InputStream)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 File (java.io.File)19 IOException (java.io.IOException)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)10 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)10 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)10 OutputStream (java.io.OutputStream)9 ArrayList (java.util.ArrayList)9 FileOutputStream (java.io.FileOutputStream)8 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)8 XmlException (org.apache.xmlbeans.XmlException)8 FileInputStream (java.io.FileInputStream)7