All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docx4j.convert.out.common.writer.AbstractTableWriterModel Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 6.1.2
Show newest version
/*
   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.convert.out.common.writer;

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.convert.out.common.AbstractWmlConversionContext;
import org.docx4j.finders.TcFinder;
import org.docx4j.jaxb.Context;
import org.docx4j.model.PropertyResolver;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTTblPrBase;
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.docx4j.wml.TrPr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 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. *
  • This table is 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 AbstractTableWriterModel { private final static Logger log = LoggerFactory.getLogger(AbstractTableWriterModel.class); public AbstractTableWriterModel() { resetIndexes(); cells = 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 cells; private int headerMaxRow; private int row; private int col; private int width = -1; private 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; // } 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) { cells.add(new AbstractTableWriterModelRow(tr)); row++; col = -1; } /** * Add a new cell to this table and copy processed content of * tc to it. */ public void addCell(Tc tc, Node content) { addCell(new AbstractTableWriterModelCell(this, row, ++col, tc, content)); } private void addDummyCell() { addDummyCell(0); } private void addDummyCell(int colSpan) { AbstractTableWriterModelCell cell = new AbstractTableWriterModelCell(this, row, ++col); if (colSpan > 0) { cell.colspan = colSpan; } addCell(cell); } private void addCell(AbstractTableWriterModelCell cell) { cells.get(row).add(cell); } public AbstractTableWriterModelCell getCell(int row, int col) { return cells.get(row).get(col); } /** * @return "colX" where X is a 1-based index */ public String getColName(int col) { return "col" + String.valueOf(col + 1); } public int getColCount() { return cells.get(0).size(); } public List getCells() { return cells; } public int getHeaderMaxRow() { return headerMaxRow; } /** * Build a table representation from a tbl instance. * Remember to set wordMLPackage before using this method! */ public void build(AbstractWmlConversionContext conversionContext, Object node, Node content) throws TransformerException { Tbl tbl = null; try { tbl = (Tbl)node; } catch (ClassCastException e) { throw new TransformerException("Node is not of the type Tbl it is " + node.getClass().getName()); } 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() ); // if (tblPr!=null // && tblPr.getTblW()!=null) { // if (tblPr.getTblW().getType()!=null // && (tblPr.getTblW().getType().equals("auto") // || tblPr.getTblW().getType().equals("nil") )) { // // @w:type // // nil, per Word 2007 implementation note // tableLayoutFixed = false; // } else if (tblPr.getTblW().getW()!=null ){ // // @w:w // if (tblPr.getTblW().getW() == BigInteger.ZERO) { // // Word 2007 implementation note // tableLayoutFixed = false; // } else { // tableLayoutFixed = true; // } // } else { // // no attributes!! // tableLayoutFixed = false; // } // } else { // // element omitted, so type is auto (2.4.61) // tableLayoutFixed = false; // } NodeList cellContents = content.getChildNodes(); // the w:tr TrFinder trFinder = new TrFinder(); new TraversalUtil(tbl, trFinder); ensureFoTableBody(trFinder.trList); // this is currently applied to HTML etc as well int r = 0; for (Tr tr : trFinder.trList) { startRow(tr); handleRow(cellContents, tr, r); r++; if (cells.get(row).getRowContents().isEmpty()) { cells.remove(row); row--; r--; } } CTTblPrBase tblPr = effectiveTableStyle.getTblPr(); if (tblPr != null) { if (tblPr.getTblCellSpacing()!=null) { borderConflictResolutionRequired = false; } } width = calcTableWidth(); } /** * "fo:table" content model is: (marker*,table-column*,table-header?,table-footer?,table-body+) * ie table-header (if any) must precede table-body * * The first requirement is that there is a table-body. Since the docx format doesn't * have any equivalent to table-footer, we can always treat the last row as table-body. * * The second requirement is that there is no table-header after table-body. * We could either treat each t-h after a t-b as t-b, * or we could treat all t-b before t-h as t-h. * * If the docx has normal rows before the a t-h row, the user should split the table into * two. Since they can do that, we'll treat all rows before last t-h row as t-h rows * */ private void ensureFoTableBody(List rows) { int numRows = rows.size(); if (numRows==0) { log.warn("Encountered table with no rows"); return; } // Req 1: Make sure the last row is not a header row Tr lastRow = rows.get(numRows-1); if (isHeaderRow(lastRow)) { List> cnfStyleOrDivIdOrGridBefore = lastRow.getTrPr().getCnfStyleOrDivIdOrGridBefore(); JAXBElement tblHeader = getElement(cnfStyleOrDivIdOrGridBefore, "tblHeader"); cnfStyleOrDivIdOrGridBefore.remove(tblHeader); } // Req 2: All rows before last header row become header rows // .. find last header row int indexOfLastHeaderRow=-1; for (int i = rows.size(); i>0; i--) { Tr tr = rows.get(i-1); if (isHeaderRow(tr)) { indexOfLastHeaderRow = i-1; break; } } // .. now convert all rows up to that one for (int i = 0; i booleandefaulttrueWrapped = Context.getWmlObjectFactory().createCTTrPrBaseTblHeader(booleandefaulttrue); trpr.getCnfStyleOrDivIdOrGridBefore().add( booleandefaulttrueWrapped); } } } static class TrFinder extends CallbackImpl { List trList = new ArrayList(); @Override public List apply(Object o) { if (o instanceof Tr ) { trList.add((Tr)o); } return null; } @Override public boolean shouldTraverse(Object o) { // Yes, unless its a nested Tbl return !(o instanceof Tbl); } } /* * TrFinder and TcFinder can find tr and tc in a complex * case such as the following: * Desscription Price banana 10 apple 20 */ private void handleRow(NodeList cellContents, Tr tr, int r) { int gridAfter = getGridAfter(tr); int gridBefore = getGridBefore(tr); boolean headerRow = isHeaderRow(tr); log.debug("Processing r " + r); if (borderConflictResolutionRequired && tr.getTblPrEx() != null && tr.getTblPrEx().getTblCellSpacing() != null) { borderConflictResolutionRequired = false; } if (headerRow && (headerMaxRow < r)) { headerMaxRow = r; } if (drawTableBorder) { drawTableBorder = (gridBefore == 0) && (gridAfter == 0); } TcFinder tcFinder = new TcFinder(); new TraversalUtil(tr, tcFinder); //add dummy cell for gridBefore if (gridBefore > 0) { addDummyCell(gridBefore); } //List cells = tr.getEGContentCellContent(); int c = 0; log.debug("Processing c " + c); for (Tc tc : tcFinder.tcList) { Node wtrNode = cellContents.item(r); // w:tr if (wtrNode==null ) { log.warn("Couldn't find item " + r); } addCell(tc, getTc(wtrNode, c, new IntRef(0))); // the cell content // addCell(tc, cellContents.item(i)); // i++; c++; } //add dummy cell for gridAfter if (gridAfter > 0) { addDummyCell(gridAfter); } } protected boolean isHeaderRow(Tr tr) { List> cnfStyleOrDivIdOrGridBefore = (tr.getTrPr() != null ? tr.getTrPr().getCnfStyleOrDivIdOrGridBefore() : null); JAXBElement element = getElement(cnfStyleOrDivIdOrGridBefore, "tblHeader"); BooleanDefaultTrue boolVal = (element != null ? (BooleanDefaultTrue)element.getValue() : null); return (boolVal != null ? boolVal.isVal() : false); } protected int getGridAfter(Tr tr) { List> cnfStyleOrDivIdOrGridBefore = (tr.getTrPr() != null ? tr.getTrPr().getCnfStyleOrDivIdOrGridBefore() : null); JAXBElement element = getElement(cnfStyleOrDivIdOrGridBefore, "gridAfter"); CTTrPrBase.GridAfter gridAfter = (element != null ? (CTTrPrBase.GridAfter)element.getValue() : null); BigInteger val = (gridAfter != null ? gridAfter.getVal() : null); return (val != null ? val.intValue() : 0); } protected int getGridBefore(Tr tr) { List> cnfStyleOrDivIdOrGridBefore = (tr.getTrPr() != null ? tr.getTrPr().getCnfStyleOrDivIdOrGridBefore() : null); JAXBElement element = getElement(cnfStyleOrDivIdOrGridBefore, "gridBefore"); CTTrPrBase.GridBefore gridBefore = (element != null ? (CTTrPrBase.GridBefore)element.getValue() : null); BigInteger val = (gridBefore != null ? gridBefore.getVal() : null); return (val != null ? val.intValue() : 0); } protected JAXBElement getElement(List> cnfStyleOrDivIdOrGridBefore, String localName) { JAXBElement element = null; if ((cnfStyleOrDivIdOrGridBefore != null) && (!cnfStyleOrDivIdOrGridBefore.isEmpty())) { for (int i=0; i gridCols = (getTblGrid() != null ? getTblGrid().getGridCol() : null); //The calculation is done the way it was done in the TableWriter. This isn't necesarily correct, //as cell-widths may override column widths. if ((gridCols != null) && (!gridCols.isEmpty())) { ret = 0; for(int i=0; i rowContents = row.getRowContents(); for (AbstractTableWriterModelCell c : rowContents) { if (c==null) { buf.append("null "); } else { buf.append(c.debugStr()); } } buf.append("\n"); } return buf.toString(); } }