/************************************************************************ * * TableConverter.java * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * Copyright: 2002-2009 by Henrik Just * * All Rights Reserved. * * Version 1.2 (2009-06-11) * */ package writer2latex.latex; import org.w3c.dom.Element; import org.w3c.dom.Node; import writer2latex.util.*; import writer2latex.office.*; import writer2latex.latex.util.BeforeAfter; import writer2latex.latex.util.Context; enum RowType { FIRST_HEAD, HEAD, BODY, FOOT, LAST_FOOT; } /**
This class converts OpenDocument tables to LaTeX.
*The following LaTeX packages are used; some of them are optional
*array.sty, longtable.sty, supertabular.sty, tabulary.sty, hhline.sty, * colortbl.sty.
*Options:
*Constructs a new TableConverter
.
Process a table (table:table or table:sub-table tag)
* @param node The element containing the table * @param ldp theLaTeXDocumentPortion
to which
* LaTeX code should be added
* @param oc the current context
*/
public void handleTable(Element node, Element caption, boolean bCaptionAbove,
LaTeXDocumentPortion ldp, Context oc) {
// Export table, if allowed by the configuration
switch (config.tableContent()) {
case LaTeXConfig.ACCEPT:
new SingleTableConverter().handleTable(node,caption,bCaptionAbove,ldp,oc);
bContainsTables = true;
break;
case LaTeXConfig.IGNORE:
// Ignore table silently
break;
case LaTeXConfig.WARNING:
System.err.println("Warning: Tables are not allowed");
break;
case LaTeXConfig.ERROR:
ldp.append("% Error in document: A table was ignored");
}
}
// Inner class to convert a single table
private class SingleTableConverter {
private TableReader table;
private TableFormatter formatter;
private Element caption;
private boolean bCaptionAbove;
private BeforeAfter baTable;
private BeforeAfter baTableAlign;
private RowType[] rowTypes;
// Return the paragraph style of the first paragraph/heading within this block text
private String getFirstParStyle(Element node) {
Node child = node.getFirstChild();
while (child!=null) {
if (Misc.isElement(child, XMLString.TEXT_P) || Misc.isElement(child, XMLString.TEXT_H)) {
String sStyleName = Misc.getAttribute(child, XMLString.TEXT_STYLE_NAME);
if (sStyleName!=null) {
StyleWithProperties style = ofr.getParStyle(sStyleName);
if (style!=null) {
if (style.isAutomatic()) {
sStyleName = style.getParentName();
}
return ofr.getParStyles().getDisplayName(sStyleName);
}
}
return null;
}
else if (OfficeReader.isTextElement(child)) {
return getFirstParStyle((Element)child);
}
child = child.getNextSibling();
}
return null;
}
private boolean hasRowType(RowType test) {
for (RowType type : rowTypes) {
if (type==test) { return true; }
}
return false;
}
private void handleTable(Element node, Element caption, boolean bCaptionAbove,
LaTeXDocumentPortion ldp, Context oc) {
// Store the caption
this.caption = caption;
this.bCaptionAbove = bCaptionAbove;
// Read the table
table = ofr.getTableReader(node);
// Get formatter and update flags according to formatter
formatter = new TableFormatter(ofr,config,palette,table,!oc.isInMulticols(),oc.isInTable());
bContainsTables = true;
bNeedLongtable |= formatter.isLongtable();
bNeedSupertabular |= formatter.isSupertabular();
bNeedTabulary |= formatter.isTabulary();
bNeedColortbl |= formatter.isColortbl();
// Update the context
Context ic = (Context) oc.clone();
ic.setInTable(true);
ic.setInSimpleTable(formatter.isSimple());
// Never allow footnotes in tables
// (longtable.sty *does* allow footnotes in body, but not in head -
// only consistent solution is to disallow all footnotes)
ic.setNoFootnotes(true);
// Get table declarations
baTable = new BeforeAfter();
baTableAlign = new BeforeAfter();
formatter.applyTableStyle(baTable,baTableAlign,config.floatTables() && !ic.isInFrame() && !table.isSubTable());
// Identify the row types
rowTypes = new RowType[table.getRowCount()];
for (int nRow=0; nRow