Java String Indent Format indentMultilineValue(String value, StringBuilder tableInfo, int[] columnWidths, boolean printNull)

Here you can find the source of indentMultilineValue(String value, StringBuilder tableInfo, int[] columnWidths, boolean printNull)

Description

comment indent processing for multi-line values values should be indented the same amount on each line if the first line comment starts indented by k, the following line comments should also be indented by k

License

Apache License

Parameter

Parameter Description
value the value to write
tableInfo the buffer to write to
columnWidths the widths of the previous columns
printNull print null as a string, or do not print anything

Declaration

private static void indentMultilineValue(String value, StringBuilder tableInfo, int[] columnWidths,
        boolean printNull) 

Method Source Code

//package com.java2s;
/**//from w w w.jav  a  2  s .c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static final String FIELD_DELIM = "\t";
    public static final String LINE_DELIM = "\n";
    private static final int ALIGNMENT = 20;

    /**
     * comment indent processing for multi-line values
     * values should be indented the same amount on each line
     * if the first line comment starts indented by k,
     * the following line comments should also be indented by k
     * @param value the value to write
     * @param tableInfo the buffer to write to
     * @param columnWidths the widths of the previous columns
     * @param printNull print null as a string, or do not print anything
     */
    private static void indentMultilineValue(String value, StringBuilder tableInfo, int[] columnWidths,
            boolean printNull) {
        if (value == null) {
            if (printNull) {
                tableInfo.append(String.format("%-" + ALIGNMENT + "s", value));
            }
            tableInfo.append(LINE_DELIM);
        } else {
            String[] valueSegments = value.split("\n|\r|\r\n");
            tableInfo.append(String.format("%-" + ALIGNMENT + "s", valueSegments[0])).append(LINE_DELIM);
            for (int i = 1; i < valueSegments.length; i++) {
                printPadding(tableInfo, columnWidths);
                tableInfo.append(String.format("%-" + ALIGNMENT + "s", valueSegments[i])).append(LINE_DELIM);
            }
        }
    }

    /**
     * Print the rigth padding, with the given column widths
     * @param tableInfo The buffer to write to
     * @param columnWidths The column widths
     */
    private static void printPadding(StringBuilder tableInfo, int[] columnWidths) {
        for (int columnWidth : columnWidths) {
            if (columnWidth == 0) {
                tableInfo.append(FIELD_DELIM);
            } else {
                tableInfo.append(String.format("%" + columnWidth + "s" + FIELD_DELIM, ""));
            }
        }
    }
}

Related

  1. indentCode(String code, int indentLevel)
  2. indented(String indenter, String s)
  3. indentHtmlSpaces(int count)
  4. indention(int level, int tabSize)
  5. indentLinesAfterNth(final int skip, final int indent, final String str)
  6. indentNextLines(String text, String indent)
  7. indentParagraph(String textBefore, String textToModify)
  8. indentRows(int level, String ss, String indentStr)
  9. indentStr(int level)