Search in sources :

Example 31 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart 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 32 with PackagePart

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

the class XWPFDocument method commit.

/**
     * commit and saves the document
     */
@Override
protected void commit() throws IOException {
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTDocument1.type.getName().getNamespaceURI(), "document"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    ctDocument.save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 33 with PackagePart

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

the class XWPFDocument method getAllEmbedds.

/**
     * Get the document's embedded files.
     */
@Override
public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
    List<PackagePart> embedds = new LinkedList<PackagePart>();
    // Get the embeddings for the workbook
    PackagePart part = getPackagePart();
    for (PackageRelationship rel : getPackagePart().getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
        embedds.add(part.getRelatedPart(rel));
    }
    for (PackageRelationship rel : getPackagePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
        embedds.add(part.getRelatedPart(rel));
    }
    return embedds;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) LinkedList(java.util.LinkedList)

Example 34 with PackagePart

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

the class XWPFHeader method commit.

/**
     * save and commit footer
     */
@Override
protected void commit() throws IOException {
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "hdr"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    super._getHdrFtr().save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 35 with PackagePart

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

the class XWPFStyles method commit.

@Override
protected void commit() throws IOException {
    if (ctStyles == null) {
        throw new IllegalStateException("Unable to write out styles that were never read in!");
    }
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    ctStyles.save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

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