Search in sources :

Example 11 with PackagePart

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();
}
Also used : OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 12 with PackagePart

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);
            }
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Row(org.apache.poi.ss.usermodel.Row) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 13 with PackagePart

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) URIReferenceException(javax.xml.crypto.URIReferenceException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) URI(java.net.URI) OctetStreamData(javax.xml.crypto.OctetStreamData)

Example 14 with PackagePart

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();
                }
            };
        }
    };
}
Also used : Iterator(java.util.Iterator) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) NoSuchElementException(java.util.NoSuchElementException)

Example 15 with PackagePart

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();
        }
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) InputStream(java.io.InputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

PackagePart (org.apache.poi.openxml4j.opc.PackagePart)118 OutputStream (java.io.OutputStream)38 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)27 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)24 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)23 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)19 QName (javax.xml.namespace.QName)18 IOException (java.io.IOException)17 XmlOptions (org.apache.xmlbeans.XmlOptions)17 InputStream (java.io.InputStream)11 Test (org.junit.Test)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 POIXMLException (org.apache.poi.POIXMLException)8 XmlException (org.apache.xmlbeans.XmlException)8 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)7 ArrayList (java.util.ArrayList)6 TikaException (org.apache.tika.exception.TikaException)6 URI (java.net.URI)5 SAXException (org.xml.sax.SAXException)5