protected static String findXPathStorageItemIdInContentControls(
     WordprocessingMLPackage wmlPackage) {
   FindContentControlsVisitor visitor = null;
   if ((wmlPackage.getCustomXmlDataStorageParts() != null)
       && (!wmlPackage.getCustomXmlDataStorageParts().isEmpty())) {
     try {
       visitor =
           new FindContentControlsVisitor(wmlPackage.getCustomXmlDataStorageParts().keySet());
       TraversalUtil.visit(wmlPackage, false, visitor);
     } catch (FindContentControlsVisitor.BreakException be) { // noop
     }
   }
   return (visitor != null ? visitor.getdefinedStoreItemId() : null);
 }
 protected static void insertXMLData(
     WordprocessingMLPackage wmlPackage, String storageId, Document xmlDocument)
     throws Docx4JException {
   CustomXmlDataStorage customXmlDataStorage = null;
   CustomXmlDataStoragePart customXmlDataStoragePart = null;
   customXmlDataStoragePart =
       (CustomXmlDataStoragePart)
           wmlPackage.getCustomXmlDataStorageParts().get(storageId.toLowerCase());
   customXmlDataStoragePart.getData().setDocument(xmlDocument);
 }
Example #3
0
  /**
   * This applies to any sdt which might be a conditional|repeat
   *
   * @param wordMLPackage
   * @param sdtParent
   * @param sdt
   * @param tag
   * @param sdtContent
   * @return
   * @throws Exception
   */
  private List<Object> processBindingRoleIfAny(WordprocessingMLPackage wordMLPackage, Object sdt) {
    log.debug("Processing " + getSdtPr(sdt).getId().getVal());
    Tag tag = getSdtPr(sdt).getTag();

    if (tag == null) {
      List<Object> newContent = new ArrayList<Object>();
      newContent.add(sdt);
      return newContent;
    }

    log.info(tag.getVal());

    HashMap<String, String> map = QueryString.parseQueryString(tag.getVal(), true);

    String conditionId = map.get(BINDING_ROLE_CONDITIONAL);
    String repeatId = map.get(BINDING_ROLE_REPEAT);
    String xp = map.get(BINDING_ROLE_XPATH);
    if (conditionId == null && repeatId == null && xp == null) {
      List<Object> newContent = new ArrayList<Object>();
      newContent.add(sdt);
      return newContent;
    }

    Map<String, CustomXmlPart> customXmlDataStorageParts =
        wordMLPackage.getCustomXmlDataStorageParts();

    if (conditionId != null) {

      log.info("Processing Conditional: " + tag.getVal());

      // At present, this only handles simple conditions
      Condition c = ConditionsPart.getConditionById(conditions, conditionId);
      if (c == null) {
        log.error("Missing condition " + conditionId);
      }

      if (c.evaluate(wordMLPackage, customXmlDataStorageParts, conditions, xPaths)) {
        log.debug("so keeping");

        List<Object> newContent = new ArrayList<Object>();
        newContent.add(sdt);
        return newContent;

      } else {
        return conditionFalse(sdt);
      }

    } else if (repeatId != null) {

      log.info("Processing Repeat: " + tag.getVal());

      return processRepeat(
          sdt, customXmlDataStorageParts, wordMLPackage.getMainDocumentPart().getXPathsPart());

    } else if (xp != null) {

      // Word can't handle an XPath that returns something
      // other than an element
      // eg string or boolean or number, so we'll need to work this out.
      // In principal, we could do this in this pre-processing step,
      // or via bind.xslt.

      // Doing it here means the bind.xslt step can be restricted to pure
      // Word-like processing.

      // Doing it there means we can take advantage of the multiline
      // processing we have there, and less code.
      // So as from 13 Sept 2011 (what will be 2.7.1), do it there.

      List<Object> newContent = new ArrayList<Object>();
      newContent.add(sdt);
      return newContent;
    }
    // shouldn't happen
    return null;
  }