org.docx4j.model.table.TableModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docx4j Show documentation
Show all versions of docx4j Show documentation
docx4j is a library which helps you to work with the Office Open
XML file format as used in docx
documents, pptx presentations, and xlsx spreadsheets.
/*
Licensed to Plutext Pty Ltd under one or more contributor license agreements.
* This file is part of docx4j.
docx4j is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.docx4j.model.table;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.transform.TransformerException;
import org.docx4j.TraversalUtil;
import org.docx4j.TraversalUtil.CallbackImpl;
import org.docx4j.XmlUtils;
import org.docx4j.finders.TcFinder;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTTrPrBase;
import org.docx4j.wml.Style;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.TblGrid;
import org.docx4j.wml.TblGridCol;
import org.docx4j.wml.TblPr;
import org.docx4j.wml.Tc;
import org.docx4j.wml.Tr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* There are different ways to represent a table with possibly merged
* cells.
*
*
* - In html, both vertically and horizontally merged cells are
* represented by one cell only that has a colspan and rowspan
* attribute. No dummy cells are used.
*
*
- In docx, horizontally merged cells are represented by one cell
* with a gridSpan attribute; while vertically merged cells are
* represented as a top cell containing the actual content and a series
* of dummy cells having a vMerge tag with "continue" attribute.
*
*
- In the model used in this class, we use a regular matrix;
* dummy cells are added for both merge directions.
*
*
*
* The algorithm is as follows:
*
*
* - When a cell is added, its colspan is set. Even a dummy cell can have
* a colspan, the same value as its upper has.
*
*
- When a new cell has a colspan greater than 1, the required extra
* dummy cells are also added
*
*
- When a docx dummy cell is encountered (one with a vMerge
* continue attribute), the rowspan is incremented in its upper
* neighbors until a real cell is found.
*
*
*
* This model captures:
* - whether the table layout is fixed or auto (Word usually does auto)
* - whether conflict resolution is required on cell borders (Word usually
* does conflict resolution)
*
* @author Adam Schmideg
* @author Alberto Zerolo
* @author Jason Harrop
*
*/
public class TableModel {
private final static Logger log = LoggerFactory.getLogger(TableModel.class);
public TableModel() {
resetIndexes();
rows = new ArrayList();
headerMaxRow = -1;
}
// TODO, retire this
private final static int DEFAULT_PAGE_WIDTH_TWIPS = 12240; // LETTER; A4 would be 11907
/**
* A list of rows
*/
protected List rows;
protected int headerMaxRow;
protected int row;
protected int col;
protected int width = -1;
protected boolean drawTableBorder = true;
protected String styleId;
/**
* @return the table's style, if any
*/
public String getStyleId() {
return styleId;
}
protected Style effectiveTableStyle;
/**
* @return the table's effective Style
*/
public Style getEffectiveTableStyle() {
return effectiveTableStyle;
}
/**
* Table properties are represented using the
* docx model.
*/
protected TblPr tblPr;
/**
* @return the w:tblPr
*/
public TblPr getTblPr() {
return tblPr;
}
protected TblGrid tblGrid;
/**
* @return the w:tblGrid
*/
public TblGrid getTblGrid() {
return tblGrid;
}
// We don't need this in our table model,
// at least for HTML. (PropertyFactory takes care of it)
// boolean tableLayoutFixed = false; // default to auto
// /**
// * @return isTableLayoutFixed
// */
// public boolean isTableLayoutFixed() {
// return tableLayoutFixed;
// }
private boolean borderConflictResolutionRequired = true;
/**
* If borderConflictResolutionRequired is required, we need
* to set this explicitly, because in CSS, 'separate' is
* the default. We need to avoid incorrectly
* overruling an inherited value (ie where TblCellSpacing
* is set), so we do borderConflictResolutionRequired here,
* as an explicit \@style value, rather than that in
* conjunction with \@class.
*
* @return borderConflictResolutionRequired
*/
public boolean isBorderConflictResolutionRequired() {
return borderConflictResolutionRequired;
}
/*
* @since 3.0.0
*/
public boolean isDrawTableBorders() {
return drawTableBorder;
}
//Table width in twips, -1 = undefined
public int getTableWidth() {
return width;
}
/**
* Reset row and col.
*/
public void resetIndexes() {
row = -1;
col = -1;
}
public void startRow(Tr tr) {
rows.add(new TableModelRow(tr));
row++;
col = -1;
}
/**
* Add a new cell to this table
*/
public void addCell(Tc tc) {
++col;
log.debug("Add tc row " + row + " col " + col);
addCell(new TableModelCell(this, row, col, tc));
// add dummy horizontal cells
if (tc.getTcPr()!=null
&& tc.getTcPr().getGridSpan()!=null
&& tc.getTcPr().getGridSpan().getVal() !=null) {
int gridSpan = tc.getTcPr().getGridSpan().getVal().intValue();
addDummyCell(gridSpan, false, false);
}
log.debug("\n\n"+ this.debugStr());
}
// private void addDummyCell() {
// addDummyCell(0);
// }
protected void addDummyCell(int colSpan, boolean isBefore, boolean isAfter) {
log.debug("gridSpan, so addDummyCell " + colSpan + " " + isBefore + " " + isAfter);
TableModelCell cell;
if (colSpan>1) {
for (int i=1; i getRows() {
return rows;
}
public int getHeaderMaxRow() {
return headerMaxRow;
}
protected Tbl tbl; // for debug purposes only
public void build(Tbl tbl) {
this.tbl = tbl;
if (tbl.getTblPr()!=null
&& tbl.getTblPr().getTblStyle()!=null) {
styleId = tbl.getTblPr().getTblStyle().getVal();
}
this.tblGrid = tbl.getTblGrid();
this.tblPr = tbl.getTblPr();
// PropertyResolver pr = conversionContext.getPropertyResolver();
// effectiveTableStyle = pr.getEffectiveTableStyle(tbl.getTblPr() );
TrFinder trFinder = new TrFinder();
new TraversalUtil(tbl, trFinder);
int r = 0;
for (Tr tr : trFinder.getTrList()) {
startRow(tr);
handleRow(tr, r);
r++;
if (log.isDebugEnabled()) {
log.debug("\n\n"+ this.debugStr());
}
if (rows.get(row).getRowContents().isEmpty()) {
log.debug("removing empty row");
rows.remove(row);
row--;
r--;
}
}
// CTTblPrBase tblPr = effectiveTableStyle.getTblPr();
// if (tblPr != null) {
// if (tblPr.getTblCellSpacing()!=null) {
// borderConflictResolutionRequired = false;
// }
// }
width = calcTableWidth();
}
public static class TrFinder extends CallbackImpl {
private List trList = new ArrayList ();
@Override
public List