/* recursively (i) create new Parts for each thing listed in the relationships (ii) add the new Part to the package (iii) cross the PartName off unusedZipEntries */ private void addPartsFromRelationships(Base source, RelationshipsPart rp, ContentTypeManager ctm) throws Docx4JException { OpcPackage pkg = source.getPackage(); for (Relationship r : rp.getRelationships().getRelationship()) { log.debug( "\n For Relationship Id=" + r.getId() + " Source is " + rp.getSourceP().getPartName() + ", Target is " + r.getTarget() + ", type: " + r.getType()); // This is usually the first logged comment for // a part, so start with a line break. try { getPart(pkg, rp, r, ctm); } catch (Exception e) { throw new Docx4JException("Failed to add parts from relationships", e); } } }
protected static void removeDefinedCustomXmlParts( WordprocessingMLPackage wmlPackage, String xpathStorageItemId) { List<PartName> partsToRemove = new ArrayList<PartName>(); RelationshipsPart relationshipsPart = wmlPackage.getMainDocumentPart().getRelationshipsPart(); List<Relationship> relationshipsList = ((relationshipsPart != null) && (relationshipsPart.getRelationships() != null) ? relationshipsPart.getRelationships().getRelationship() : null); Part part = null; CustomXmlDataStoragePart dataPart = null; if (relationshipsList != null) { for (Relationship relationship : relationshipsList) { if (Namespaces.CUSTOM_XML_DATA_STORAGE.equals(relationship.getType())) { part = relationshipsPart.getPart(relationship); if (IsPartToRemove(part, xpathStorageItemId)) { partsToRemove.add(part.getPartName()); } } } } if (!partsToRemove.isEmpty()) { for (int i = 0; i < partsToRemove.size(); i++) { relationshipsPart.removePart(partsToRemove.get(i)); } } }
protected static Set<ContentAccessor> getParts(WordprocessingMLPackage srcPackage) { Set<ContentAccessor> partList = new HashSet<ContentAccessor>(); partList.add(srcPackage.getMainDocumentPart()); // Add headers/footers RelationshipsPart rp = srcPackage.getMainDocumentPart().getRelationshipsPart(); for (Relationship r : rp.getRelationships().getRelationship()) { if (r.getType().equals(Namespaces.HEADER)) { partList.add((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { partList.add((FooterPart) rp.getPart(r)); } } return partList; }
// private void addPartsFromRelationships(ZipFile zf, Base source, RelationshipsPart rp) private void addPartsFromRelationships( HashMap<String, ByteArray> partByteArrays, Base source, RelationshipsPart rp, ContentTypeManager ctm) throws Docx4JException { OpcPackage pkg = source.getPackage(); // for (Iterator it = rp.iterator(); it.hasNext(); ) { // Relationship r = (Relationship)it.next(); // log.info("For Relationship Id=" + r.getId() + " Source is " // + r.getSource().getPartName() // + ", Target is " + r.getTargetURI() ); // try { // // getPart(zf, pkg, rp, r); // // } catch (Exception e) { // throw new Docx4JException("Failed to add parts from relationships", e); // } // } for (Relationship r : rp.getRelationships().getRelationship()) { log.debug( "\n For Relationship Id=" + r.getId() + " Source is " + rp.getSourceP().getPartName() + ", Target is " + r.getTarget() + ", type: " + r.getType()); // This is usually the first logged comment for // a part, so start with a line break. try { getPart(partByteArrays, pkg, rp, r, ctm); } catch (Exception e) { throw new Docx4JException("Failed to add parts from relationships", e); } } }
protected static boolean IsPartToRemove(Part part, String xpathStorageItemId) { boolean ret = false; RelationshipsPart relationshipsPart = part.getRelationshipsPart(); List<Relationship> relationshipsList = ((relationshipsPart != null) && (relationshipsPart.getRelationships() != null) ? relationshipsPart.getRelationships().getRelationship() : null); CustomXmlDataStoragePropertiesPart propertiesPart = null; DatastoreItem datastoreItem = null; if ((relationshipsList != null) && (!relationshipsList.isEmpty())) { for (Relationship relationship : relationshipsList) { if (Namespaces.CUSTOM_XML_DATA_STORAGE_PROPERTIES.equals(relationship.getType())) { propertiesPart = (CustomXmlDataStoragePropertiesPart) relationshipsPart.getPart(relationship); break; } } } if (propertiesPart != null) { datastoreItem = propertiesPart.getJaxbElement(); } if (datastoreItem != null) { if ((datastoreItem.getItemID() != null) && (datastoreItem.getItemID().length() > 0)) { ret = datastoreItem.getItemID().equals(xpathStorageItemId); } if ((!ret) && (datastoreItem.getSchemaRefs() != null) && (datastoreItem.getSchemaRefs().getSchemaRef() != null) && (!datastoreItem.getSchemaRefs().getSchemaRef().isEmpty())) { for (SchemaRef ref : datastoreItem.getSchemaRefs().getSchemaRef()) { if (PART_TO_REMOVE_SCHEMA_TYPES.contains(ref.getUri())) { ret = true; break; } } } } return ret; }
public static void visit( WordprocessingMLPackage wmlPackage, boolean bodyOnly, Callback callback) { MainDocumentPart mainDocument = null; RelationshipsPart relPart = null; List<Relationship> relList = null; List<Object> elementList = null; if ((wmlPackage != null) && (callback != null)) { mainDocument = wmlPackage.getMainDocumentPart(); callback.walkJAXBElements(mainDocument.getJaxbElement().getBody()); if (!bodyOnly) { relPart = mainDocument.getRelationshipsPart(); relList = relPart.getRelationships().getRelationship(); for (Relationship rs : relList) { elementList = null; if (Namespaces.HEADER.equals(rs.getType())) { elementList = ((HeaderPart) relPart.getPart(rs)).getJaxbElement().getContent(); } else if (Namespaces.FOOTER.equals(rs.getType())) { elementList = ((FooterPart) relPart.getPart(rs)).getJaxbElement().getContent(); } else if (Namespaces.ENDNOTES.equals(rs.getType())) { // elementList = ((EndnotesPart) relPart.getPart(rs)).getContent(); elementList = new ArrayList(); elementList.addAll(((EndnotesPart) relPart.getPart(rs)).getJaxbElement().getEndnote()); } else if (Namespaces.FOOTNOTES.equals(rs.getType())) { // elementList = ((FootnotesPart) relPart.getPart(rs)).getContent(); elementList = new ArrayList(); elementList.addAll( ((FootnotesPart) relPart.getPart(rs)).getJaxbElement().getFootnote()); } else if (Namespaces.COMMENTS.equals(rs.getType())) { elementList = new ArrayList(); for (Comment comment : ((CommentsPart) relPart.getPart(rs)).getJaxbElement().getComment()) { elementList.addAll(comment.getEGBlockLevelElts()); } } if ((elementList != null) && (!elementList.isEmpty())) { log.debug("Processing target: " + rs.getTarget() + ", type: " + rs.getType()); callback.walkJAXBElements(elementList); } } } } }
/** * 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)); } }
/* recursively (i) get each Part listed in the relationships (ii) add the Part to the zip file (iii) traverse its relationship */ public void addPartsFromRelationships(ZipOutputStream out, RelationshipsPart rp) throws Docx4JException { // for (Iterator it = rp.iterator(); it.hasNext(); ) { // Relationship r = (Relationship)it.next(); // log.info("For Relationship Id=" + r.getId() + " Source is " + r.getSource().getPartName() + // ", Target is " + r.getTargetURI() ); for (Relationship r : rp.getRelationships().getRelationship()) { log.debug( "For Relationship Id=" + r.getId() + " Source is " + rp.getSourceP().getPartName() + ", Target is " + r.getTarget()); if (r.getType().equals(Namespaces.HYPERLINK)) { continue; // whether internal or external } if (r.getTargetMode() != null && r.getTargetMode().equals("External")) { // ie its EXTERNAL // As at 1 May 2008, we don't have a Part for these; // there is just the relationship. log.warn("Encountered external resource " + r.getTarget() + " of type " + r.getType()); // So continue; } try { // String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() // ).toString(); String 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. // target = (new java.net.URI(target)).normalize().toString(); // log.info("Normalised, it is " + target ); // Document contents = getDocumentFromZippedPart( zf, target); if (!false) { log.debug("Getting part /" + resolvedPartUri); // Part part = p.getParts().get(new PartName("/" + resolvedPartUri)); Part part = rp.getPart(r); // 2012 09 26: If the part is actually attached to // a different package, using this you can still get it. // Use this 'feature' at your own risk! if (part == null) { log.error("Part " + resolvedPartUri + " not found!"); } else { log.debug(part.getClass().getName()); } if (!part.getPackage().equals(p)) { log.warn("Part " + resolvedPartUri + " is attached to some other package"); } savePart(out, part); } } catch (Exception e) { throw new Docx4JException( "Failed to add parts from relationships of " + rp.getSourceP().getPartName(), e); } } }