Search in sources :

Example 31 with PackagePartName

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

the class TestOPCCompliancePackageModel method testAddPackageAlreadyAddFailure2.

/**
     * Rule M1.12 : Packages shall not contain equivalent part names and package
     * implementers shall neither create nor recognize packages with equivalent
     * part names.
     */
@Test
public void testAddPackageAlreadyAddFailure2() throws Exception {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName partName = null;
    try {
        partName = PackagingURIHelper.createPartName("/word/document.xml");
    } catch (InvalidFormatException e) {
        throw new Exception(e.getMessage());
    }
    pkg.createPart(partName, ContentTypes.XML);
    try {
        pkg.createPart(partName, ContentTypes.XML);
    } catch (InvalidOperationException e) {
        return;
    }
    fail("Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) Test(org.junit.Test)

Example 32 with PackagePartName

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

the class TestOPCCompliancePackageModel method testAddRelationshipRelationshipsPartFailure.

/**
     * Try to add a relationship to a relationship part.
     *
     * Check rule M1.25: The Relationships part shall not have relationships to
     * any other part. Package implementers shall enforce this requirement upon
     * the attempt to create such a relationship and shall treat any such
     * relationship as invalid.
     */
@Test
public void testAddRelationshipRelationshipsPartFailure() {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName name1 = null;
    try {
        name1 = PackagingURIHelper.createPartName("/test/_rels/document.xml.rels");
    } catch (InvalidFormatException e) {
        fail("This exception should never happen !");
    }
    try {
        pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
    } catch (InvalidOperationException e) {
        return;
    }
    fail("Fail test -> M1.25: The Relationships part shall not have relationships to any other part");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Example 33 with PackagePartName

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

the class TestPackagePartName method testGetExtension.

/**
	 * Test method getExtension().
	 */
public void testGetExtension() throws Exception {
    PackagePartName name1 = PackagingURIHelper.createPartName("/doc/props/document.xml");
    PackagePartName name2 = PackagingURIHelper.createPartName("/root/document");
    assertEquals("xml", name1.getExtension());
    assertEquals("", name2.getExtension());
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName)

Example 34 with PackagePartName

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

the class TestPackagingURIHelper method testCreatePartNameRelativeURI.

/**
	 * Test createPartName(URI, y)
	 */
public void testCreatePartNameRelativeURI() throws Exception {
    PackagePartName partNameToValid = PackagingURIHelper.createPartName("/word/media/image1.gif");
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    // Base part
    PackagePartName nameBase = PackagingURIHelper.createPartName("/word/document.xml");
    PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
    // Relative part name
    PackagePartName relativeName = PackagingURIHelper.createPartName(new URI("media/image1.gif"), partBase);
    assertTrue("The part name must be equal to " + partNameToValid.getName(), partNameToValid.equals(relativeName));
    pkg.revert();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) URI(java.net.URI)

Example 35 with PackagePartName

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

the class XSSFDrawing method createObjectData.

@Override
public XSSFObjectData createObjectData(ClientAnchor anchor, int storageId, int pictureIndex) {
    XSSFSheet sh = getSheet();
    PackagePart sheetPart = sh.getPackagePart();
    /*
         * The shape id of the ole object seems to be a legacy shape id.
         * 
         * see 5.3.2.1 legacyDrawing (Legacy Drawing Object):
         * Legacy Shape ID that is unique throughout the entire document.
         * Legacy shape IDs should be assigned based on which portion of the document the
         * drawing resides on. The assignment of these ids is broken down into clusters of
         * 1024 values. The first cluster is 1-1024, the second 1025-2048 and so on.
         *
         * Ole shapes seem to start with 1025 on the first sheet ...
         * and not sure, if the ids need to be reindexed when sheets are removed
         * or more than 1024 shapes are on a given sheet (see #51332 for a similar issue)
         */
    XSSFSheet sheet = getSheet();
    XSSFWorkbook wb = sheet.getWorkbook();
    int sheetIndex = wb.getSheetIndex(sheet);
    long shapeId = (sheetIndex + 1) * 1024 + newShapeId();
    // add reference to OLE part
    PackagePartName olePN;
    try {
        olePN = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + storageId + ".bin");
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    PackageRelationship olePR = sheetPart.addRelationship(olePN, TargetMode.INTERNAL, POIXMLDocument.OLE_OBJECT_REL_TYPE);
    // add reference to image part
    XSSFPictureData imgPD = sh.getWorkbook().getAllPictures().get(pictureIndex);
    PackagePartName imgPN = imgPD.getPackagePart().getPartName();
    PackageRelationship imgSheetPR = sheetPart.addRelationship(imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART);
    PackageRelationship imgDrawPR = getPackagePart().addRelationship(imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART);
    // add OLE part metadata to sheet
    CTWorksheet cwb = sh.getCTWorksheet();
    CTOleObjects oo = cwb.isSetOleObjects() ? cwb.getOleObjects() : cwb.addNewOleObjects();
    CTOleObject ole1 = oo.addNewOleObject();
    ole1.setProgId("Package");
    ole1.setShapeId(shapeId);
    ole1.setId(olePR.getId());
    XmlCursor cur1 = ole1.newCursor();
    cur1.toEndToken();
    cur1.beginElement("objectPr", XSSFRelation.NS_SPREADSHEETML);
    cur1.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, imgSheetPR.getId());
    cur1.insertAttributeWithValue("defaultSize", "0");
    cur1.beginElement("anchor", XSSFRelation.NS_SPREADSHEETML);
    cur1.insertAttributeWithValue("moveWithCells", "1");
    CTTwoCellAnchor ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
    XmlCursor cur2 = ctAnchor.newCursor();
    cur2.copyXmlContents(cur1);
    cur2.dispose();
    cur1.toParent();
    cur1.toFirstChild();
    cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "from"));
    cur1.toNextSibling();
    cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "to"));
    cur1.dispose();
    // add a new shape and link OLE & image part
    CTShape ctShape = ctAnchor.addNewSp();
    ctShape.set(XSSFObjectData.prototype());
    ctShape.getSpPr().setXfrm(createXfrm((XSSFClientAnchor) anchor));
    // workaround for not having the vmlDrawing filled
    CTBlipFillProperties blipFill = ctShape.getSpPr().addNewBlipFill();
    blipFill.addNewBlip().setEmbed(imgDrawPR.getId());
    blipFill.addNewStretch().addNewFillRect();
    CTNonVisualDrawingProps cNvPr = ctShape.getNvSpPr().getCNvPr();
    cNvPr.setId(shapeId);
    cNvPr.setName("Object " + shapeId);
    XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor();
    extCur.toFirstChild();
    extCur.setAttributeText(new QName("spid"), "_x0000_s" + shapeId);
    extCur.dispose();
    XSSFObjectData shape = new XSSFObjectData(this, ctShape);
    shape.anchor = (XSSFClientAnchor) anchor;
    return shape;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) QName(javax.xml.namespace.QName) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTOleObjects(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObjects) CTShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XmlCursor(org.apache.xmlbeans.XmlCursor) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) CTNonVisualDrawingProps(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps) CTOleObject(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObject) CTTwoCellAnchor(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor)

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