Search in sources :

Example 21 with PackagePartName

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

the class POIXMLDocumentPart method read.

/**
     * Iterate through the underlying PackagePart and create child POIXMLFactory instances
     * using the specified factory
     *
     * @param factory   the factory object that creates POIXMLFactory instances
     * @param context   context map containing already visited noted keyed by targetURI
     * 
     * @throws OpenXML4JException thrown when a related part can't be read
     */
protected void read(POIXMLFactory factory, Map<PackagePart, POIXMLDocumentPart> context) throws OpenXML4JException {
    PackagePart pp = getPackagePart();
    // add mapping a second time, in case of initial caller hasn't done so
    POIXMLDocumentPart otherChild = context.put(pp, this);
    if (otherChild != null && otherChild != this) {
        throw new POIXMLException("Unique PackagePart-POIXMLDocumentPart relation broken!");
    }
    if (!pp.hasRelationships())
        return;
    PackageRelationshipCollection rels = packagePart.getRelationships();
    List<POIXMLDocumentPart> readLater = new ArrayList<POIXMLDocumentPart>();
    // scan breadth-first, so parent-relations are hopefully the shallowest element
    for (PackageRelationship rel : rels) {
        if (rel.getTargetMode() == TargetMode.INTERNAL) {
            URI uri = rel.getTargetURI();
            // check for internal references (e.g. '#Sheet1!A1')
            PackagePartName relName;
            if (uri.getRawFragment() != null) {
                relName = PackagingURIHelper.createPartName(uri.getPath());
            } else {
                relName = PackagingURIHelper.createPartName(uri);
            }
            final PackagePart p = packagePart.getPackage().getPart(relName);
            if (p == null) {
                logger.log(POILogger.ERROR, "Skipped invalid entry " + rel.getTargetURI());
                continue;
            }
            POIXMLDocumentPart childPart = context.get(p);
            if (childPart == null) {
                childPart = factory.createDocumentPart(this, p);
                childPart.parent = this;
                // already add child to context, so other children can reference it
                context.put(p, childPart);
                readLater.add(childPart);
            }
            addRelation(rel, childPart);
        }
    }
    for (POIXMLDocumentPart childPart : readLater) {
        childPart.read(factory, context);
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) ArrayList(java.util.ArrayList) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) URI(java.net.URI)

Example 22 with PackagePartName

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

the class POIXMLDocumentPart method addRelation.

/**
     * Add a new child POIXMLDocumentPart
     *
     * @param relId the preferred relation id, when null the next free relation id will be used
     * @param relationshipType the package relationship type
     * @param part the child to add
     * 
     * @return the new RelationPart
     *
     * @since 3.14-Beta1
     */
public final RelationPart addRelation(String relId, POIXMLRelation relationshipType, POIXMLDocumentPart part) {
    PackageRelationship pr = this.packagePart.findExistingRelation(part.getPackagePart());
    if (pr == null) {
        PackagePartName ppn = part.getPackagePart().getPartName();
        String relType = relationshipType.getRelation();
        pr = packagePart.addRelationship(ppn, TargetMode.INTERNAL, relType, relId);
    }
    addRelation(pr, part);
    return new RelationPart(pr, part);
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName)

Example 23 with PackagePartName

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

the class XWPFDocument method newPackage.

/**
     * Create a new WordProcessingML package and setup the default minimal content
     */
protected static OPCPackage newPackage() {
    try {
        OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
        // Main part
        PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
        // Create main part relationship
        pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
        // Create main document part
        pkg.createPart(corePartName, XWPFRelation.DOCUMENT.getContentType());
        pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
        return pkg;
    } catch (Exception e) {
        throw new POIXMLException(e);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) POIXMLException(org.apache.poi.POIXMLException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException)

Example 24 with PackagePartName

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

the class XWPFDocument method getNextPicNameNumber.

/**
     * get the next free ImageNumber
     *
     * @param format
     * @return the next free ImageNumber
     * @throws InvalidFormatException
     */
public int getNextPicNameNumber(int format) throws InvalidFormatException {
    int img = getAllPackagePictures().size() + 1;
    String proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
    PackagePartName createPartName = PackagingURIHelper.createPartName(proposal);
    while (this.getPackage().getPart(createPartName) != null) {
        img++;
        proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
        createPartName = PackagingURIHelper.createPartName(proposal);
    }
    return img;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName)

Example 25 with PackagePartName

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

the class XSLFMetroShape method parseShape.

/*
     * parses the metro bytes to a XSLF shape
     */
public static Shape<?, ?> parseShape(byte[] metroBytes) throws InvalidFormatException, IOException, XmlException {
    PackagePartName shapePN = PackagingURIHelper.createPartName("/drs/shapexml.xml");
    OPCPackage pkg = null;
    try {
        pkg = OPCPackage.open(new ByteArrayInputStream(metroBytes));
        PackagePart shapePart = pkg.getPart(shapePN);
        CTGroupShape gs = CTGroupShape.Factory.parse(shapePart.getInputStream(), DEFAULT_XML_OPTIONS);
        XSLFGroupShape xgs = new XSLFGroupShape(gs, null);
        return xgs.getShapes().get(0);
    } finally {
        if (pkg != null) {
            pkg.close();
        }
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayInputStream(java.io.ByteArrayInputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) CTGroupShape(org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape)

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