/************************************************************************
*
* TableConverter.java
*
* Copyright: 2002-2014 by Henrik Just
*
* This file is part of Writer2LaTeX.
*
* Writer2LaTeX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Writer2LaTeX 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Writer2LaTeX. If not, see
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);
if (palette.getMathCv().handleDisplayEquation(table,ldp)) { return; }
// 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