use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class CommentsTable method commit.
@Override
protected void commit() throws IOException {
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
writeTo(out);
out.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class UpdateEmbeddedDoc method checkUpdatedDoc.
/**
* Called to test whether or not the embedded workbook was correctly
* updated. This method simply recovers the first cell from the first row
* of the first workbook and tests the value it contains.
* <p/>
* Note that execution will not continue up to the assertion as the
* embedded workbook is now corrupted and causes an IllegalArgumentException
* with the following message
* <p/>
* <em>java.lang.IllegalArgumentException: Your InputStream was neither an
* OLE2 stream, nor an OOXML stream</em>
* <p/>
* to be thrown when the WorkbookFactory.createWorkbook(InputStream) method
* is executed.
*
* @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
* Rather
* than use the specific classes (HSSF/XSSF) to handle the embedded
* workbook this method uses those defeined in the SS stream. As
* a result, it might be the case that a SpreadsheetML file is
* opened for processing, throwing this exception if that file is
* invalid.
* @throws java.io.IOException Thrown if a problem occurs in the underlying
* file system.
*/
public void checkUpdatedDoc() throws OpenXML4JException, IOException {
for (PackagePart pPart : this.doc.getAllEmbedds()) {
String ext = pPart.getPartName().getExtension();
if (BINARY_EXTENSION.equals(ext) || OPENXML_EXTENSION.equals(ext)) {
InputStream is = pPart.getInputStream();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(SHEET_NUM);
Row row = sheet.getRow(ROW_NUM);
Cell cell = row.getCell(CELL_NUM);
assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
} finally {
IOUtils.closeQuietly(workbook);
IOUtils.closeQuietly(is);
}
}
}
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class OOXMLURIDereferencer method dereference.
public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
if (baseUriDereferencer == null) {
baseUriDereferencer = signatureConfig.getSignatureFactory().getURIDereferencer();
}
if (null == uriReference) {
throw new NullPointerException("URIReference cannot be null");
}
if (null == context) {
throw new NullPointerException("XMLCrytoContext cannot be null");
}
URI uri;
try {
uri = new URI(uriReference.getURI());
} catch (URISyntaxException e) {
throw new URIReferenceException("could not URL decode the uri: " + uriReference.getURI(), e);
}
PackagePart part = findPart(uri);
if (part == null) {
LOG.log(POILogger.DEBUG, "cannot resolve, delegating to base DOM URI dereferencer", uri);
return this.baseUriDereferencer.dereference(uriReference, context);
}
InputStream dataStream;
try {
dataStream = part.getInputStream();
// workaround for office 2007 pretty-printed .rels files
if (part.getPartName().toString().endsWith(".rels")) {
// although xmlsec has an option to ignore line breaks, currently this
// only affects .rels files, so we only modify these
// http://stackoverflow.com/questions/4728300
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int ch; (ch = dataStream.read()) != -1; ) {
if (ch == 10 || ch == 13)
continue;
bos.write(ch);
}
dataStream = new ByteArrayInputStream(bos.toByteArray());
}
} catch (IOException e) {
throw new URIReferenceException("I/O error: " + e.getMessage(), e);
}
return new OctetStreamData(dataStream, uri.toString(), null);
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class SignatureInfo method getSignatureParts.
/**
* @return a signature part for each signature document.
* the parts can be validated independently.
*/
public Iterable<SignaturePart> getSignatureParts() {
signatureConfig.init(true);
return new Iterable<SignaturePart>() {
public Iterator<SignaturePart> iterator() {
return new Iterator<SignaturePart>() {
OPCPackage pkg = signatureConfig.getOpcPackage();
Iterator<PackageRelationship> sigOrigRels = pkg.getRelationshipsByType(PackageRelationshipTypes.DIGITAL_SIGNATURE_ORIGIN).iterator();
Iterator<PackageRelationship> sigRels = null;
PackagePart sigPart = null;
public boolean hasNext() {
while (sigRels == null || !sigRels.hasNext()) {
if (!sigOrigRels.hasNext())
return false;
sigPart = pkg.getPart(sigOrigRels.next());
LOG.log(POILogger.DEBUG, "Digital Signature Origin part", sigPart);
try {
sigRels = sigPart.getRelationshipsByType(PackageRelationshipTypes.DIGITAL_SIGNATURE).iterator();
} catch (InvalidFormatException e) {
LOG.log(POILogger.WARN, "Reference to signature is invalid.", e);
}
}
return true;
}
public SignaturePart next() {
PackagePart sigRelPart = null;
do {
try {
if (!hasNext())
throw new NoSuchElementException();
sigRelPart = sigPart.getRelatedPart(sigRels.next());
LOG.log(POILogger.DEBUG, "XML Signature part", sigRelPart);
} catch (InvalidFormatException e) {
LOG.log(POILogger.WARN, "Reference to signature is invalid.", e);
}
} while (sigPart == null);
return new SignaturePart(sigRelPart);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class LoadEmbedded method loadEmbedded.
public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException {
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
if (contentType.equals("application/vnd.ms-excel")) {
// Excel Workbook - either binary or OpenXML
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
embeddedWorkbook.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
// Excel Workbook - OpenXML file format
XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
embeddedWorkbook.close();
} else if (contentType.equals("application/msword")) {
// Word Document - binary (OLE2CDF) file format
HWPFDocument document = new HWPFDocument(pPart.getInputStream());
document.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
// Word Document - OpenXML file format
XWPFDocument document = new XWPFDocument(pPart.getInputStream());
document.close();
} else if (contentType.equals("application/vnd.ms-powerpoint")) {
// PowerPoint Document - binary file format
HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream());
slideShow.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
// PowerPoint Document - OpenXML file format
XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream());
slideShow.close();
} else {
// Any other type of embedded object.
System.out.println("Unknown Embedded Document: " + contentType);
InputStream inputStream = pPart.getInputStream();
inputStream.close();
}
}
}
Aggregations