Search in sources :

Example 11 with OPCPackage

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

the class FromHowTo method processFirstSheet.

public void processFirstSheet(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable();
        XMLReader parser = fetchSheetParser(sst);
        // process the first sheet
        InputStream sheet2 = r.getSheetsData().next();
        InputSource sheetSource = new InputSource(sheet2);
        parser.parse(sheetSource);
        sheet2.close();
    } finally {
        pkg.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InputStream(java.io.InputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 12 with OPCPackage

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

the class CustomXMLMapping method main.

public static void main(String[] args) throws Exception {
    OPCPackage pkg = OPCPackage.open(args[0]);
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    for (XSSFMap map : wb.getCustomXMLMappings()) {
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, true);
        String xml = os.toString("UTF-8");
        System.out.println(xml);
    }
    pkg.close();
}
Also used : XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFExportToXml(org.apache.poi.xssf.extractor.XSSFExportToXml) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 13 with OPCPackage

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

the class POIXMLDocumentPart method getNextPartNumber.

/**
     * Identifies the next available part number for a part of the given type,
     *  if possible, otherwise -1 if none are available.
     * The found (valid) index can then be safely given to
     *  {@link #createRelationship(POIXMLRelation, POIXMLFactory, int)} or
     *  {@link #createRelationship(POIXMLRelation, POIXMLFactory, int, boolean)}
     *  without naming clashes.
     * If parts with other types are already claiming a name for this relationship
     *  type (eg a {@link XSSFRelation#CHART} using the drawing part namespace 
     *  normally used by {@link XSSFRelation#DRAWINGS}), those will be considered
     *  when finding the next spare number.
     *
     * @param descriptor The relationship type to find the part number for
     * @param minIdx The minimum free index to assign, use -1 for any
     * @return The next free part number, or -1 if none available
     */
protected final int getNextPartNumber(POIXMLRelation descriptor, int minIdx) {
    OPCPackage pkg = packagePart.getPackage();
    try {
        String name = descriptor.getDefaultFileName();
        if (name.equals(descriptor.getFileName(9999))) {
            // Non-index based, check if default is free
            PackagePartName ppName = PackagingURIHelper.createPartName(name);
            if (pkg.containPart(ppName)) {
                // Default name already taken, not index based, nothing free
                return -1;
            } else {
                // Default name free
                return 0;
            }
        }
        // Default to searching from 1, unless they asked for 0+
        int idx = (minIdx < 0) ? 1 : minIdx;
        int maxIdx = minIdx + pkg.getParts().size();
        while (idx <= maxIdx) {
            name = descriptor.getFileName(idx);
            PackagePartName ppName = PackagingURIHelper.createPartName(name);
            if (!pkg.containPart(ppName)) {
                return idx;
            }
            idx++;
        }
    } catch (InvalidFormatException e) {
        // Give a general wrapped exception for the problem
        throw new POIXMLException(e);
    }
    return -1;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 14 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 15 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)

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