Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Source/Core/src/ca/uqac/lif/mtnp/table/HardTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,26 @@ protected String toHtml(String[] sort_order)
*/
protected void toHtml(TableNode cur_node, StringBuilder out, int depth, int total_depth)
{

if (depth > 0)
{
out.append("<td>");
if (cur_node.m_value.isString())

if (cur_node == null)
{
out.append(cur_node.m_value);

out.append("");
}
else if (cur_node.m_value == null)
{

out.append("");
}
else if (cur_node.m_value.isString())
{
out.append(cur_node.m_value);
}

else
{
out.append(cur_node.m_value);
Expand Down
176 changes: 74 additions & 102 deletions Source/Core/src/ca/uqac/lif/mtnp/table/rendering/LatexTableRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@
package ca.uqac.lif.mtnp.table.rendering;

import java.util.List;
import java.util.Objects;

import ca.uqac.lif.mtnp.table.PrimitiveValue;
import ca.uqac.lif.mtnp.table.Table;
import ca.uqac.lif.mtnp.table.TableNode;
import ca.uqac.lif.mtnp.table.Table.CellCoordinate;
import ca.uqac.lif.mtnp.table.TableNode;
import ca.uqac.lif.petitpoucet.NodeFunction;

/**
* Renders a result tree as a LaTeX table. The resulting table
* uses the <tt>multirow</tt> package to merge cells vertically.
* Renders a result tree as a LaTeX table. The resulting table uses the
* <tt>multirow</tt> package to merge cells vertically.
*
* @author Sylvain Hallé
*/
public class LatexTableRenderer extends TableNodeRenderer
{
public LatexTableRenderer(Table t)
{
public class LatexTableRenderer extends TableNodeRenderer {
public LatexTableRenderer(Table t) {
super(t);
}

Expand All @@ -54,11 +53,10 @@ public LatexTableRenderer(Table t)
protected boolean m_datapointHyperlinks = true;

/**
* A buffer to hold the column headers until we know how many
* columns there are. This is due to the fact that a <code>tabular</code>
* environment in LaTeX must specify the alignment of each column
* <em>before</em> the keys are listed, and so in our case before
* we know how many keys there are.
* A buffer to hold the column headers until we know how many columns there are.
* This is due to the fact that a <code>tabular</code> environment in LaTeX must
* specify the alignment of each column <em>before</em> the keys are listed, and
* so in our case before we know how many keys there are.
*/
protected final transient StringBuilder m_keyBuffer = new StringBuilder();

Expand All @@ -75,218 +73,191 @@ public LatexTableRenderer(Table t)
/**
* The name of the LaTeX environment that displays the table
*/
public static enum EnvironmentName {TABULAR, LONGTABLE};
public static enum EnvironmentName {
TABULAR, LONGTABLE
};

/**
* The name of the LaTeX environment that displays the table
*/
protected EnvironmentName m_environmentName = EnvironmentName.TABULAR;

@Override
public void reset()
{
public void reset() {
m_numColumns = 0;
m_keyBuffer.setLength(0);
m_repeatedCells = 0;
}

/**
* Sets the name of the LaTeX environment that displays the table
* @param name The name
*
* @param name
* The name
*/
public void setEnvironmentName(EnvironmentName name)
{
public void setEnvironmentName(EnvironmentName name) {
m_environmentName = name;
}

/**
* Sets whether the table's column headers are printed in bold
* @param b Set to <code>true</code> to put the headers in bold,
* (the default), <code>false</code> otherwise
*
* @param b
* Set to <code>true</code> to put the headers in bold, (the
* default), <code>false</code> otherwise
*/
public void setBoldKeys(boolean b)
{
public void setBoldKeys(boolean b) {
m_boldKeys = b;
}

/**
* Sets whether the data points in the table will be enclosed in
* hyperlinks containing their ID
* @param b Set to <code>true</code> to enable hyperlinks,
* <code>false</code> otherwise
* Sets whether the data points in the table will be enclosed in hyperlinks
* containing their ID
*
* @param b
* Set to <code>true</code> to enable hyperlinks, <code>false</code>
* otherwise
*/
public void setDatapointHyperlinks(boolean b)
{
public void setDatapointHyperlinks(boolean b) {
m_datapointHyperlinks = b;
}

/**
* Gets the LaTeX name associated to each value of EnvironmentName
* @param name The name
*
* @param name
* The name
* @return The LaTeX string
*/
protected static String getLatexEnvironmentName(EnvironmentName name)
{
if (name == EnvironmentName.LONGTABLE)
{
protected static String getLatexEnvironmentName(EnvironmentName name) {
if (name == EnvironmentName.LONGTABLE) {
return "longtable";
}
return "tabular";
}

@Override
public void startStructure(StringBuilder out)
{
public void startStructure(StringBuilder out) {
out.append("\\begin{").append(getLatexEnvironmentName(m_environmentName)).append("}");
}

@Override
public void startKeys(StringBuilder out)
{
public void startKeys(StringBuilder out) {
// Do nothing
}

@Override
public void printKey(StringBuilder out, String key)
{
if (m_keyBuffer.length() > 0)
{
public void printKey(StringBuilder out, String key) {
if (m_keyBuffer.length() > 0) {
m_keyBuffer.append(" & ");
}
if (m_boldKeys)
{
if (m_boldKeys) {
m_keyBuffer.append("\\textbf{");
}
else
{
} else {
m_keyBuffer.append("{");
}
m_keyBuffer.append(escape(key)).append("}");
m_numColumns++;
}

@Override
public void endKeys(StringBuilder out)
{
public void endKeys(StringBuilder out) {
out.append("{|");
for (int i = 0; i < m_numColumns; i++)
{
for (int i = 0; i < m_numColumns; i++) {
out.append("c|");
}
out.append("}").append(CRLF);
out.append("\\hline").append(CRLF).append(m_keyBuffer).append("\\\\").append(CRLF);
if (m_environmentName == EnvironmentName.LONGTABLE)
{
if (m_environmentName == EnvironmentName.LONGTABLE) {
out.append("\\endfirsthead").append(CRLF);
}
else
{
} else {
out.append("\\hline");
}
}

@Override
public void startBody(StringBuilder out)
{
public void startBody(StringBuilder out) {
// Do nothing
}

@Override
public void startRow(StringBuilder out, int max_depth)
{
public void startRow(StringBuilder out, int max_depth) {
m_keyBuffer.setLength(0); // Clear
m_repeatedCells = 0;
}

@Override
public void printCell(StringBuilder out, List<PrimitiveValue> values, int nb_children, int max_depth, TableNode node)
{
public void printCell(StringBuilder out, List<PrimitiveValue> values, int nb_children, int max_depth,
TableNode node) {
List<CellCoordinate> coordinates = node.getCoordinates();
if (nb_children > 2)
{
if (nb_children > 2) {
m_keyBuffer.append(" \\multirow{").append(nb_children).append("}{*}{");
}
else
{
} else {
m_keyBuffer.append("{");
}
PrimitiveValue last = values.get(values.size() - 1);
if (!last.isNull())
{
if (coordinates.size() > 0)
{
if (!Objects.isNull(last) && !last.isNull()) {
if (coordinates.size() > 0) {
CellCoordinate cc = coordinates.get(0);
String dp_id = "";
NodeFunction nf = m_table.dependsOn(cc.row, cc.col);
if (nf != null)
{
if (nf != null) {
dp_id = nf.getDataPointId();
}
m_keyBuffer.append("\\href{").append(dp_id).append("}{");
}
}
m_keyBuffer.append(escape(last.toString()));
if (coordinates.size() > 0)
{
if (coordinates.size() > 0) {
m_keyBuffer.append("}");
}
}
else
{
} else {
m_keyBuffer.append("");
}
m_keyBuffer.append("}");
if (values.size() < max_depth)
{
if (values.size() < max_depth) {
m_keyBuffer.append(" & ");
}

}

@Override
public void printRepeatedCell(StringBuilder out, List<PrimitiveValue> values, int index, int max_depth)
{
if (values.size() > 1)
{
public void printRepeatedCell(StringBuilder out, List<PrimitiveValue> values, int index, int max_depth) {
if (values.size() > 1) {
m_keyBuffer.append(" & ");
}
m_repeatedCells++;
}

@Override
public void endRow(StringBuilder out, int max_depth)
{
if (m_repeatedCells > 0)
{
public void endRow(StringBuilder out, int max_depth) {
if (m_repeatedCells > 0) {
out.append("\\cline{").append(m_repeatedCells + 1).append("-").append(max_depth).append("}").append(CRLF);
}
else
{
} else {
out.append("\\hline\n");
}
out.append(m_keyBuffer);
out.append("\\\\").append(CRLF);
out.append("\\\\").append(CRLF);
}

@Override
public void endBody(StringBuilder out)
{
public void endBody(StringBuilder out) {
// Do nothing
}

@Override
public void endStructure(StringBuilder out)
{
public void endStructure(StringBuilder out) {
out.append(CRLF).append("\\hline").append(CRLF);
out.append("\\end{").append(getLatexEnvironmentName(m_environmentName)).append("}");
}

/**
* Escapes a string for LaTeX
* @param input The input string
*
* @param input
* The input string
* @return The output string
*/
public static String escape(String input)
{
public static String escape(String input) {
String output = input;
output = output.replaceAll("\\\\", "\\\\\\\\");
output = output.replaceAll("_", "\\\\_");
Expand All @@ -301,11 +272,12 @@ public static String escape(String input)

/**
* Formats a table name to be a valid name in LaTeX
* @param name The name
*
* @param name
* The name
* @return The formatted name
*/
public static String formatName(String name)
{
public static String formatName(String name) {
String output = name;
// Keep only letters and numbers
output = output.replaceAll("[^A-Za-z0-9]", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void reset()

protected void renderRecursive(TableNode cur_node, List<PrimitiveValue> values, StringBuilder out, int max_depth)
{
if (values.size() > 0)
if (values!=null && values.size() > 0)
{
printCell(out, values, cur_node.countLeaves(), max_depth, cur_node);
}
Expand Down