Search in sources :

Example 6 with PackageRelationship

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

the class ExternalLinksTable method setLinkedFileName.

/**
     * Updates the last recorded name for the file that this links to
     */
public void setLinkedFileName(String target) {
    String rId = link.getExternalBook().getId();
    if (rId == null || rId.isEmpty()) {
    // We're a new External Link Table, so nothing to remove
    } else {
        // Relationships can't be changed, so remove the old one
        getPackagePart().removeRelationship(rId);
    }
    // Have a new one added
    PackageRelationship newRel = getPackagePart().addExternalRelationship(target, PackageRelationshipTypes.EXTERNAL_LINK_PATH);
    link.getExternalBook().setId(newRel.getId());
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship)

Example 7 with PackageRelationship

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

the class POIXMLRelation method getContents.

/**
     *  Fetches the InputStream to read the contents, based
     *  of the specified core part, for which we are defined
     *  as a suitable relationship
     *
     *  @since 3.16-beta3
     */
public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
    PackageRelationshipCollection prc = corePart.getRelationshipsByType(getRelation());
    Iterator<PackageRelationship> it = prc.iterator();
    if (it.hasNext()) {
        PackageRelationship rel = it.next();
        PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
        PackagePart part = corePart.getPackage().getPart(relName);
        return part.getInputStream();
    }
    log.log(POILogger.WARN, "No part " + getDefaultFileName() + " found");
    return null;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 8 with PackageRelationship

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

the class POIXMLDocumentPart method getPartFromOPCPackage.

/**
     * Retrieves the core document part
     * 
     * @since POI 3.14-Beta1
     */
private static PackagePart getPartFromOPCPackage(OPCPackage pkg, String coreDocumentRel) {
    PackageRelationship coreRel = pkg.getRelationshipsByType(coreDocumentRel).getRelationship(0);
    if (coreRel != null) {
        PackagePart pp = pkg.getPart(coreRel);
        if (pp == null) {
            throw new POIXMLException("OOXML file structure broken/invalid - core document '" + coreRel.getTargetURI() + "' not found.");
        }
        return pp;
    }
    coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
    if (coreRel != null) {
        throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
    }
    throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 9 with PackageRelationship

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

the class XSSFShapeGroup method createPicture.

/**
     * Creates a picture.
     *
     * @param anchor       the client anchor describes how this picture is attached to the sheet.
     * @param pictureIndex the index of the picture in the workbook collection of pictures,
     *                     {@link XSSFWorkbook#getAllPictures()} .
     * @return the newly created picture shape.
     */
public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex) {
    PackageRelationship rel = getDrawing().addPictureReference(pictureIndex);
    CTPicture ctShape = ctGroup.addNewPic();
    ctShape.set(XSSFPicture.prototype());
    XSSFPicture shape = new XSSFPicture(getDrawing(), ctShape);
    shape.parent = this;
    shape.anchor = anchor;
    shape.setPictureReference(rel);
    return shape;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) CTPicture(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture)

Example 10 with PackageRelationship

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

the class XSSFSheet method initHyperlinks.

/**
     * Read hyperlink relations, link them with CTHyperlink beans in this worksheet
     * and initialize the internal array of XSSFHyperlink objects
     */
private void initHyperlinks() {
    hyperlinks = new ArrayList<XSSFHyperlink>();
    if (!worksheet.isSetHyperlinks()) {
        return;
    }
    try {
        PackageRelationshipCollection hyperRels = getPackagePart().getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
        // Turn each one into a XSSFHyperlink
        for (CTHyperlink hyperlink : worksheet.getHyperlinks().getHyperlinkArray()) {
            PackageRelationship hyperRel = null;
            if (hyperlink.getId() != null) {
                hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
            }
            hyperlinks.add(new XSSFHyperlink(hyperlink, hyperRel));
        }
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) POIXMLException(org.apache.poi.POIXMLException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Aggregations

PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)50 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)28 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)13 IOException (java.io.IOException)11 POIXMLException (org.apache.poi.POIXMLException)8 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)5 TikaException (org.apache.tika.exception.TikaException)5 ArrayList (java.util.ArrayList)4 XmlException (org.apache.xmlbeans.XmlException)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 HashMap (java.util.HashMap)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 SAXException (org.xml.sax.SAXException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2