List of usage examples for org.apache.commons.lang StringUtils rightPad
public static String rightPad(String str, int size)
Right pad a String with spaces (' ').
From source file:org.apache.tajo.client.TajoAdmin.java
private void processList(Writer writer) throws ParseException, IOException, ServiceException, SQLException { tajoClient = TajoHAClientUtil.getTajoClient(tajoConf, tajoClient); List<BriefQueryInfo> queryList = tajoClient.getRunningQueryList(); SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); StringBuilder builder = new StringBuilder(); /* print title */ builder.append(StringUtils.rightPad("QueryId", 21)); builder.append(StringUtils.rightPad("State", 20)); builder.append(StringUtils.rightPad("StartTime", 20)); builder.append(StringUtils.rightPad("Query", 30)).append("\n"); builder.append(StringUtils.rightPad(StringUtils.repeat("-", 20), 21)); builder.append(StringUtils.rightPad(StringUtils.repeat("-", 19), 20)); builder.append(StringUtils.rightPad(StringUtils.repeat("-", 19), 20)); builder.append(StringUtils.rightPad(StringUtils.repeat("-", 29), 30)).append("\n"); writer.write(builder.toString());/*from www . jav a2s .c o m*/ builder = new StringBuilder(); for (BriefQueryInfo queryInfo : queryList) { builder.append(StringUtils.rightPad(new QueryId(queryInfo.getQueryId()).toString(), 21)); builder.append(StringUtils.rightPad(queryInfo.getState().name(), 20)); builder.append(StringUtils.rightPad(df.format(queryInfo.getStartTime()), 20)); builder.append(StringUtils.abbreviate(queryInfo.getQuery(), 30)).append("\n"); } writer.write(builder.toString()); }
From source file:org.apache.torque.generator.outlet.java.JavadocOutlet.java
/** * Wraps the content string such that the line length is not longer than * maxLineLength characters, if that can be achieved by wrapping at the * wrap characters. All resulting lines are also indented. * * @param content The content to wrap, may be null. * * @return the wrapped and indented content, not null. *///from w w w. j a va2s . com String wrapLinesAndIndent(String content) { if (StringUtils.isBlank(content)) { return ""; } StringTokenizer tokenizer = new StringTokenizer(content.trim(), removableWrapCharacters + wrapAfterCharacters + "\r\n" + JAVADOC_ATTRIBUTE_START, true); StringBuilder result = new StringBuilder(); result.append(indent).append(MID_LINE_START); int currentLineLength = indent.length() + MID_LINE_START.length(); boolean lineBreakPossible = false; // last char is space so it can be removed boolean lastCharRemovable = true; String token = null; String currentIndent = indent + MID_LINE_START; String lastJavadocAttribute = null; while (tokenizer.hasMoreTokens() || token != null) { if (token == null) { // token has not been prefetched token = tokenizer.nextToken(); } if ("\r".equals(token)) { // Assumption: no \r without line breaks // always remove, will be added again if linebreak is \r\n token = null; } else if ("\n".equals(token)) { // line break does not count towards line length result.append(lineBreak); lineBreakPossible = false; // because currentIndent ends with a space // we can remove the last char lastCharRemovable = true; if (tokenizer.hasMoreTokens()) { result.append(currentIndent); currentLineLength = currentIndent.length(); } token = null; } else if (JAVADOC_ATTRIBUTE_START.equals(token)) { if (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); // + 2 because of "@" + space after attribute currentIndent = StringUtils.rightPad(indent + MID_LINE_START, indent.length() + MID_LINE_START.length() + 2 + token.length()); if (result.length() > indent.length() + MID_LINE_START.length()) { // we could already be indented by a line break // in a previous param removeEnd(result, " \r\n"); boolean alreadyIndented = false; if (result.toString().endsWith(indent + " *")) { alreadyIndented = true; } boolean doubleLineBreak = false; if (!token.equals(lastJavadocAttribute)) { doubleLineBreak = true; } if (!alreadyIndented) { result.append(lineBreak).append(indent).append(" *"); } if (doubleLineBreak) { result.append(lineBreak).append(indent).append(" *"); } result.append(" "); } //+ 3 because " * " currentLineLength = indent.length() + MID_LINE_START.length(); lastJavadocAttribute = token; } result.append(JAVADOC_ATTRIBUTE_START); ++currentLineLength; lineBreakPossible = false; lastCharRemovable = false; } else if (maxLineLength == -1) { result.append(token); token = null; } else if (removableWrapCharacters.indexOf(token) != -1) { if (currentLineLength + 1 > maxLineLength) { result.append(lineBreak); if (tokenizer.hasMoreTokens()) { result.append(currentIndent); currentLineLength = currentIndent.length(); } lineBreakPossible = false; lastCharRemovable = false; } else { result.append(token); currentLineLength++; lineBreakPossible = true; lastCharRemovable = true; } token = null; } else if (lineBreakPossible) { // we must check next token String nextToken = null; if (tokenizer.hasMoreTokens()) { nextToken = tokenizer.nextToken(); } int unbreakableChunkSize; if (nextToken == null || "\r".equals(nextToken) || "\n".equals(nextToken) || wrapAfterCharacters.contains(token) || JAVADOC_ATTRIBUTE_START.equals(nextToken) || removableWrapCharacters.contains(nextToken)) { unbreakableChunkSize = token.length(); } else { unbreakableChunkSize = token.length() + nextToken.length(); } if (currentLineLength + unbreakableChunkSize > maxLineLength) { // break now if (lastCharRemovable) { result.replace(result.length() - 1, result.length(), ""); } result.append(lineBreak).append(currentIndent).append(token); currentLineLength = currentIndent.length() + token.length(); } else { // no break necessary result.append(token); currentLineLength += token.length(); } lastCharRemovable = false; lineBreakPossible = wrapAfterCharacters.contains(token); token = nextToken; } else { result.append(token); currentLineLength += token.length(); lastCharRemovable = false; lineBreakPossible = wrapAfterCharacters.contains(token); token = null; } } if (!result.toString().endsWith(lineBreak)) { result.append(lineBreak); } return result.toString(); }
From source file:org.batoo.common.impl.log.BLoggerImpl.java
/** * {@inheritDoc}// w w w . java 2 s . co m * */ @Override public String boxed(String block, Object[] parameters) { try { if ((parameters != null) && (parameters.length > 0)) { block += "\n\n" + Arrays.toString(parameters); } block = block.replaceAll("\\t", " "); final List<String> lines = IOUtils.readLines(new StringReader(block)); int max = 0; for (final String line : lines) { max = Math.max(max, line.length()); } max += 4; final StringBuffer boxed = new StringBuffer("\n"); boxed.append(StringUtils.repeat("-", max)); boxed.append("\n"); for (final String line : lines) { boxed.append("| "); boxed.append(StringUtils.rightPad(line, max - 4)); boxed.append(" |\n"); } boxed.append(StringUtils.repeat("-", max)); return boxed.toString(); } catch (final Exception e) { return block; } }
From source file:org.batoo.jpa.common.impl.log.BLoggerImpl.java
/** * {@inheritDoc}//w ww . ja va2 s .c o m * */ @Override public String boxed(String block, Object[] parameters) { try { if ((parameters != null) && (parameters.length > 0)) { block += "\n\n" + Arrays.toString(parameters); } block = block.replaceAll("\\t", " "); final List<String> lines = IOUtils.readLines(new StringReader(block)); int max = 0; for (final String line : lines) { max = Math.max(max, line.length()); } max += 4; final StringBuffer boxed = new StringBuffer("\n"); boxed.append(StringUtils.repeat("-", max)); boxed.append("\n"); for (final String line : lines) { boxed.append("| "); boxed.append(StringUtils.rightPad(line, max - 4)); boxed.append(" |\n"); } boxed.append(StringUtils.repeat("-", max)); return boxed.toString(); } catch (final Throwable e) { return block; } }
From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java
private void dumpResultSet() throws SQLException { final int[] lengths = new int[this.labels.length]; for (int i = 0; i < lengths.length; i++) { lengths[i] = this.max(lengths[i], StringUtils.length(this.labels[i])); }//from w w w . j av a2 s .c om for (final Object[] data : this.data) { for (int i = 0; i < this.labels.length; i++) { final Object value = data[i]; if (value != null) { lengths[i] = this.max(lengths[i], StringUtils.length(value.toString())); } } } int length = 1; for (final int l : lengths) { length += l + 3; } final StringBuffer dump = new StringBuffer("Query returned {0} row(s):\n"); // the labels dump.append(StringUtils.repeat("-", length)); dump.append("\n| "); for (int i = 0; i < this.labels.length; i++) { String strValue = StringUtils.abbreviate(this.labels[i], lengths[i]); strValue = StringUtils.rightPad(strValue, lengths[i]); dump.append(strValue); dump.append(" | "); } // the data dump.append("\n"); dump.append(StringUtils.repeat("-", length)); for (final Object[] data : this.data) { dump.append("\n| "); for (int i = 0; i < this.labels.length; i++) { final Object value = data[i]; String strValue = value != null ? value.toString() : "!NULL!"; strValue = StringUtils.abbreviate(strValue, lengths[i]); if (value instanceof Number) { strValue = StringUtils.leftPad(strValue, lengths[i]); } else { strValue = StringUtils.rightPad(strValue, lengths[i]); } dump.append(strValue); dump.append(" | "); } } dump.append("\n"); dump.append(StringUtils.repeat("-", length)); QueryImpl.LOG.debug(dump.toString(), this.data.size()); }
From source file:org.beangle.model.persist.hibernate.support.DefaultTableNameConfig.java
public String toString() { int maxlength = 0; for (TableNamePattern pattern : patterns) { if (pattern.getPackageName().length() > maxlength) { maxlength = pattern.getPackageName().length(); }//from www .j av a 2 s . c o m } StringBuilder sb = new StringBuilder(); for (int i = 0; i < patterns.size(); i++) { TableNamePattern pattern = patterns.get(i); sb.append(StringUtils.rightPad(pattern.getPackageName(), maxlength)).append(" : [") .append(pattern.getSchema()); sb.append(" , ").append(pattern.getPrefix()).append(']'); if (i < patterns.size() - 1) sb.append('\n'); } return sb.toString(); }
From source file:org.codice.ddf.admin.application.service.command.ListApplicationCommand.java
@Override protected void doExecute(ApplicationService applicationService) throws ApplicationServiceException { Set<Application> applications = applicationService.getApplications(); console.printf("%s%10s%n", "State", "Name"); for (Application curApp : applications) { ApplicationStatus appStatus = applicationService.getApplicationStatus(curApp); // only show applications that have features (gets rid of repo // aggregator 'apps') if (!curApp.getFeatures().isEmpty()) { console.print("["); switch (appStatus.getState()) { case ACTIVE: console.print(Ansi.ansi().fg(Ansi.Color.GREEN).toString()); break; case FAILED: console.print(Ansi.ansi().fg(Ansi.Color.RED).toString()); break; case INACTIVE: // don't set a color break; case UNKNOWN: console.print(Ansi.ansi().fg(Ansi.Color.YELLOW).toString()); break; default: break; }/*from w ww .j av a 2 s . c om*/ console.print(StringUtils.rightPad(appStatus.getState().toString(), STATUS_COLUMN_LENGTH)); console.print(Ansi.ansi().reset().toString()); console.println("] " + curApp.getName()); } } return; }
From source file:org.codice.ddf.admin.application.service.impl.ListApplicationCommand.java
@Override protected void applicationCommand() throws ApplicationServiceException { Set<Application> applications = applicationService.getApplications(); console.printf("%s%10s%n", "State", "Name"); for (Application curApp : applications) { ApplicationStatus appStatus = applicationService.getApplicationStatus(curApp); // only show applications that have features (gets rid of repo // aggregator 'apps') if (!curApp.getFeatures().isEmpty()) { console.print("["); switch (appStatus.getState()) { case ACTIVE: console.print(Ansi.ansi().fg(Ansi.Color.GREEN).toString()); break; case FAILED: console.print(Ansi.ansi().fg(Ansi.Color.RED).toString()); break; case INACTIVE: // don't set a color break; case UNKNOWN: console.print(Ansi.ansi().fg(Ansi.Color.YELLOW).toString()); break; default: break; }/* ww w. j a va 2s .c om*/ console.print(StringUtils.rightPad(appStatus.getState().toString(), STATUS_COLUMN_LENGTH)); console.print(Ansi.ansi().reset().toString()); console.println("] " + curApp.getName()); } } return; }
From source file:org.codice.ddf.platform.status.impl.ListApplicationCommand.java
@Override protected Object doExecute() throws Exception { PrintStream console = System.out; ServiceReference ref = getBundleContext().getServiceReference(ApplicationService.class.getName()); if (ref == null) { console.println("Application Status service is unavailable."); return null; }// w w w .j a v a 2s . c o m try { ApplicationService appService = (ApplicationService) getBundleContext().getService(ref); if (appService == null) { console.println("Application Status service is unavailable."); return null; } Set<Application> applications = appService.getApplications(); console.printf("%s%10s\n", "State", "Name"); for (Application curApp : applications) { ApplicationStatus appStatus = appService.getApplicationStatus(curApp); // only show applications that have features (gets rid of repo // aggregator 'apps') if (!curApp.getFeatures().isEmpty()) { console.print("["); switch (appStatus.getState()) { case ACTIVE: console.print(Ansi.ansi().fg(Ansi.Color.GREEN).toString()); break; case FAILED: console.print(Ansi.ansi().fg(Ansi.Color.RED).toString()); break; case INACTIVE: // don't set a color break; case UNKNOWN: console.print(Ansi.ansi().fg(Ansi.Color.YELLOW).toString()); break; default: break; } console.print(StringUtils.rightPad(appStatus.getState().toString(), STATUS_COLUMN_LENGTH)); console.print(Ansi.ansi().reset().toString()); console.println("] " + curApp.getName()); } } } finally { getBundleContext().ungetService(ref); } return null; }
From source file:org.dkpro.lab.reporting.FlexTable.java
public StreamWriter getTextWriter() { return new StreamWriter() { @Override//from www .j a v a 2s . c o m public void write(OutputStream aStream) throws Exception { String[] colIds = FlexTable.this.compact ? getCompactColumnIds(false) : getColumnIds(); // Obtain the width of the columns based on their content and headers // col 0 is reserved here for the rowId width int colWidths[] = new int[colIds.length + 1]; for (String rowId : getRowIds()) { colWidths[0] = Math.max(colWidths[0], rowId.length()); } for (int i = 1; i < colWidths.length; i++) { colWidths[i] = Math.max(colWidths[i], colIds[i - 1].length()); for (String rowId : getRowIds()) { colWidths[i] = Math.max(colWidths[i], getValueAsString(rowId, colIds[i - 1]).length()); } } StringBuilder separator = new StringBuilder(); for (int w : colWidths) { if (separator.length() > 0) { separator.append("-+-"); } separator.append(StringUtils.repeat("-", w)); } separator.insert(0, "+-"); separator.append("-+"); PrintWriter writer = new PrintWriter(new OutputStreamWriter(aStream, "UTF-8")); // Render header column writer.println(separator); writer.print("| "); writer.print(StringUtils.repeat(" ", colWidths[0])); for (int i = 0; i < colIds.length; i++) { writer.print(" | "); // Remember: colWidth[0] is the rowId width! writer.print(StringUtils.center(colIds[i], colWidths[i + 1])); } writer.println(" |"); writer.println(separator); // Render body for (String rowId : getRowIds()) { writer.print("| "); writer.print(StringUtils.rightPad(rowId, colWidths[0])); for (int i = 0; i < colIds.length; i++) { writer.print(" | "); // Remember: colWidth[0] is the rowId width! String val = getValueAsString(rowId, colIds[i]); if (isDouble(val)) { writer.print(StringUtils.leftPad(val, colWidths[i + 1])); } else { writer.print(StringUtils.rightPad(val, colWidths[i + 1])); } } writer.println(" |"); } // Closing separator writer.println(separator); writer.flush(); } private boolean isDouble(String val) { try { double d = Double.parseDouble(val); // TODO: A bit of a hack; use Regex instead? } catch (NumberFormatException ex) { return false; } return true; } }; }