/** * Get the Relationships Part (if there is one) for a given Part. Otherwise return null. * * @param zf * @param part * @return * @throws InvalidFormatException */ public RelationshipsPart getRelationshipsPart(ZipFile zf, Part part) throws Docx4JException, InvalidFormatException { RelationshipsPart rrp = null; // recurse via this parts relationships, if it has any // String relPart = PartName.getRelationshipsPartName(target); String relPart = PartName.getRelationshipsPartName(part.getPartName().getName().substring(1)); if (zf.getEntry(relPart) != null) { log.info("Found relationships " + relPart); log.info("Recursing ... "); rrp = getRelationshipsPartFromZip(part, zf, relPart); part.setRelationships(rrp); } else { log.info("No relationships " + relPart); return null; } return rrp; }
// public RelationshipsPart getRelationshipsPart(ZipFile zf, Part part) public RelationshipsPart getRelationshipsPart( HashMap<String, ByteArray> partByteArrays, Part part) throws Docx4JException, InvalidFormatException { RelationshipsPart rrp = null; // recurse via this parts relationships, if it has any // String relPart = PartName.getRelationshipsPartName(target); String relPart = PartName.getRelationshipsPartName(part.getPartName().getName().substring(1)); if (partByteArrays.get(relPart) != null) { log.debug("Found relationships " + relPart); rrp = getRelationshipsPartFromZip(part, partByteArrays, relPart); part.setRelationships(rrp); } else { log.debug("No relationships " + relPart); return null; } return rrp; }
/** * @param out * @param resolvedPartUri * @param part * @throws Docx4JException * @throws IOException */ public void savePart(ZipOutputStream out, Part part) throws Docx4JException, IOException { // Drop the leading '/' String resolvedPartUri = part.getPartName().getName().substring(1); if (handled.get(resolvedPartUri) != null) { log.debug(".. duplicate save avoided .."); return; } if (part instanceof BinaryPart) { log.debug(".. saving binary stuff"); saveRawBinaryPart(out, part); } else { log.debug(".. saving "); saveRawXmlPart(out, part); } handled.put(resolvedPartUri, resolvedPartUri); // recurse via this parts relationships, if it has any RelationshipsPart rrp = part.getRelationshipsPart(false); // don't create if (rrp != null) { // log.debug("Found relationships " + rrp.getPartName() ); // Only save it if it actually has rels in it if (rrp.getRelationships().getRelationship().size() > 0) { String relPart = PartName.getRelationshipsPartName(resolvedPartUri); // log.debug("Cf constructed name " + relPart ); saveRawXmlPart(out, rrp, relPart); addPartsFromRelationships(out, rrp); } } // else { // log.debug("No relationships for " + resolvedPartUri ); // } }
/** * Get the Relationships Part (if there is one) for a given Part. Otherwise return null. * * @param zf * @param part * @return * @throws InvalidFormatException */ public RelationshipsPart getRelationshipsPart(Part part) throws Docx4JException, InvalidFormatException { RelationshipsPart rrp = null; // recurse via this parts relationships, if it has any // String relPart = PartName.getRelationshipsPartName(target); String relPart = PartName.getRelationshipsPartName(part.getPartName().getName().substring(1)); rrp = getRelationshipsPartFromZip(part, relPart); part.setRelationships(rrp); // if (partStore.partExists(relPart)) { // //if (partByteArrays.get(relPart) !=null ) { // log.debug("Found relationships " + relPart ); // rrp = getRelationshipsPartFromZip(part, relPart); // part.setRelationships(rrp); // } else { // log.debug("No relationships " + relPart ); // return null; // } return rrp; }
/** * Get a Part (except a relationships part), and all its related parts. This can be called * directly from outside the library, in which case the Part will not be owned by a Package until * the calling code makes it so. * * @param zf * @param source * @param unusedZipEntries * @param pkg * @param r * @param resolvedPartUri * @throws Docx4JException * @throws InvalidFormatException */ private void getPart(OpcPackage pkg, RelationshipsPart rp, Relationship r, ContentTypeManager ctm) throws Docx4JException, InvalidFormatException, URISyntaxException { Base source = null; String resolvedPartUri = null; if (r.getType().equals(Namespaces.HYPERLINK)) { // Could be Internal or External // Example of Internal is w:drawing/wp:inline/wp:docPr/a:hlinkClick log.info("Encountered (but not loading) hyperlink " + r.getTarget()); return; } else if (r.getTargetMode() == null || !r.getTargetMode().equals("External")) { // Usual case source = rp.getSourceP(); resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget())).toString(); // Now drop leading "/' resolvedPartUri = resolvedPartUri.substring(1); // Now normalise it .. ie abc/def/../ghi // becomes abc/ghi // Maybe this isn't necessary with a zip file, // - ZipFile class may be smart enough to do it. // But it is certainly necessary in the JCR case. // resolvedPartUri = (new java.net.URI(resolvedPartUri)).normalize().toString(); // log.info("Normalised, it is " + resolvedPartUri ); } else { // EXTERNAL if (loadExternalTargets && r.getType().equals(Namespaces.IMAGE)) { // It could instead be, for example, of type hyperlink, // and we don't want to try to fetch that log.info("Loading external resource " + r.getTarget() + " of type " + r.getType()); BinaryPart bp = ExternalResourceUtils.getExternalResource(r.getTarget()); pkg.getExternalResources().put(bp.getExternalTarget(), bp); } else { log.info( "Encountered (but not loading) external resource " + r.getTarget() + " of type " + r.getType()); } return; } String relationshipType = r.getType(); Part part; if (pkg.handled.get(resolvedPartUri) != null) { // The source Part (or Package) might have a convenience // method for this part = pkg.getParts().getParts().get(new PartName("/" + resolvedPartUri)); if (source.setPartShortcut(part, relationshipType)) { log.debug( "Convenience method established from " + source.getPartName() + " to " + part.getPartName()); } return; } part = getRawPart(ctm, resolvedPartUri, r); // will throw exception if null // The source Part (or Package) might have a convenience // method for this if (source.setPartShortcut(part, relationshipType)) { log.debug( "Convenience method established from " + source.getPartName() + " to " + part.getPartName()); } if (part instanceof BinaryPart || part instanceof DefaultXmlPart) { // The constructors of other parts should take care of this... part.setRelationshipType(relationshipType); } rp.loadPart(part, r); pkg.handled.put(resolvedPartUri, resolvedPartUri); // unusedZipEntries.put(resolvedPartUri, new Boolean(false)); RelationshipsPart rrp = getRelationshipsPart(part); if (rrp != null) { // recurse via this parts relationships, if it has any addPartsFromRelationships(part, rrp, ctm); String relPart = PartName.getRelationshipsPartName(part.getPartName().getName().substring(1)); // unusedZipEntries.put(relPart, new Boolean(false)); } }