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;
}
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);
}
}
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);
}
}
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);
}
}
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());
}
}
Aggregations