Search in sources :

Example 1 with PackageRelationshipCollection

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

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

the class XSLFSlideShow method getSlideComments.

/**
	 * Returns all the comments for the given slide
	 */
@Internal
public CTCommentList getSlideComments(CTSlideIdListEntry slide) throws IOException, XmlException {
    PackageRelationshipCollection commentRels;
    PackagePart slidePart = getSlidePart(slide);
    try {
        commentRels = slidePart.getRelationshipsByType(XSLFRelation.COMMENTS.getRelation());
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
    if (commentRels.size() == 0) {
        // No comments for this slide
        return null;
    }
    if (commentRels.size() > 1) {
        throw new IllegalStateException("Expecting 0 or 1 comments for a slide, but found " + commentRels.size());
    }
    try {
        PackagePart cPart = slidePart.getRelatedPart(commentRels.getRelationship(0));
        CmLstDocument commDoc = CmLstDocument.Factory.parse(cPart.getInputStream(), DEFAULT_XML_OPTIONS);
        return commDoc.getCmLst();
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
}
Also used : CmLstDocument(org.openxmlformats.schemas.presentationml.x2006.main.CmLstDocument) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Internal(org.apache.poi.util.Internal)

Example 3 with PackageRelationshipCollection

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

the class XSLFSlideShow method getNodesPart.

/**
	 * Gets the PackagePart of the notes for the
	 *  given slide, or null if there isn't one.
	 */
public PackagePart getNodesPart(CTSlideIdListEntry parentSlide) throws IOException, XmlException {
    PackageRelationshipCollection notes;
    PackagePart slidePart = getSlidePart(parentSlide);
    try {
        notes = slidePart.getRelationshipsByType(XSLFRelation.NOTES.getRelation());
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
    if (notes.size() == 0) {
        // No notes for this slide
        return null;
    }
    if (notes.size() > 1) {
        throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size());
    }
    try {
        return slidePart.getRelatedPart(notes.getRelationship(0));
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
}
Also used : PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 4 with PackageRelationshipCollection

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

Example 5 with PackageRelationshipCollection

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

the class TestXSSFHyperlink method testCreate.

@Test
public void testCreate() throws Exception {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    XSSFRow row = sheet.createRow(0);
    XSSFCreationHelper createHelper = workbook.getCreationHelper();
    String[] urls = { "http://apache.org", "www.apache.org", "/temp", "c:/temp", "http://apache.org/default.php?s=isTramsformed&submit=Search&la=*&li=*" };
    for (int i = 0; i < urls.length; i++) {
        String s = urls[i];
        XSSFHyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
        link.setAddress(s);
        XSSFCell cell = row.createCell(i);
        cell.setHyperlink(link);
    }
    workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    sheet = workbook.getSheetAt(0);
    PackageRelationshipCollection rels = sheet.getPackagePart().getRelationships();
    assertEquals(urls.length, rels.size());
    for (int i = 0; i < rels.size(); i++) {
        PackageRelationship rel = rels.getRelationship(i);
        // there should be a relationship for each URL
        assertEquals(urls[i], rel.getTargetURI().toString());
    }
    // Bugzilla 53041: Hyperlink relations are duplicated when saving XSSF file
    workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    sheet = workbook.getSheetAt(0);
    rels = sheet.getPackagePart().getRelationships();
    assertEquals(urls.length, rels.size());
    for (int i = 0; i < rels.size(); i++) {
        PackageRelationship rel = rels.getRelationship(i);
        // there should be a relationship for each URL
        assertEquals(urls[i], rel.getTargetURI().toString());
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) Test(org.junit.Test)

Aggregations

PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)23 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)15 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)14 IOException (java.io.IOException)8 XmlException (org.apache.xmlbeans.XmlException)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)4 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)4 XWPFRelation (org.apache.poi.xwpf.usermodel.XWPFRelation)4 TikaException (org.apache.tika.exception.TikaException)4 SAXException (org.xml.sax.SAXException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 ZipException (java.util.zip.ZipException)2 XMLSignatureException (javax.xml.crypto.dsig.XMLSignatureException)2