コード例 #1
0
  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;
  }