Search in sources :

Example 1 with PackagePartName

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

the class XSLFHyperlink method linkToSlide.

@Override
public void linkToSlide(Slide<XSLFShape, XSLFTextParagraph> slide) {
    PackagePart thisPP = _sheet.getPackagePart();
    PackagePartName otherPPN = ((XSLFSheet) slide).getPackagePart().getPartName();
    if (_link.isSetId() && !_link.getId().isEmpty()) {
        thisPP.removeRelationship(_link.getId());
    }
    PackageRelationship rel = thisPP.addRelationship(otherPPN, TargetMode.INTERNAL, XSLFRelation.SLIDE.getRelation());
    _link.setId(rel.getId());
    _link.setAction("ppaction://hlinksldjump");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 2 with PackagePartName

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

the class XSLFSheet method importPart.

/**
     * Import a package part into this sheet.
     */
PackagePart importPart(PackageRelationship srcRel, PackagePart srcPafrt) {
    PackagePart destPP = getPackagePart();
    PackagePartName srcPPName = srcPafrt.getPartName();
    OPCPackage pkg = destPP.getPackage();
    if (pkg.containPart(srcPPName)) {
        // already exists
        return pkg.getPart(srcPPName);
    }
    destPP.addRelationship(srcPPName, TargetMode.INTERNAL, srcRel.getRelationshipType());
    PackagePart part = pkg.createPart(srcPPName, srcPafrt.getContentType());
    try {
        OutputStream out = part.getOutputStream();
        InputStream is = srcPafrt.getInputStream();
        IOUtils.copy(is, out);
        is.close();
        out.close();
    } catch (IOException e) {
        throw new POIXMLException(e);
    }
    return part;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 3 with PackagePartName

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

the class XSSFReader method getSheet.

/**
     * Returns an InputStream to read the contents of the
     *  specified Sheet.
     * @param relId The relationId of the sheet, from a r:id on the workbook
     */
public InputStream getSheet(String relId) throws IOException, InvalidFormatException {
    PackageRelationship rel = workbookPart.getRelationship(relId);
    if (rel == null) {
        throw new IllegalArgumentException("No Sheet found with r:id " + relId);
    }
    PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
    PackagePart sheet = pkg.getPart(relName);
    if (sheet == null) {
        throw new IllegalArgumentException("No data found for Sheet with r:id " + relId);
    }
    return sheet.getInputStream();
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 4 with PackagePartName

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

the class ZipPartMarshaller method marshall.

/**
	 * Save the specified part.
	 *
	 * @throws OpenXML4JException
	 *             Throws if an internal exception is thrown.
	 */
@Override
public boolean marshall(PackagePart part, OutputStream os) throws OpenXML4JException {
    if (!(os instanceof ZipOutputStream)) {
        logger.log(POILogger.ERROR, "Unexpected class " + os.getClass().getName());
        throw new OpenXML4JException("ZipOutputStream expected !");
    // Normally should happen only in developement phase, so just throw
    // exception
    }
    // might depend on empty parts being saved, e.g. some unit tests verify this currently.
    if (part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
        return true;
    }
    ZipOutputStream zos = (ZipOutputStream) os;
    ZipEntry partEntry = new ZipEntry(ZipHelper.getZipItemNameFromOPCName(part.getPartName().getURI().getPath()));
    try {
        // Create next zip entry
        zos.putNextEntry(partEntry);
        // Saving data in the ZIP file
        InputStream ins = part.getInputStream();
        byte[] buff = new byte[ZipHelper.READ_WRITE_FILE_BUFFER_SIZE];
        while (ins.available() > 0) {
            int resultRead = ins.read(buff);
            if (resultRead == -1) {
                // End of file reached
                break;
            }
            zos.write(buff, 0, resultRead);
        }
        zos.closeEntry();
    } catch (IOException ioe) {
        logger.log(POILogger.ERROR, "Cannot write: " + part.getPartName() + ": in ZIP", ioe);
        return false;
    }
    // Saving relationship part
    if (part.hasRelationships()) {
        PackagePartName relationshipPartName = PackagingURIHelper.getRelationshipPartName(part.getPartName());
        marshallRelationshipPart(part.getRelationships(), relationshipPartName, zos);
    }
    return true;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 5 with PackagePartName

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

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