Ejemplo n.º 1
0
  /**
   * 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;
  }
Ejemplo n.º 3
0
  /**
   * 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;
  }
Ejemplo n.º 4
0
  public static void apply(WordprocessingMLPackage otherPackage, Alterations alterations)
      throws Docx4JException, JAXBException {

    if (alterations.getContentTypes() != null) {
      log.info("replacing [Content_Types].xml");
      ContentTypeManager newCTM = new ContentTypeManager();
      newCTM.parseContentTypesFile(new ByteArrayInputStream(alterations.getContentTypes()));
      otherPackage.setContentTypeManager(newCTM);
    }

    if (alterations.isEmpty()) return;

    // -- Deletions
    List<PartName> removedParts = new ArrayList<PartName>();

    for (Alteration a : alterations.getPartsDeleted()) {

      org.docx4j.xmlPackage.Part xmlpart = a.getPart();

      // These deleted parts are likely to be attached to
      // to otherPackage, but there is no requirement for
      // that to be so.  In other words, you could try
      // to apply the alterations to some third docx.

      // It might have already been deleted as a consequence
      // of recursive deletion...
      if (removedParts.contains(xmlpart.getName())) continue;

      // Design decision: how to find owning rels part?
      // Since part might not be from this package, can't do:
      // part.getOwningRelationshipPart().removePart(p.getPartName())
      // We could store the info when AlteredParts is run,
      // but lets try to get away without that...
      // If a part has been deleted, we know its owning rels will
      // have been modified or deleted.  So look for that.
      // BUT IT WON'T BE THERE IF ITS BEEN DELETED! DOH!
      // So we have to store the info when AlteredParts is run
      // after all.

      Part parentPart = otherPackage.getParts().get(a.getSourcePartName());
      if (a.getPart().getContentType().equals(ContentTypes.RELATIONSHIPS_PART)) {
        parentPart.setRelationships(null);
      } else {
        removedParts.addAll(
            parentPart.getRelationshipsPart().removePart(new PartName(xmlpart.getName())));
      }
    }

    // -- Modifications
    for (Alteration a : alterations.getPartsModified()) {

      log.info("Applying modifications to part: " + a.getPart().getName());

      if (a.getPart().getContentType().equals(ContentTypes.RELATIONSHIPS_PART)) {

        RelationshipsPart newRP = null; // FlatOpcXmlImporter.createRelationshipsPart(a.getPart());

        if (a.getSourcePartName().equals("/")) {
          newRP = otherPackage.getRelationshipsPart(true);
          //					otherPackage.setRelationships(newRP);
          //					newRP.setSourceP(otherPackage);
        } else {
          Part parentPart = otherPackage.getParts().get(a.getSourcePartName());
          newRP = parentPart.getRelationshipsPart(true);
          //					parentPart.setRelationships(newRP);
          //					newRP.setSourceP(parentPart);
        }
        FlatOpcXmlImporter.populateRelationshipsPart(newRP, a.getPart().getXmlData().getAny());

      } else {

        Part targetPart = otherPackage.getParts().get(new PartName(a.getPart().getName()));

        if (targetPart == null) {
          log.error(
              "Couldn't find " + a.getPart().getName() + " @ " + a.getSourcePartName().getName());
          continue;
        }

        Part tmpPart =
            FlatOpcXmlImporter.getRawPart(otherPackage.getContentTypeManager(), a.getPart(), null);

        if (targetPart instanceof JaxbXmlPart) {
          ((JaxbXmlPart) targetPart).setJaxbElement(((JaxbXmlPart) tmpPart).getJaxbElement());

        } else if (targetPart instanceof XmlPart) {

          ((XmlPart) targetPart).setDocument(((XmlPart) tmpPart).getDocument());

        } else if (targetPart instanceof CustomXmlDataStoragePart) {

          ((CustomXmlDataStoragePart) targetPart)
              .setData(((CustomXmlDataStoragePart) tmpPart).getData());
          // TODO: check that

        } else if (targetPart instanceof BinaryPart) {

          ((BinaryPart) targetPart).setBinaryData(((BinaryPart) tmpPart).getBuffer());

        } else {
          log.error("TODO: handle " + targetPart.getClass().getName());
        }
      }
    }

    // -- Additions
    for (Alteration a : alterations.getPartsAdded()) {

      log.info("Adding part: " + a.getPart().getName());

      if (a.getPart().getContentType().equals(ContentTypes.RELATIONSHIPS_PART)) {

        RelationshipsPart newRP = null; // FlatOpcXmlImporter.createRelationshipsPart(a.getPart());

        if (a.getSourcePartName().equals("/")) {
          newRP = otherPackage.getRelationshipsPart(true);
          //					otherPackage.setRelationships(newRP);
          //					newRP.setSourceP(otherPackage);
        } else {
          Part parentPart = otherPackage.getParts().get(a.getSourcePartName());
          newRP = parentPart.getRelationshipsPart(true);
          //					parentPart.setRelationships(newRP);
          //					newRP.setSourceP(parentPart);
        }
        FlatOpcXmlImporter.populateRelationshipsPart(newRP, a.getPart().getXmlData().getAny());

      } else {

        Part parentPart = otherPackage.getParts().get(a.getSourcePartName());
        Part newPart =
            FlatOpcXmlImporter.getRawPart(otherPackage.getContentTypeManager(), a.getPart(), null);

        // There will already be a rel for the new part,
        // since we will already have modified or added the rels part
        // so don't do AddTargetPart (which will probably create a new rel id,
        // which will cause problems)

        newPart.setOwningRelationshipPart(parentPart.getRelationshipsPart());
        newPart
            .getSourceRelationships()
            .add(parentPart.getRelationshipsPart().getRel(new PartName(a.getPart().getName())));
        otherPackage.getParts().put(newPart);
        newPart.setPackage(otherPackage);

        // TODO: add content type if necessary
      }
    }
  }