From f673379f93ea26706451460ca9324fd974a1e6fc Mon Sep 17 00:00:00 2001 From: "chafik.meniar" Date: Mon, 10 Dec 2018 22:45:52 -0500 Subject: [PATCH] bug fixed --- .../src/ca/uqac/lif/mtnp/table/HardTable.java | 13 +- .../table/rendering/LatexTableRenderer.java | 176 ++++++++---------- .../table/rendering/TableNodeRenderer.java | 2 +- 3 files changed, 86 insertions(+), 105 deletions(-) diff --git a/Source/Core/src/ca/uqac/lif/mtnp/table/HardTable.java b/Source/Core/src/ca/uqac/lif/mtnp/table/HardTable.java index 43ff178..71957e5 100644 --- a/Source/Core/src/ca/uqac/lif/mtnp/table/HardTable.java +++ b/Source/Core/src/ca/uqac/lif/mtnp/table/HardTable.java @@ -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(""); - 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); diff --git a/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/LatexTableRenderer.java b/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/LatexTableRenderer.java index 2704c8e..63f9592 100644 --- a/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/LatexTableRenderer.java +++ b/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/LatexTableRenderer.java @@ -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 multirow package to merge cells vertically. + * Renders a result tree as a LaTeX table. The resulting table uses the + * multirow 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); } @@ -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 tabular - * environment in LaTeX must specify the alignment of each column - * before 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 tabular environment in LaTeX must + * specify the alignment of each column before 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(); @@ -75,7 +73,9 @@ 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 @@ -83,8 +83,7 @@ public static enum EnvironmentName {TABULAR, LONGTABLE}; protected EnvironmentName m_environmentName = EnvironmentName.TABULAR; @Override - public void reset() - { + public void reset() { m_numColumns = 0; m_keyBuffer.setLength(0); m_repeatedCells = 0; @@ -92,73 +91,69 @@ public void reset() /** * 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 true to put the headers in bold, - * (the default), false otherwise + * + * @param b + * Set to true to put the headers in bold, (the + * default), false 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 true to enable hyperlinks, - * false otherwise + * Sets whether the data points in the table will be enclosed in hyperlinks + * containing their ID + * + * @param b + * Set to true to enable hyperlinks, false + * 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("}"); @@ -166,127 +161,103 @@ public void printKey(StringBuilder out, String key) } @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 values, int nb_children, int max_depth, TableNode node) - { + public void printCell(StringBuilder out, List values, int nb_children, int max_depth, + TableNode node) { List 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 values, int index, int max_depth) - { - if (values.size() > 1) - { + public void printRepeatedCell(StringBuilder out, List 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("_", "\\\\_"); @@ -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]", ""); diff --git a/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/TableNodeRenderer.java b/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/TableNodeRenderer.java index ee8fa94..6ea87c8 100644 --- a/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/TableNodeRenderer.java +++ b/Source/Core/src/ca/uqac/lif/mtnp/table/rendering/TableNodeRenderer.java @@ -114,7 +114,7 @@ public void reset() protected void renderRecursive(TableNode cur_node, List 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); }