View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /**
25   * Helper class to format multiline messages to the console
26   */
27  public class MultilineMessageHelper {
28  
29      private static final int DEFAULT_MAX_SIZE = 65;
30      private static final char BOX_CHAR = '*';
31  
32      public static String separatorLine() {
33          StringBuilder sb = new StringBuilder(DEFAULT_MAX_SIZE);
34          repeat(sb, '*', DEFAULT_MAX_SIZE);
35          return sb.toString();
36      }
37  
38      public static List<String> format(String... lines) {
39          int size = DEFAULT_MAX_SIZE;
40          int remainder = size - 4; // 4 chars = 2 box_char + 2 spaces
41          List<String> result = new ArrayList<>();
42          StringBuilder sb = new StringBuilder(size);
43          // first line
44          sb.setLength(0);
45          repeat(sb, BOX_CHAR, size);
46          result.add(sb.toString());
47          // lines
48          for (String line : lines) {
49              sb.setLength(0);
50              String[] words = line.split("\\s+");
51              for (String word : words) {
52                  if (sb.length() >= remainder - word.length() - (sb.length() > 0 ? 1 : 0)) {
53                      repeat(sb, ' ', remainder - sb.length());
54                      result.add(BOX_CHAR + " " + sb + " " + BOX_CHAR);
55                      sb.setLength(0);
56                  }
57                  if (sb.length() > 0) {
58                      sb.append(' ');
59                  }
60                  sb.append(word);
61              }
62  
63              while (sb.length() < remainder) {
64                  sb.append(' ');
65              }
66              result.add(BOX_CHAR + " " + sb + " " + BOX_CHAR);
67          }
68          // last line
69          sb.setLength(0);
70          repeat(sb, BOX_CHAR, size);
71          result.add(sb.toString());
72          return result;
73      }
74  
75      private static void repeat(StringBuilder sb, char c, int nb) {
76          for (int i = 0; i < nb; i++) {
77              sb.append(c);
78          }
79      }
80  }