@Override
  public List<Object> apply(Object o) {

    //		System.out.println(o.getClass().getName());

    //		if (o instanceof P) {
    //			currentP = (P)o;
    //		}

    if (o instanceof org.docx4j.wml.FldChar) {
      FldChar fldChar = (FldChar) o;
      if (fldChar.getFldCharType().equals(STFldCharType.BEGIN)) {
        // System.out.println("Found a BEGIN");
        depth++;
        P currentP = pStack.peek();
        if (depth == 1 && !starts.contains(currentP)) {
          starts.add(currentP);
          //					System.out.println("Adding " + XmlUtils.marshaltoString(currentP));
        }
      }
      if (fldChar.getFldCharType().equals(STFldCharType.END)) {
        depth--;
      }
    }

    return null;
  }
Beispiel #2
0
  public void updateState(NodeIterator fldCharNodeIt) {
    org.docx4j.wml.FldChar field = null;

    Node node = fldCharNodeIt.nextNode();

    try {
      field =
          (org.docx4j.wml.FldChar)
              XmlUtils.unmarshal(node, Context.jc, org.docx4j.wml.FldChar.class);
    } catch (JAXBException e1) {
      e1.printStackTrace();
    }

    STFldCharType fieldCharType = field.getFldCharType();

    if (fieldCharType == null) {

      log.debug("Ignoring unrecognised: " + XmlUtils.w3CDomNodeToString(node));

    } else {

      if (fieldCharType == STFldCharType.BEGIN) {
        inField = true;
      } else if (fieldCharType == STFldCharType.END) {
        inField = false;
      }
      // else ignore STFldCharType.SEPARATE
    }
  }
  public static List<Object> getChildrenImpl(Object o) {

    if (o == null) {
      log.warn("null passed to getChildrenImpl");
      return null;
    }

    log.debug("getting children of " + o.getClass().getName());
    if (o instanceof org.docx4j.wml.Text) return null;

    // Short circuit for common elements
    if (o instanceof List) {
      // Handy if you have your own list of objects you wish to process
      return (List<Object>) o;
    } else if (o instanceof org.docx4j.wml.ContentAccessor) {
      return ((org.docx4j.wml.ContentAccessor) o).getContent();
    } else if (o instanceof org.docx4j.wml.SdtElement) {
      return ((org.docx4j.wml.SdtElement) o).getSdtContent().getContent();
    } else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Anchor) {
      org.docx4j.dml.wordprocessingDrawing.Anchor anchor =
          (org.docx4j.dml.wordprocessingDrawing.Anchor) o;
      List<Object> artificialList = new ArrayList<Object>();
      CTNonVisualDrawingProps drawingProps = anchor.getDocPr();
      if (drawingProps != null) {
        handleCTNonVisualDrawingProps(drawingProps, artificialList);
      }
      if (anchor.getGraphic() != null) {
        log.debug("found a:graphic");
        org.docx4j.dml.Graphic graphic = anchor.getGraphic();
        if (graphic.getGraphicData() != null) {
          artificialList.addAll(handleGraphicData(graphic.getGraphicData()));
        }
      }
      if (!artificialList.isEmpty()) return artificialList;
    } else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Inline) {
      org.docx4j.dml.wordprocessingDrawing.Inline inline =
          (org.docx4j.dml.wordprocessingDrawing.Inline) o;
      List<Object> artificialList = new ArrayList<Object>();
      CTNonVisualDrawingProps drawingProps = inline.getDocPr();
      if (drawingProps != null) {
        handleCTNonVisualDrawingProps(drawingProps, artificialList);
      }
      if (inline.getGraphic() != null) {
        log.debug("found a:graphic");
        org.docx4j.dml.Graphic graphic = inline.getGraphic();
        if (graphic.getGraphicData() != null) {
          artificialList.addAll(handleGraphicData(graphic.getGraphicData()));
        }
        return artificialList;
      }
      if (!artificialList.isEmpty()) return artificialList;

    } else if (o instanceof Pict) {
      return ((Pict) o).getAnyAndAny(); // (why didn't the reflection below find this?)

    } else if (o instanceof org.docx4j.dml.picture.Pic) { // Post 2.7.1; untested

      org.docx4j.dml.picture.Pic dmlPic = ((org.docx4j.dml.picture.Pic) o);
      if (dmlPic.getBlipFill() != null && dmlPic.getBlipFill().getBlip() != null) {
        log.debug("found DML Blip");
        List<Object> artificialList = new ArrayList<Object>();
        artificialList.add(dmlPic.getBlipFill().getBlip());
        return artificialList;
      } else {
        return null;
      }
    } else if (o instanceof org.docx4j.dml.CTGvmlPicture) { // Post 2.7.1

      org.docx4j.dml.CTGvmlPicture dmlPic = ((org.docx4j.dml.CTGvmlPicture) o);
      if (dmlPic.getBlipFill() != null && dmlPic.getBlipFill().getBlip() != null) {
        log.debug("found DML Blip");
        List<Object> artificialList = new ArrayList<Object>();
        artificialList.add(dmlPic.getBlipFill().getBlip());
        return artificialList;
      } else {
        return null;
      }

    } else if (o instanceof org.docx4j.vml.CTShape) {
      //			return ((org.docx4j.vml.CTShape)o).getAny();
      List<Object> artificialList = new ArrayList<Object>();
      for (JAXBElement<?> j : ((org.docx4j.vml.CTShape) o).getPathOrFormulasOrHandles()) {
        //				System.out.println(XmlUtils.unwrap(j).getClass().getName() );
        artificialList.add(j);
      }
      return artificialList;
    } else if (o instanceof CTDataModel) {
      CTDataModel dataModel = (CTDataModel) o;
      List<Object> artificialList = new ArrayList<Object>();
      // We're going to create a list merging two children ..
      artificialList.addAll(dataModel.getPtLst().getPt());
      artificialList.addAll(dataModel.getCxnLst().getCxn());
      return artificialList;
    } else if (o instanceof org.docx4j.dml.diagram2008.CTDrawing) {
      return ((org.docx4j.dml.diagram2008.CTDrawing) o).getSpTree().getSpOrGrpSp();
    } else if (o instanceof org.docx4j.vml.CTTextbox) {
      //			return ((org.docx4j.vml.CTTextbox)o).getAny();
      org.docx4j.vml.CTTextbox textBox = ((org.docx4j.vml.CTTextbox) o);
      if (textBox.getTxbxContent() == null) {
        return null;
      } else {
        return textBox.getTxbxContent().getEGBlockLevelElts();
        // grandchildren
      }
      //		} else if (o instanceof org.docx4j.wml.CTTxbxContent) {
      //			return ((org.docx4j.wml.CTTxbxContent)o).getEGBlockLevelElts();
    } else if (o instanceof CTObject) {

      CTObject ctObject = (CTObject) o;
      List<Object> artificialList = new ArrayList<Object>();
      artificialList.addAll(ctObject.getAnyAndAny());
      if (ctObject.getControl() != null) {
        artificialList.add(ctObject.getControl()); // CTControl
      }
      return artificialList;
    } else if (o instanceof org.docx4j.dml.CTGvmlGroupShape) {
      return ((org.docx4j.dml.CTGvmlGroupShape) o).getTxSpOrSpOrCxnSp();
    } else if (o instanceof FldChar) {
      FldChar fldChar = ((FldChar) o);
      List<Object> artificialList = new ArrayList<Object>();
      artificialList.add(fldChar.getFldCharType());
      if (fldChar.getFfData() != null) {
        artificialList.add(fldChar.getFfData());
      }
      if (fldChar.getFldData() != null) {
        artificialList.add(fldChar.getFldData());
      }
      if (fldChar.getNumberingChange() != null) {
        artificialList.add(fldChar.getNumberingChange());
      }
      return artificialList;
    }

    // OK, what is this? Use reflection ..
    // This should work for things including w:drawing
    log.debug(".. looking for method which returns list ");
    try {
      Method[] methods = o.getClass().getDeclaredMethods();
      for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        if (m.getReturnType().getName().equals("java.util.List")) {
          return (List<Object>) m.invoke(o);
        }
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    log.debug(".. no list member");
    return null;
  }