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  import java.util.regex.Pattern;
24  
25  /**
26   * Helper class to format multiline messages to the console
27   */
28  public class MultilineMessageHelper {
29  
30      private static final int DEFAULT_MAX_SIZE = 65;
31      private static final char BOX_CHAR = '*';
32  
33      private static final Pattern S_FILTER = Pattern.compile("\\s+");
34  
35      public static String separatorLine() {
36          StringBuilder sb = new StringBuilder(DEFAULT_MAX_SIZE);
37          repeat(sb, '*', DEFAULT_MAX_SIZE);
38          return sb.toString();
39      }
40  
41      public static List<String> format(String... lines) {
42          int size = DEFAULT_MAX_SIZE;
43          int remainder = size - 4; // 4 chars = 2 box_char + 2 spaces
44          List<String> result = new ArrayList<>();
45          StringBuilder sb = new StringBuilder(size);
46          // first line
47          sb.setLength(0);
48          repeat(sb, BOX_CHAR, size);
49          result.add(sb.toString());
50          // lines
51          for (String line : lines) {
52              sb.setLength(0);
53              String[] words = S_FILTER.split(line);
54              for (String word : words) {
55                  if (sb.length() >= remainder - word.length() - (sb.length() > 0 ? 1 : 0)) {
56                      repeat(sb, ' ', remainder - sb.length());
57                      result.add(BOX_CHAR + " " + sb + " " + BOX_CHAR);
58                      sb.setLength(0);
59                  }
60                  if (sb.length() > 0) {
61                      sb.append(' ');
62                  }
63                  sb.append(word);
64              }
65  
66              while (sb.length() < remainder) {
67                  sb.append(' ');
68              }
69              result.add(BOX_CHAR + " " + sb + " " + BOX_CHAR);
70          }
71          // last line
72          sb.setLength(0);
73          repeat(sb, BOX_CHAR, size);
74          result.add(sb.toString());
75          return result;
76      }
77  
78      private static void repeat(StringBuilder sb, char c, int nb) {
79          for (int i = 0; i < nb; i++) {
80              sb.append(c);
81          }
82      }
83  }