diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java b/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java index 36d07125..80ac935e 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java @@ -21,12 +21,14 @@ import org.apache.sqoop.common.SqoopException; import org.apache.sqoop.shell.core.ShellError; import org.apache.sqoop.shell.core.Constants; +import org.apache.sqoop.shell.utils.TableDisplayer; import org.codehaus.groovy.tools.shell.IO; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.text.MessageFormat; +import java.util.List; import java.util.Locale; import java.util.ResourceBundle; @@ -57,6 +59,18 @@ private ShellEnvironment() { private static boolean interactive = false; private static long pollTimeout = DEFAULT_POLL_TIMEOUT; + private static TableDisplayer tableDisplayer = new TableDisplayer(new TableDisplayer.TableDisplayerWriter() { + @Override + public void append(String text) { + print(text); + } + + @Override + public void newLineAndFlush() { + println(); + } + }, TableDisplayer.DEFAULT_STYLE); + static ResourceBundle resource = ResourceBundle.getBundle(Constants.RESOURCE_NAME, Locale.getDefault()); static SqoopClient client = new SqoopClient(getServerUrl()); static IO io; @@ -226,6 +240,10 @@ public static void print(String format, Object... args) { io.out.printf(format, args); } + public static void displayTable(List headers, List ...columns) { + tableDisplayer.display(headers, columns); + } + // for tests only public static void cleanup() { serverHost = DEFAULT_SERVER_HOST; diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java index d973499d..c90fe941 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java @@ -27,7 +27,6 @@ import org.apache.sqoop.common.SqoopException; import org.apache.sqoop.model.MConnector; import org.apache.sqoop.shell.core.Constants; -import org.apache.sqoop.shell.utils.TableDisplayer; import org.apache.sqoop.validation.Status; import static org.apache.sqoop.shell.ShellEnvironment.*; @@ -83,7 +82,7 @@ private void showSummary() { supportedDirections.add(connector.getSupportedDirections().toString()); } - TableDisplayer.display(header, uniqueNames, versions, classes, supportedDirections); + displayTable(header, uniqueNames, versions, classes, supportedDirections); } private void showConnectors() { diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java index ebbfe828..139e7565 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java @@ -19,12 +19,8 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.OptionBuilder; -import org.apache.sqoop.common.Direction; -import org.apache.sqoop.model.MConnector; import org.apache.sqoop.model.MJob; -import org.apache.sqoop.model.MLink; import org.apache.sqoop.shell.core.Constants; -import org.apache.sqoop.shell.utils.TableDisplayer; import org.apache.sqoop.validation.Status; import java.text.DateFormat; @@ -97,7 +93,7 @@ private void showSummary() { availabilities.add(String.valueOf(job.getEnabled())); } - TableDisplayer.display(header, ids, names, fromConnectors, toConnectors, availabilities); + displayTable(header, ids, names, fromConnectors, toConnectors, availabilities); } private void showJobs(String jArg) { diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowLinkFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowLinkFunction.java index 25bd1dba..4976c98f 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowLinkFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowLinkFunction.java @@ -19,19 +19,13 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.OptionBuilder; -import org.apache.sqoop.common.SqoopException; -import org.apache.sqoop.model.MConnector; import org.apache.sqoop.model.MLink; import org.apache.sqoop.shell.core.Constants; -import org.apache.sqoop.shell.utils.TableDisplayer; import org.apache.sqoop.validation.Status; import java.text.DateFormat; -import java.util.ArrayList; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; -import java.util.Map; import static org.apache.sqoop.shell.ShellEnvironment.*; import static org.apache.sqoop.shell.utils.ConfigDisplayer.*; @@ -86,7 +80,7 @@ private void showSummary() { availabilities.add(String.valueOf(link.getEnabled())); } - TableDisplayer.display(header, names, connectorNames, availabilities); + displayTable(header, names, connectorNames, availabilities); } private void showLinks() { diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java index a450aafd..45abf148 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java @@ -22,13 +22,13 @@ import org.apache.sqoop.model.MPrincipal; import org.apache.sqoop.model.MRole; import org.apache.sqoop.shell.core.Constants; -import org.apache.sqoop.shell.utils.TableDisplayer; import org.apache.sqoop.validation.Status; import java.util.LinkedList; import java.util.List; import static org.apache.sqoop.shell.ShellEnvironment.client; +import static org.apache.sqoop.shell.ShellEnvironment.displayTable; import static org.apache.sqoop.shell.ShellEnvironment.resourceString; @SuppressWarnings("serial") @@ -70,6 +70,6 @@ private void showPrincipals(MRole role) { types.add(principal.getType()); } - TableDisplayer.display(header, names, types); + displayTable(header, names, types); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java index 2cf69724..efd4b194 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java @@ -25,13 +25,13 @@ import org.apache.sqoop.model.MResource; import org.apache.sqoop.shell.core.Constants; import org.apache.sqoop.shell.core.ShellError; -import org.apache.sqoop.shell.utils.TableDisplayer; import org.apache.sqoop.validation.Status; import java.util.LinkedList; import java.util.List; import static org.apache.sqoop.shell.ShellEnvironment.client; +import static org.apache.sqoop.shell.ShellEnvironment.displayTable; import static org.apache.sqoop.shell.ShellEnvironment.resourceString; @SuppressWarnings("serial") @@ -108,6 +108,6 @@ private void showPrivileges(MPrincipal principal, MResource resource) { withGrant.add(Boolean.toString(privilege.isWith_grant_option())); } - TableDisplayer.display(header, actions, resourceNames, resourceTypes, withGrant); + displayTable(header, actions, resourceNames, resourceTypes, withGrant); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java index 6b619210..91ceff30 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java @@ -24,13 +24,13 @@ import org.apache.sqoop.model.MRole; import org.apache.sqoop.shell.core.Constants; import org.apache.sqoop.shell.core.ShellError; -import org.apache.sqoop.shell.utils.TableDisplayer; import org.apache.sqoop.validation.Status; import java.util.LinkedList; import java.util.List; import static org.apache.sqoop.shell.ShellEnvironment.client; +import static org.apache.sqoop.shell.ShellEnvironment.displayTable; import static org.apache.sqoop.shell.ShellEnvironment.resourceString; @SuppressWarnings("serial") @@ -83,6 +83,6 @@ private void showRoles(MPrincipal principal) { names.add(role.getName()); } - TableDisplayer.display(header, names); + displayTable(header, names); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java index 89899134..c7f371f5 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java @@ -25,7 +25,6 @@ import org.apache.sqoop.model.MSubmission; import org.apache.sqoop.shell.core.Constants; import org.apache.sqoop.shell.utils.SubmissionDisplayer; -import org.apache.sqoop.shell.utils.TableDisplayer; import org.apache.sqoop.validation.Status; import static org.apache.sqoop.shell.ShellEnvironment.*; @@ -91,7 +90,7 @@ private void showSummary(String jArg) { dates.add(submission.getLastUpdateDate().toString()); } - TableDisplayer.display(header, jnames, eids, status, dates); + displayTable(header, jnames, eids, status, dates); } private void showSubmissions(String jArg) { diff --git a/shell/src/main/java/org/apache/sqoop/shell/utils/TableDisplayer.java b/shell/src/main/java/org/apache/sqoop/shell/utils/TableDisplayer.java index 51030d03..8b4f1f6b 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/utils/TableDisplayer.java +++ b/shell/src/main/java/org/apache/sqoop/shell/utils/TableDisplayer.java @@ -22,62 +22,154 @@ import java.util.LinkedList; import java.util.List; -import static org.apache.sqoop.shell.ShellEnvironment.*; - /** * Display table based data */ public class TableDisplayer { + public static class Style { + // Various characters that should be used to "draw" the table's columns, lines, ... + String headerLineSeparator; + String lineSeparator; + boolean linesAfterEachRow; + + // Generated ones + String columnLeft; + String columnRight; + String columnMiddle; + + String lineLeft; + String lineRight; + String lineMiddle; + + String headerLeft; + String headerRight; + String headerMiddle; + + public Style(String headerLineSeparator, String lineSeparator, String columnSeparator, String lineColumnSeparator, boolean linesAfterEachRow) { + this.headerLineSeparator = headerLineSeparator; + this.lineSeparator = lineSeparator; + this.linesAfterEachRow = linesAfterEachRow; + + this.columnLeft = columnSeparator + " "; + this.columnRight = " " + columnSeparator; + this.columnMiddle = " " + columnSeparator + " "; + + this.lineLeft = "+" + lineSeparator; + this.lineRight = lineSeparator + "+"; + this.lineMiddle = lineSeparator + "+" + lineSeparator; + + this.headerLeft = "+" + headerLineSeparator; + this.headerRight = headerLineSeparator + "+"; + this.headerMiddle = headerLineSeparator + "+" + headerLineSeparator; + } + } + + public static final Style DEFAULT_STYLE = new Style("-", "-", "|", "+", false); + public static final Style RST_STYLE = new Style("=", "-", "|", "+", true); + + /** + * Interface that this displayer will use to write out formatted text. + */ + public interface TableDisplayerWriter { + /** + * Print out addition formatted text to the output + * + * @param text + */ + void append(String text); + + /** + * Flush and print new line + */ + void newLineAndFlush(); + } + + public TableDisplayer(TableDisplayerWriter writer, Style style) { + setWriter(writer); + this.style = style; + } + + /** + * Writer instance that we should use to write something out + */ + private TableDisplayerWriter writer; + + /** + * Style that should be used to the generated tables + */ + private Style style; + + /** + * Reset the writer if needed. + * + * @param writer + */ + public void setWriter(TableDisplayerWriter writer) { + this.writer = writer; + } + /** * Display given columns in nice table structure to given IO object. * * @param headers List of headers * @param columns Array of columns */ - public static void display(List headers, List ...columns) { - assert headers != null; + public void display(List headers, List ...columns) { assert columns != null; - assert headers.size() == columns.length; + assert columns.length >= 1; + assert writer != null; + if(headers != null) { + assert headers.size() == columns.length; + } // Count of columns - int columnCount = headers.size(); + int columnCount = columns.length; // List of all maximal widths of each column List widths = new LinkedList(); for(int i = 0; i < columnCount; i++) { - widths.add(getMaximalWidth(headers.get(i), columns[i])); + widths.add(getMaximalWidth(headers != null ? headers.get(i) : null, columns[i])); } // First line is border drawLine(widths); - // Print out header (text is centralised) - print("| "); - for(int i = 0 ; i < columnCount; i++) { - print(StringUtils.center(headers.get(i), widths.get(i), ' ')); - print((i == columnCount -1) ? " |" : " | "); - } - println(); + if(headers != null) { + // Print out header (text is centralised) + print(style.columnLeft); + for (int i = 0; i < columnCount; i++) { + print(StringUtils.center(headers.get(i), widths.get(i), ' ')); + print((i == columnCount - 1) ? style.columnRight : style.columnMiddle); + } + println(); - // End up header by border - drawLine(widths); + // End up header by border + drawHeaderLine(widths); + } // Number of rows in the table int rows = getMaximalRows(columns); // Print out each row for(int row = 0 ; row < rows; row++) { - print("| "); + print(style.columnLeft); for(int i = 0 ; i < columnCount; i++) { print(StringUtils.rightPad(columns[i].get(row), widths.get(i), ' ')); - print((i == columnCount -1) ? " |" : " | "); + print((i == columnCount - 1) ? style.columnRight : style.columnMiddle); + } println(); + + if(style.linesAfterEachRow) { + drawLine(widths); + } } // End table by final border - drawLine(widths); + if(!style.linesAfterEachRow) { + drawLine(widths); + } } /** @@ -85,12 +177,19 @@ public static void display(List headers, List ...columns) { * * @param widths List of widths of each column */ - private static void drawLine(List widths) { + private void drawLine(List widths) { + drawLine(widths, style.lineLeft, style.lineRight, style.lineMiddle, style.lineSeparator); + } + private void drawHeaderLine(List widths) { + drawLine(widths, style.headerLeft, style.headerRight, style.headerMiddle, style.headerLineSeparator); + } + + private void drawLine(List widths, String left, String right, String middle, String line) { int last = widths.size() - 1; - print("+-"); + print(left); for(int i = 0; i < widths.size(); i++) { - print(StringUtils.repeat("-", widths.get(i))); - print((i == last) ? "-+" : "-+-"); + print(StringUtils.repeat(line, widths.get(i))); + print((i == last) ? right : middle); } println(); } @@ -102,11 +201,10 @@ private static void drawLine(List widths) { * @param column All column values * @return Maximal */ - private static int getMaximalWidth(String header, List column) { - assert header != null; + private int getMaximalWidth(String header, List column) { assert column != null; - int max = header.length(); + int max = header != null ? header.length() : 0; for(String value : column) { if(value != null && value.length() > max) { @@ -123,7 +221,7 @@ private static int getMaximalWidth(String header, List column) { * @param columns Array with all column values * @return */ - private static int getMaximalRows(List... columns) { + private int getMaximalRows(List... columns) { int max = 0; for(List column : columns) { @@ -135,7 +233,12 @@ private static int getMaximalRows(List... columns) { return max; } - private TableDisplayer() { - // Instantiation is prohibited + private void print(String text) { + writer.append(text); } + + private void println() { + writer.newLineAndFlush(); + } + }