Exemple #1
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
    }
  }
Exemple #2
0
  /**
   * Build a table representation from a <var>tbl</var> instance. Remember to set wordMLPackage
   * before using this method!
   */
  public void build(Node node, NodeList children) throws TransformerException {

    Tbl tbl = null;
    try {
      tbl = (Tbl) XmlUtils.unmarshal(node);
    } catch (JAXBException e) {
      throw new TransformerException("Node: " + node.getNodeName() + "=" + node.getNodeValue(), e);
    }
    build(tbl, children.item(0));
  }
Exemple #3
0
  public static Object nodeToObjectModel(Node n, Class declaredType) throws Docx4JException {

    if (n == null) {
      throw new Docx4JException("null input");
    }

    //    	if (log.isDebugEnabled() ) {
    //    		System.out.println("IN: " + XmlUtils.w3CDomNodeToString(n));
    //    	}

    Object jaxb = null;
    try {
      jaxb = XmlUtils.unmarshal(n, Context.jcPML, declaredType);
    } catch (JAXBException e1) {
      throw new Docx4JException("Couldn't unmarshall " + XmlUtils.w3CDomNodeToString(n), e1);
    }
    try {
      if (jaxb instanceof JAXBElement) {

        JAXBElement jb = (JAXBElement) jaxb;
        if (jb.getDeclaredType().getName().equals(declaredType.getName())) {
          return jb.getValue();
        } else {
          log.error("UNEXPECTED " + XmlUtils.JAXBElementDebug(jb));
          throw new Docx4JException(
              "Expected " + declaredType.getName() + " but got " + XmlUtils.JAXBElementDebug(jb));
        }
      } else if (jaxb.getClass().getName().equals(declaredType.getName())) {
        return jaxb;
      } else {
        log.error(jaxb.getClass().getName());
        throw new Docx4JException(
            "Expected " + declaredType.getName() + " but got " + jaxb.getClass().getName());
      }
    } catch (ClassCastException e) {
      throw new Docx4JException(
          "Expected " + declaredType.getName() + " but got " + jaxb.getClass().getName(), e);
    }
  }
Exemple #4
0
  /* (non-Javadoc)
   * @see org.docx4j.model.Model#toJAXB()
   */
  @Override
  public Object toJAXB() {

    ObjectFactory factory = Context.getWmlObjectFactory();
    Tbl tbl = factory.createTbl();

    // <w:tblPr>
    TblPr tblPr = null;
    if (tblPr == null) {
      log.warn("tblPr is null");
      tblPr = factory.createTblPr();

      // Default to page width
      TblWidth tblWidth = factory.createTblWidth();
      tblWidth.setW(BigInteger.valueOf(DEFAULT_PAGE_WIDTH_TWIPS));
      // TODO: shouldn't hard code that.  Pass it in?
      tblWidth.setType("dxa"); // twips
      tblPr.setTblW(tblWidth);
    }
    tbl.setTblPr(tblPr);

    // <w:tblGrid>
    // This specifies the number of columns,
    // and also their width
    if (tblGrid == null) {
      log.warn("tblGrid is null");
      tblGrid = factory.createTblGrid();
      // Default to equal width
      int width = Math.round(tbl.getTblPr().getTblW().getW().floatValue() / getColCount());
      for (int i = 0; i < getColCount(); i++) {
        TblGridCol tblGridCol = factory.createTblGridCol();
        tblGridCol.setW(BigInteger.valueOf(width)); // twips
        tblGrid.getGridCol().add(tblGridCol);
      }
    }
    tbl.setTblGrid(tblGrid);

    // <w:tr>
    // we need a table row, even if every entry is just a vertical span,
    // so that is easy
    for (int i = 0; i < cells.size(); i++) {
      Tr tr = factory.createTr();
      tbl.getEGContentRowContent().add(tr);

      // populate the row
      for (int j = 0; j < getColCount(); j++) {
        Tc tc = factory.createTc();
        Cell cell = cells.get(i).get(j);
        if (cell == null) {
          // easy, nothing to do.
          // this is just an empty tc
          tr.getEGContentCellContent().add(tc);
        } else {
          if (cell.isDummy()) {
            // we need to determine whether this is a result of
            // a vertical merge or a horizontal merge
            if (j > 0
                && (cells.get(i).get(j - 1).isDummy() || cells.get(i).get(j - 1).colspan > 1)) {
              // Its a horizontal merge, so
              // just leave it out
            } else if (i > 0
                && (cells.get(i - 1).get(j).isDummy() || cells.get(i - 1).get(j).rowspan > 1)) {
              // Its a vertical merge
              TcPr tcPr = factory.createTcPr();
              VMerge vm = factory.createTcPrInnerVMerge();
              tcPr.setVMerge(vm);
              tc.setTcPr(tcPr);
              tr.getEGContentCellContent().add(tc);

              // Must have an empty paragraph
              P p = factory.createP();
              tc.getEGBlockLevelElts().add(p);
            } else {
              log.error("Encountered phantom dummy cell at (" + i + "," + j + ") ");
              log.debug(debugStr());
            }

          } else { // a real cell
            TcPr tcPr = factory.createTcPr();

            if (cell.colspan > 1) {
              // add <w:gridSpan>
              GridSpan gridSpan = factory.createTcPrInnerGridSpan();
              gridSpan.setVal(BigInteger.valueOf(cell.colspan));
              tcPr.setGridSpan(gridSpan);
              tc.setTcPr(tcPr);
            }
            if (cell.rowspan > 1) {
              // Its a vertical merge
              VMerge vm = factory.createTcPrInnerVMerge();
              vm.setVal("restart");
              tcPr.setVMerge(vm);
              tc.setTcPr(tcPr);
            }

            if (cell.colspan > 1 && cell.rowspan > 1) {
              log.warn("Both rowspan & colspan set; that will be interesting..");
            }

            tr.getEGContentCellContent().add(tc);

            // Add the cell content, if we have it.
            // We won't have compatible content if this model has
            // been created via XSLT for an outward bound conversion.
            // But in that case, this method isn't needed
            // because the developer started with the JAXB model.
            Node foreign = cell.getContent(); // eg a <td>
            for (int n = 0; n < foreign.getChildNodes().getLength(); n++) {
              Object o;
              try {
                o = XmlUtils.unmarshal(foreign.getChildNodes().item(n));
                tc.getEGBlockLevelElts().add(o);
              } catch (JAXBException e) {
                e.printStackTrace();
              }
            }
          }
        }
      }
    }

    return tbl;
  }