package org.apache.poi.ooxml.util;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.util.IOUtils;
import java.io.*;
import java.net.URI;
public final class PackageHelper {
public static OPCPackage open(InputStream is) throws IOException {
try {
return OPCPackage.open(is);
} catch (InvalidFormatException e){
throw new POIXMLException(e);
}
}
public static OPCPackage clone(OPCPackage pkg, File file) throws OpenXML4JException, IOException {
String path = file.getAbsolutePath();
OPCPackage dest = OPCPackage.create(path);
PackageRelationshipCollection rels = pkg.getRelationships();
for (PackageRelationship rel : rels) {
PackagePart part = pkg.getPart(rel);
PackagePart part_tgt;
if (rel.getRelationshipType().equals(PackageRelationshipTypes.CORE_PROPERTIES)) {
copyProperties(pkg.getPackageProperties(), dest.getPackageProperties());
continue;
}
dest.addRelationship(part.getPartName(), rel.getTargetMode(), rel.getRelationshipType());
part_tgt = dest.createPart(part.getPartName(), part.getContentType());
OutputStream out = part_tgt.getOutputStream();
IOUtils.copy(part.getInputStream(), out);
out.close();
if(part.hasRelationships()) {
copy(pkg, part, dest, part_tgt);
}
}
dest.close();
new File(path).deleteOnExit();
return OPCPackage.open(path);
}
private static void copy(OPCPackage pkg, PackagePart part, OPCPackage tgt, PackagePart part_tgt) throws OpenXML4JException, IOException {
PackageRelationshipCollection rels = part.getRelationships();
if(rels != null) for (PackageRelationship rel : rels) {
PackagePart p;
if(rel.getTargetMode() == TargetMode.EXTERNAL){
part_tgt.addExternalRelationship(rel.getTargetURI().toString(), rel.getRelationshipType(), rel.getId());
continue;
}
URI uri = rel.getTargetURI();
if(uri.getRawFragment() != null) {
part_tgt.addRelationship(uri, rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
continue;
}
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
p = pkg.getPart(relName);
part_tgt.addRelationship(p.getPartName(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
PackagePart dest;
if(!tgt.containPart(p.getPartName())){
dest = tgt.createPart(p.getPartName(), p.getContentType());
OutputStream out = dest.getOutputStream();
IOUtils.copy(p.getInputStream(), out);
out.close();
copy(pkg, p, tgt, dest);
}
}
}
private static void copyProperties(PackageProperties src, PackageProperties tgt) {
tgt.setCategoryProperty(src.getCategoryProperty());
tgt.setContentStatusProperty(src.getContentStatusProperty());
tgt.setContentTypeProperty(src.getContentTypeProperty());
tgt.setCreatorProperty(src.getCreatorProperty());
tgt.setDescriptionProperty(src.getDescriptionProperty());
tgt.setIdentifierProperty(src.getIdentifierProperty());
tgt.setKeywordsProperty(src.getKeywordsProperty());
tgt.setLanguageProperty(src.getLanguageProperty());
tgt.setRevisionProperty(src.getRevisionProperty());
tgt.setSubjectProperty(src.getSubjectProperty());
tgt.setTitleProperty(src.getTitleProperty());
tgt.setVersionProperty(src.getVersionProperty());
}
}