You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sqoop.apache.org by af...@apache.org on 2016/03/07 20:07:21 UTC
sqoop git commit: SQOOP-2853: Sqoop2: Refactor TableDisplayer to be
used in document generation
Repository: sqoop
Updated Branches:
refs/heads/sqoop2 b024e8cae -> dc39a5aaf
SQOOP-2853: Sqoop2: Refactor TableDisplayer to be used in document generation
(Jarek Jarcec Cecho via Abraham Fine)
Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/dc39a5aa
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/dc39a5aa
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/dc39a5aa
Branch: refs/heads/sqoop2
Commit: dc39a5aafe72b00c912f9cf04655fdfe78582f02
Parents: b024e8c
Author: Abraham Fine <af...@apache.org>
Authored: Mon Mar 7 11:05:08 2016 -0800
Committer: Abraham Fine <af...@apache.org>
Committed: Mon Mar 7 11:05:08 2016 -0800
----------------------------------------------------------------------
.../apache/sqoop/shell/ShellEnvironment.java | 18 +++
.../sqoop/shell/ShowConnectorFunction.java | 3 +-
.../org/apache/sqoop/shell/ShowJobFunction.java | 6 +-
.../apache/sqoop/shell/ShowLinkFunction.java | 8 +-
.../sqoop/shell/ShowPrincipalFunction.java | 4 +-
.../sqoop/shell/ShowPrivilegeFunction.java | 4 +-
.../apache/sqoop/shell/ShowRoleFunction.java | 4 +-
.../sqoop/shell/ShowSubmissionFunction.java | 3 +-
.../sqoop/shell/utils/TableDisplayer.java | 161 +++++++++++++++----
9 files changed, 160 insertions(+), 51 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java
----------------------------------------------------------------------
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 36d0712..80ac935 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.client.SqoopClient;
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 @@ public final class 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 final class ShellEnvironment {
io.out.printf(format, args);
}
+ public static void displayTable(List<String> headers, List<String> ...columns) {
+ tableDisplayer.display(headers, columns);
+ }
+
// for tests only
public static void cleanup() {
serverHost = DEFAULT_SERVER_HOST;
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java
----------------------------------------------------------------------
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 d973499..c90fe94 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.client.ClientError;
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 @@ public class ShowConnectorFunction extends SqoopFunction {
supportedDirections.add(connector.getSupportedDirections().toString());
}
- TableDisplayer.display(header, uniqueNames, versions, classes, supportedDirections);
+ displayTable(header, uniqueNames, versions, classes, supportedDirections);
}
private void showConnectors() {
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java
----------------------------------------------------------------------
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 ebbfe82..139e756 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 @@ package org.apache.sqoop.shell;
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 @@ public class ShowJobFunction extends SqoopFunction {
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) {
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShowLinkFunction.java
----------------------------------------------------------------------
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 25bd1db..4976c98 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 @@ package org.apache.sqoop.shell;
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 @@ public class ShowLinkFunction extends SqoopFunction {
availabilities.add(String.valueOf(link.getEnabled()));
}
- TableDisplayer.display(header, names, connectorNames, availabilities);
+ displayTable(header, names, connectorNames, availabilities);
}
private void showLinks() {
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java
----------------------------------------------------------------------
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 a450aaf..45abf14 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.commons.cli.OptionBuilder;
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 @@ public class ShowPrincipalFunction extends SqoopFunction {
types.add(principal.getType());
}
- TableDisplayer.display(header, names, types);
+ displayTable(header, names, types);
}
}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java
----------------------------------------------------------------------
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 2cf6972..efd4b19 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.MPrivilege;
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 @@ public class ShowPrivilegeFunction extends SqoopFunction {
withGrant.add(Boolean.toString(privilege.isWith_grant_option()));
}
- TableDisplayer.display(header, actions, resourceNames, resourceTypes, withGrant);
+ displayTable(header, actions, resourceNames, resourceTypes, withGrant);
}
}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java
----------------------------------------------------------------------
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 6b61921..91ceff3 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.MPrincipal;
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 @@ public class ShowRoleFunction extends SqoopFunction {
names.add(role.getName());
}
- TableDisplayer.display(header, names);
+ displayTable(header, names);
}
}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java
----------------------------------------------------------------------
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 8989913..c7f371f 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.commons.cli.OptionBuilder;
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 @@ public class ShowSubmissionFunction extends SqoopFunction {
dates.add(submission.getLastUpdateDate().toString());
}
- TableDisplayer.display(header, jnames, eids, status, dates);
+ displayTable(header, jnames, eids, status, dates);
}
private void showSubmissions(String jArg) {
http://git-wip-us.apache.org/repos/asf/sqoop/blob/dc39a5aa/shell/src/main/java/org/apache/sqoop/shell/utils/TableDisplayer.java
----------------------------------------------------------------------
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 51030d0..8b4f1f6 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 org.apache.commons.lang.StringUtils;
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<String> headers, List<String> ...columns) {
- assert headers != null;
+ public void display(List<String> headers, List<String> ...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<Integer> widths = new LinkedList<Integer>();
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 class TableDisplayer {
*
* @param widths List of widths of each column
*/
- private static void drawLine(List<Integer> widths) {
+ private void drawLine(List<Integer> widths) {
+ drawLine(widths, style.lineLeft, style.lineRight, style.lineMiddle, style.lineSeparator);
+ }
+ private void drawHeaderLine(List<Integer> widths) {
+ drawLine(widths, style.headerLeft, style.headerRight, style.headerMiddle, style.headerLineSeparator);
+ }
+
+ private void drawLine(List<Integer> 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 @@ public class TableDisplayer {
* @param column All column values
* @return Maximal
*/
- private static int getMaximalWidth(String header, List<String> column) {
- assert header != null;
+ private int getMaximalWidth(String header, List<String> 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 @@ public class TableDisplayer {
* @param columns Array with all column values
* @return
*/
- private static int getMaximalRows(List<String>... columns) {
+ private int getMaximalRows(List<String>... columns) {
int max = 0;
for(List<String> column : columns) {
@@ -135,7 +233,12 @@ public class TableDisplayer {
return max;
}
- private TableDisplayer() {
- // Instantiation is prohibited
+ private void print(String text) {
+ writer.append(text);
}
+
+ private void println() {
+ writer.newLineAndFlush();
+ }
+
}