Search in sources :

Example 6 with PackagePartName

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

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

the class POIXMLProperties method commit.

/**
     * Commit changes to the underlying OPC package
     * 
     * @throws IOException if the properties can't be saved
     * @throws POIXMLException if the properties are erroneous
     */
public void commit() throws IOException {
    if (extPart == null && !NEW_EXT_INSTANCE.toString().equals(ext.props.toString())) {
        try {
            PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/app.xml");
            pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties");
            extPart = pkg.createPart(prtname, "application/vnd.openxmlformats-officedocument.extended-properties+xml");
        } catch (InvalidFormatException e) {
            throw new POIXMLException(e);
        }
    }
    if (custPart == null && !NEW_CUST_INSTANCE.toString().equals(cust.props.toString())) {
        try {
            PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/custom.xml");
            pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties");
            custPart = pkg.createPart(prtname, "application/vnd.openxmlformats-officedocument.custom-properties+xml");
        } catch (InvalidFormatException e) {
            throw new POIXMLException(e);
        }
    }
    if (extPart != null) {
        OutputStream out = extPart.getOutputStream();
        if (extPart.getSize() > 0) {
            extPart.clear();
        }
        ext.props.save(out, DEFAULT_XML_OPTIONS);
        out.close();
    }
    if (custPart != null) {
        OutputStream out = custPart.getOutputStream();
        cust.props.save(out, DEFAULT_XML_OPTIONS);
        out.close();
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OutputStream(java.io.OutputStream) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 8 with PackagePartName

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

the class TestXSSFWorkbook method bug47668.

/**
     * Verify that the attached test data was not modified. If this test method
     * fails, the test data is not working properly.
     */
@Test
public void bug47668() throws Exception {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("47668.xlsx");
    List<XSSFPictureData> allPictures = workbook.getAllPictures();
    assertEquals(1, allPictures.size());
    PackagePartName imagePartName = PackagingURIHelper.createPartName("/xl/media/image1.jpeg");
    PackagePart imagePart = workbook.getPackage().getPart(imagePartName);
    assertNotNull(imagePart);
    for (XSSFPictureData pictureData : allPictures) {
        PackagePart picturePart = pictureData.getPackagePart();
        assertSame(imagePart, picturePart);
    }
    XSSFSheet sheet0 = workbook.getSheetAt(0);
    XSSFDrawing drawing0 = sheet0.createDrawingPatriarch();
    XSSFPictureData pictureData0 = (XSSFPictureData) drawing0.getRelations().get(0);
    byte[] data0 = pictureData0.getData();
    CRC32 crc0 = new CRC32();
    crc0.update(data0);
    XSSFSheet sheet1 = workbook.getSheetAt(1);
    XSSFDrawing drawing1 = sheet1.createDrawingPatriarch();
    XSSFPictureData pictureData1 = (XSSFPictureData) drawing1.getRelations().get(0);
    byte[] data1 = pictureData1.getData();
    CRC32 crc1 = new CRC32();
    crc1.update(data1);
    assertEquals(crc0.getValue(), crc1.getValue());
    workbook.close();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) CRC32(java.util.zip.CRC32) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Test(org.junit.Test)

Example 9 with PackagePartName

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

the class XSSFWorkbook method setVBAProject.

/**
     * Adds a vbaProject.bin file to the workbook.  This will change the workbook
     * type if necessary.
     *
     * @throws IOException
     */
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
    if (!isMacroEnabled()) {
        setWorkbookType(XSSFWorkbookType.XLSM);
    }
    PackagePartName ppName;
    try {
        ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    OPCPackage opc = getPackage();
    OutputStream outputStream;
    if (!opc.containPart(ppName)) {
        POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
        outputStream = relationship.getPackagePart().getOutputStream();
    } else {
        PackagePart part = opc.getPart(ppName);
        outputStream = part.getOutputStream();
    }
    try {
        IOUtils.copy(vbaProjectStream, outputStream);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 10 with PackagePartName

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

the class XSSFWorkbook method addOlePackage.

@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException {
    // find an unused part name
    OPCPackage opc = getPackage();
    PackagePartName pnOLE;
    int oleId = 0;
    do {
        try {
            pnOLE = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + (++oleId) + ".bin");
        } catch (InvalidFormatException e) {
            throw new IOException("ole object name not recognized", e);
        }
    } while (opc.containPart(pnOLE));
    PackagePart pp = opc.createPart(pnOLE, "application/vnd.openxmlformats-officedocument.oleObject");
    Ole10Native ole10 = new Ole10Native(label, fileName, command, oleData);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length + 500);
    ole10.writeOut(bos);
    POIFSFileSystem poifs = new POIFSFileSystem();
    DirectoryNode root = poifs.getRoot();
    root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
    root.setStorageClsid(ClassID.OLE10_PACKAGE);
    // TODO: generate CombObj stream
    OutputStream os = pp.getOutputStream();
    poifs.writeFilesystem(os);
    os.close();
    poifs.close();
    return oleId;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Ole10Native(org.apache.poi.poifs.filesystem.Ole10Native) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Aggregations

PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)37 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)19 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)19 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)16 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)13 Test (org.junit.Test)10 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 POIXMLException (org.apache.poi.POIXMLException)5 ArrayList (java.util.ArrayList)4 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)4 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)4 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)4 URI (java.net.URI)3 PartAlreadyExistsException (org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException)3 XmlException (org.apache.xmlbeans.XmlException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 NoSuchElementException (java.util.NoSuchElementException)2