Example usage for org.apache.commons.lang StringUtils center

List of usage examples for org.apache.commons.lang StringUtils center

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils center.

Prototype

public static String center(String str, int size) 

Source Link

Document

Centers a String in a larger String of size size using the space character (' ').

Usage

From source file:edu.umd.cfar.lamp.viper.util.StringHelp.java

/**
 * Generates an asterisk-surrounded banner containing the 
 * given text. For example, <code>banner("Hello, world!",12)</code>
 * generates:/*from   w  w w  .j a va2 s . com*/
 * <pre>
 * ***********
 * *  Hello, *
 * *  world! *
 * ***********
 * </pre>
 * @param text
 * @param width
 * @return a banner describing the text, with a newline at the end
 */
public static String banner(String text, int width) {
    StringBuffer s = new StringBuffer();
    int innerWidth = width - 4;
    for (int i = 0; i < width; i++)
        s.append('*');
    s.append('\n');
    StringTokenizer st = new StringTokenizer(WordUtils.wrap(text, innerWidth), "\n\r\f");
    while (st.hasMoreTokens()) {
        s.append("* ");
        s.append(StringUtils.center(st.nextToken(), innerWidth));
        s.append(" *\n");
    }
    for (int i = 0; i < width; i++)
        s.append('*');
    s.append('\n');
    return s.toString();
}

From source file:org.apache.archiva.web.startup.Banner.java

public static String injectVersion(String text, String version) {
    Pattern pat = Pattern.compile("#{2,}");
    Matcher mat = pat.matcher(text);
    StringBuilder ret = new StringBuilder();
    int off = 0;//from   ww  w .ja  v a  2s .c  o m

    while (mat.find(off)) {
        ret.append(text.substring(off, mat.start()));
        String repl = mat.group();
        ret.append(StringUtils.center(version, repl.length()));
        off = mat.end();
    }

    ret.append(text.substring(off));

    return ret.toString();
}

From source file:org.apache.hadoop.hdfs.server.diskbalancer.command.PlanCommand.java

/**
 * Prints a quick summary of the plan to screen.
 *
 * @param plans - List of NodePlans./*  ww  w  .ja  va2  s . c om*/
 */
static private void printToScreen(List<NodePlan> plans) {
    System.out.println("\nPlan :\n");
    System.out.println(StringUtils.repeat("=", 80));

    System.out.println(StringUtils.center("Source Disk", 30) + StringUtils.center("Dest.Disk", 30)
            + StringUtils.center("Size", 10) + StringUtils.center("Type", 10));

    for (NodePlan plan : plans) {
        for (Step step : plan.getVolumeSetPlans()) {
            System.out.println(
                    String.format("%s %s %s %s", StringUtils.center(step.getSourceVolume().getPath(), 30),
                            StringUtils.center(step.getDestinationVolume().getPath(), 30),
                            StringUtils.center(step.getSizeString(step.getBytesToMove()), 10),
                            StringUtils.center(step.getDestinationVolume().getStorageType(), 10)));
        }
    }

    System.out.println(StringUtils.repeat("=", 80));
}

From source file:org.dkpro.lab.reporting.FlexTable.java

public StreamWriter getTextWriter() {
    return new StreamWriter() {
        @Override// w  w  w  .j  av  a  2 s.  co 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;
        }
    };
}

From source file:org.eclipse.gyrex.boot.internal.app.ServerApplication.java

private static void printError(final String message, final Throwable cause) {
    System.err.println();//from  w  ww.  jav  a 2  s  .c  o  m
    System.err.println();
    System.err.println(StringUtils.leftPad("", 72, '*'));
    System.err.println(StringUtils.center(" Server Startup Error ", 72));
    System.err.println(StringUtils.leftPad("", 72, '*'));
    System.err.println();
    System.err.println(WordUtils.wrap(message, 72));
    System.err.println();
    if (cause != null) {
        System.err.println("Reported error:");
        System.err.print("  ");
        System.err.println(WordUtils.wrap(cause.getMessage(), 70, String.format("%n  "), false));
        final List throwables = ExceptionUtils.getThrowableList(cause);
        if (throwables.size() > 1) {
            System.err.println();
            System.err.println("Caused by:");
            for (int i = 1; i < throwables.size(); i++) {
                System.err.print("  ");
                System.err.println(WordUtils.wrap(ExceptionUtils.getMessage((Throwable) throwables.get(i)), 70,
                        String.format("%n  "), false));
            }
        }
        System.err.println();
    }
    System.err.println(StringUtils.leftPad("", 72, '*'));
    System.err.println();
    System.err.println();
}

From source file:org.openspotlight.graph.query.console.command.dynamic.QueryCommand.java

/**
 * Generate output based on result nodes.
 * //from w ww .j a v  a2s .c  o m
 * @param nodes the nodes
 * @param additionalProperties the additional properties
 * @return the string
 */
protected String generateOutput(final Collection<Node> nodes, final Collection<String> additionalProperties) {
    final StringBuilder buffer = new StringBuilder();
    // Header
    StringBuilderUtil.append(buffer,
            StringUtils.repeat("-", ((3 + additionalProperties.size()) * (COLUMN_SIZE + 3)) + 1), "\n");
    StringBuilderUtil.append(buffer, "|", StringUtils.center("type name", COLUMN_SIZE + 2), "|");
    StringBuilderUtil.append(buffer, StringUtils.center("name", COLUMN_SIZE + 2), "|");
    StringBuilderUtil.append(buffer, StringUtils.center("parent name", COLUMN_SIZE + 2), "|");
    for (final String property : additionalProperties) {
        StringBuilderUtil.append(buffer, StringUtils.center(property, COLUMN_SIZE + 2), "|");
    }
    StringBuilderUtil.append(buffer, "\n");
    StringBuilderUtil.append(buffer,
            StringUtils.repeat("-", ((3 + additionalProperties.size()) * (COLUMN_SIZE + 3)) + 1), "\n");
    if (!nodes.isEmpty()) {
        for (final Node node : nodes) {
            final List<String> output = new LinkedList<String>();
            output.add("| ");
            output.add(
                    StringUtils.rightPad(StringUtils.abbreviate(node.getTypeName(), COLUMN_SIZE), COLUMN_SIZE));
            output.add(" | ");
            output.add(StringUtils.rightPad(StringUtils.abbreviate(node.getName(), COLUMN_SIZE), COLUMN_SIZE));
            output.add(" | ");
            output.add(StringUtils.rightPad(
                    StringUtils.abbreviate(StringKeysSupport.getNodeType(node.getParentId()), COLUMN_SIZE),
                    COLUMN_SIZE));
            output.add(" | ");
            for (final String propertyName : additionalProperties) {
                String propertyValue = "";
                try {
                    propertyValue = node.getPropertyValueAsString(propertyName);
                } catch (final Exception e) {
                }
                output.add(
                        StringUtils.rightPad(StringUtils.abbreviate(propertyValue, COLUMN_SIZE), COLUMN_SIZE));
                output.add(" | ");
            }
            StringBuilderUtil.appendLine(buffer, output);
        }
        StringBuilderUtil.append(buffer, "\n", nodes.size(), " nodes affected.", "\n");
    } else {
        StringBuilderUtil.append(buffer, "\n", "0 nodes affected.", "\n");
    }

    return buffer.toString();
}

From source file:ro.fortsoft.pf4j.spring.demo.Boot.java

private static void printLogo() {
    System.out.println(StringUtils.repeat("#", 40));
    System.out.println(StringUtils.center("PF4J-SPRING", 40));
    System.out.println(StringUtils.repeat("#", 40));
}