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));
     }
   }
 }
Пример #2
0
  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);
          }
        }
      }
    }
  }
Пример #3
0
  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;
  }
Пример #4
0
  /**
   * Traverse the document, and return a map of all styles which are used directly in the document.
   * (IE this does not include styles on which others are just BasedOn).
   *
   * @return
   */
  public Set<String> getStylesInUse() {

    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) this.getJaxbElement();
    Body body = wmlDocumentEl.getBody();

    List<Object> bodyChildren = body.getContent();

    Set<String> stylesInUse = new HashSet<String>();
    FontAndStyleFinder finder = new FontAndStyleFinder(null, null, stylesInUse);
    finder.defaultCharacterStyle = this.getStyleDefinitionsPart().getDefaultCharacterStyle();
    finder.defaultParagraphStyle = this.getStyleDefinitionsPart().getDefaultParagraphStyle();

    new TraversalUtil(bodyChildren, finder);
    finder.finish();

    // Styles in headers, footers?
    RelationshipsPart rp = this.getRelationshipsPart();
    if (rp != null) {
      for (Relationship r : rp.getRelationships().getRelationship()) {
        Part part = rp.getPart(r);
        if (part instanceof FooterPart) {

          Ftr ftr = ((FooterPart) part).getJaxbElement();
          finder.walkJAXBElements(ftr);

        } else if (part instanceof HeaderPart) {

          Hdr hdr = ((HeaderPart) part).getJaxbElement();
          finder.walkJAXBElements(hdr);
        }
      }
    }

    // Styles in endnotes, footnotes?
    if (this.getEndNotesPart() != null) {
      log.debug("Looking at endnotes");
      CTEndnotes endnotes = this.getEndNotesPart().getJaxbElement();
      finder.walkJAXBElements(endnotes);
    }
    if (this.getFootnotesPart() != null) {
      log.debug("Looking at footnotes");
      CTFootnotes footnotes = this.getFootnotesPart().getJaxbElement();
      finder.walkJAXBElements(footnotes);
    }

    // Comments
    if (this.getCommentsPart() != null) {
      log.debug("Looking at comments");
      Comments comments = this.getCommentsPart().getJaxbElement();
      finder.walkJAXBElements(comments);
    }

    return stylesInUse;
  }
  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;
  }
Пример #6
0
  /**
   * Traverse the document, looking for fonts which have been applied, either directly, or via a
   * style.
   *
   * @return
   */
  public Set<String> fontsInUse() {

    log.info("fontsInUse..");

    getPropertyResolver(); // this inits our virtual DocDefaults style

    // Setup

    Set<String> fontsDiscovered = new java.util.HashSet<String>();

    //    	// Keep track of styles we encounter, so we can
    //    	// inspect these for fonts
    //    	Set<String> stylesInUse = new java.util.HashSet<String>();
    //
    //		org.docx4j.wml.Styles styles = null;
    //		if (this.getStyleDefinitionsPart()!=null) {
    //			styles = (org.docx4j.wml.Styles)this.getStyleDefinitionsPart().getJaxbElement();
    //		}
    //		// It is convenient to have a HashMap of styles
    //		Map<String, Style> stylesDefined = new java.util.HashMap<String, Style>();
    //		if (styles!=null) {
    //		     for (Iterator<Style> iter = styles.getStyle().iterator(); iter.hasNext();) {
    //		            Style s = iter.next();
    //		            stylesDefined.put(s.getStyleId(), s);
    //		     }
    //		}
    //    // We need to know what fonts and styles are used in the document

    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) this.getJaxbElement();
    Body body = wmlDocumentEl.getBody();

    List<Object> bodyChildren = body.getContent();

    FontDiscoveryCharacterVisitor visitor = new FontDiscoveryCharacterVisitor(fontsDiscovered);
    RunFontSelector runFontSelector =
        new RunFontSelector(
            (WordprocessingMLPackage) this.pack, visitor, RunFontActionType.DISCOVERY);

    FontAndStyleFinder finder = new FontAndStyleFinder(runFontSelector, fontsDiscovered, null);
    finder.defaultCharacterStyle = this.getStyleDefinitionsPart().getDefaultCharacterStyle();
    finder.defaultParagraphStyle = this.getStyleDefinitionsPart().getDefaultParagraphStyle();
    new TraversalUtil(bodyChildren, finder);
    //		finder.finish();

    fontsDiscovered.add(runFontSelector.getDefaultFont());

    // fonts in headers, footers?
    RelationshipsPart rp = this.getRelationshipsPart();
    if (rp != null) {
      for (Relationship r : rp.getRelationships().getRelationship()) {
        Part part = rp.getPart(r);
        if (part instanceof FooterPart) {

          Ftr ftr = ((FooterPart) part).getJaxbElement();
          finder.walkJAXBElements(ftr);

        } else if (part instanceof HeaderPart) {

          Hdr hdr = ((HeaderPart) part).getJaxbElement();
          finder.walkJAXBElements(hdr);
        }
      }
    }

    // Styles in endnotes, footnotes?
    if (this.getEndNotesPart() != null) {
      log.debug("Looking at endnotes");
      CTEndnotes endnotes = this.getEndNotesPart().getJaxbElement();
      finder.walkJAXBElements(endnotes);
    }
    if (this.getFootnotesPart() != null) {
      log.debug("Looking at footnotes");
      CTFootnotes footnotes = this.getFootnotesPart().getJaxbElement();
      finder.walkJAXBElements(footnotes);
    }

    // Comments
    if (this.getCommentsPart() != null) {
      log.debug("Looking at comments");
      Comments comments = this.getCommentsPart().getJaxbElement();
      finder.walkJAXBElements(comments);
    }

    // Add fonts used in the styles we discovered
    // .. 2013 03 10: no longer necessary

    // Fonts can also be used in the numbering part
    // For now, treat any font mentioned in that part as in use.
    // Ideally, we'd only register fonts used in numbering levels
    // that were actually used in the document
    if (getNumberingDefinitionsPart() != null) {
      Numbering numbering = getNumberingDefinitionsPart().getJaxbElement();
      for (Numbering.AbstractNum abstractNumNode : numbering.getAbstractNum()) {
        for (Lvl lvl : abstractNumNode.getLvl()) {
          if (lvl.getRPr() != null && lvl.getRPr().getRFonts() != null) {
            String fontName = lvl.getRPr().getRFonts().getAscii();
            if (fontName != null) {
              fontsDiscovered.add(fontName);
              log.debug(
                  "Registered "
                      + fontName
                      + " for abstract list "
                      + abstractNumNode.getAbstractNumId()
                      + " lvl "
                      + lvl.getIlvl());
            }
          }
        }
      }
    }

    return fontsDiscovered;
  }
Пример #7
0
  /* 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);
      }
    }
  }