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