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