View Javadoc

1   package org.apache.maven.announcement;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.StringTokenizer;
24  
25  /**
26   * Format text by wrapping it if it goes beyond some column position.
27   *
28   * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
29   *
30   * @version $Id: Formatter.java 529166 2007-04-16 08:31:26Z ltheussl $
31   */
32  public final class Formatter
33  {
34      /**
35       * Format text by wrapping it if it goes beyond the passed column value.
36       *
37       * @param text the text to format
38       * @param column the max column position
39       * @return the wrapped text
40       */
41      public List format(final String text, final int column)
42      {
43          StringTokenizer st = new StringTokenizer(text, " \t\r\n");
44          List strings = new ArrayList();
45          StringBuffer buffer = new StringBuffer();
46          while (st.hasMoreTokens())
47          {
48              String token = st.nextToken();
49              if (buffer.length() + token.length() <= column)
50              {
51                  buffer.append(token);
52                  buffer.append(' ');
53              }
54              else
55              {
56                  strings.add(buffer.toString());
57                  buffer = new StringBuffer();
58                  buffer.append(token);
59                  buffer.append(' ');
60              }
61          }
62          strings.add(buffer.toString());
63          return strings;
64      }
65  }