Ejemplo n.º 1
0
  protected void createColumns(
      TableModel table, TransformState transformState, Document doc, Element tableRoot)
      throws DOMException {
    List<TblGridCol> gridCols =
        (table.getTblGrid() != null ? table.getTblGrid().getGridCol() : null);
    Element columnGroup = createNode(doc, tableRoot, NODE_TABLE_COLUMN_GROUP);
    Element column = null;

    applyColumnGroupCustomAttributes(table, transformState, columnGroup);
    if ((gridCols != null) && (!gridCols.isEmpty())) {
      for (int i = 0; i < gridCols.size(); i++) {
        column = createNode(doc, columnGroup, NODE_TABLE_COLUMN);
        applyColumnCustomAttributes(
            table, transformState, column, i, gridCols.get(i).getW().intValue());
      }
    } else {
      for (int i = 0; i < table.getColCount(); i++) {
        column = createNode(doc, columnGroup, NODE_TABLE_COLUMN);
        applyColumnCustomAttributes(table, transformState, column, i, -1);
      }
    }
  }
Ejemplo n.º 2
0
  protected void applyTableStyles(
      TableModel table, TransformState transformState, Element tableRoot) {
    List<Property> tableProperties = null;
    int tblCellSpacing = -1;

    // This handles:
    // - position (tblPr/tblInd)
    // - table-layout

    tableProperties = PropertyFactory.createProperties(table.getEffectiveTableStyle().getTblPr());

    // Borders, shading
    if (table.getEffectiveTableStyle().getTcPr() != null) {
      PropertyFactory.createPropertiesTable(
          tableProperties, table.getEffectiveTableStyle().getTcPr());
    }

    // vAlign fix: match Word's default of top
    if (table.getEffectiveTableStyle().getTcPr() == null
        || table.getEffectiveTableStyle().getTcPr().getVAlign() == null) {
      tableProperties.add(new TextAlignmentVertical());
    }

    if (!table.isDrawTableBorders()) {
      // isn't nice, but better than passing a lot of flags to the PropertyFactory
      // 1. remove any borders and shading
      for (int i = tableProperties.size() - 1; i >= 0; i--) {
        if ((tableProperties.get(i) instanceof Shading)
            || (tableProperties.get(i) instanceof AbstractBorder)) {
          tableProperties.remove(i);
        }
      }
      // 2. apply explicit none-borders and transparent shading
      //   (in html there might be borders and shading inherited from the class)
      appendNoneBordersAndShading(tableProperties);
    }

    processAttributes(tableProperties, tableRoot);

    applyTableCustomAttributes(table, transformState, tableRoot);
  }
Ejemplo n.º 3
0
  public Node toNode(Model tableModel, TransformState transformState, Document doc)
      throws TransformerException {
    TableModel table = (TableModel) tableModel;
    getLog().debug("Table asXML:\n" + table.debugStr());

    DocumentFragment docfrag = doc.createDocumentFragment();
    Element tableRoot = createNode(doc, null, NODE_TABLE);
    List<Property> rowProperties = new ArrayList<Property>();
    int rowPropertiesTableSize = -1;

    List<Property> cellProperties = new ArrayList<Property>();
    int cellPropertiesTableSize = -1;
    int cellPropertiesRowSize = -1;
    boolean inHeader = (table.getHeaderMaxRow() > -1);

    TableModelRow rowModel = null;
    Element rowContainer = null;
    Element row = null;
    Element cellNode = null;

    createRowProperties(rowProperties, table.getEffectiveTableStyle().getTrPr(), true);
    rowPropertiesTableSize = rowProperties.size();
    createCellProperties(cellProperties, table.getEffectiveTableStyle().getTrPr());
    createCellProperties(cellProperties, table.getEffectiveTableStyle().getTcPr());
    // will apply these as a default on each td, and then override
    createCellProperties(cellProperties, table.getEffectiveTableStyle().getTblPr());
    cellPropertiesTableSize = cellProperties.size();

    docfrag.appendChild(tableRoot);
    applyTableStyles(table, transformState, tableRoot);

    // setup column widths
    createColumns(table, transformState, doc, tableRoot);

    rowContainer = createNode(doc, tableRoot, (inHeader ? NODE_TABLE_HEADER : NODE_TABLE_BODY));
    tableRoot.appendChild(rowContainer);

    applyTableRowContainerCustomAttributes(table, transformState, rowContainer, inHeader);

    for (int rowIndex = 0; rowIndex < table.getCells().size(); rowIndex++) {
      rowModel = table.getCells().get(rowIndex);

      if ((inHeader) && (rowIndex > table.getHeaderMaxRow())) {
        rowContainer = createNode(doc, tableRoot, NODE_TABLE_BODY);
        tableRoot.appendChild(rowContainer);
        inHeader = false;
        applyTableRowContainerCustomAttributes(table, transformState, rowContainer, inHeader);
      }
      row = createNode(doc, rowContainer, (inHeader ? NODE_TABLE_HEADER_ROW : NODE_TABLE_BODY_ROW));
      TrPr trPr = rowModel.getRowProperties();
      CTTblPrEx tblPrEx = rowModel.getRowPropertiesExceptions();

      createRowProperties(rowProperties, trPr, false);
      processAttributes(rowProperties, row);
      applyTableRowCustomAttributes(table, transformState, row, rowIndex, inHeader);

      createCellProperties(cellProperties, trPr);
      createCellProperties(cellProperties, tblPrEx);
      cellPropertiesRowSize = cellProperties.size();

      for (Cell cell : rowModel.getRowContents()) {
        // process cell

        if (cell.isDummy()) {
          if (!cell.isVMerged()) {
            // Dummy-Cells resulting from vertical merged cells shouldn't be included
            cellNode =
                createNode(doc, row, (inHeader ? NODE_TABLE_HEADER_CELL : NODE_TABLE_BODY_CELL));
            row.appendChild(cellNode);
            applyTableCellCustomAttributes(table, transformState, cell, cellNode, inHeader, true);
          }
        } else {

          cellNode =
              createNode(doc, row, (inHeader ? NODE_TABLE_HEADER_CELL : NODE_TABLE_BODY_CELL));
          row.appendChild(cellNode);
          // Apply cell style
          createCellProperties(cellProperties, cell.getTcPr());
          processAttributes(cellProperties, cellNode);
          applyTableCellCustomAttributes(table, transformState, cell, cellNode, inHeader, false);
          // remove properties defined on cell level
          resetProperties(cellProperties, cellPropertiesRowSize);

          // insert content into cell
          // skipping w:tc node itself, insert only its children
          if (cell.getContent() == null) {
            getLog().warn("model cell had no contents!");
          } else {
            getLog().debug("copying cell contents..");
            XmlUtils.treeCopy(cell.getContent().getChildNodes(), cellNode);
          }
        }
      }
      // remove properties defined on row level
      resetProperties(cellProperties, cellPropertiesTableSize);
      resetProperties(rowProperties, rowPropertiesTableSize);
    }
    return docfrag;
  }