View Javadoc
1   /*
2    * Copyright 2004 Diogo Quintela (dquintela@users.sourceforge.net)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.whatsnew.filter.util;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.StringTokenizer;
21  
22  import net.sf.whatsnew.exceptions.AppException;
23  
24  
25  /***
26   * <p>
27   * Operations on Strings
28   * </p>
29   *
30   * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
31   * @version $Id: StringUtils.java,v 1.1 2004/05/13 01:22:37 dquintela Exp $
32   */
33  public class StringUtils {
34      /***
35       * Expand tabs contained in string argument <code>line</code>.
36       *
37       * @param line The string to have tabs removed
38       * @param tabSize The tab size to use in the transformation
39       *
40       * @return The input string with tabs removed (<code>null</code> if null
41       *         string input)
42       *
43       * @throws AppException if an invalid tab size is especied (<code>tabSize
44       *         &lt;= 0</code>)
45       */
46      public static String expandTabs(
47          String line,
48          int tabSize)
49      throws AppException {
50          if (line == null) {
51              return null;
52          }
53          if (tabSize <= 0) {
54              throw new AppException("error.invalid.tabs", true);
55          }
56  
57          StringBuffer retBuf = new StringBuffer();
58          int scanPos = 0;
59          while (scanPos < line.length()) {
60              // scanPos = next position to scan for tab.
61              // tabPos = position containing tab currently being expanded.
62              int tabPos = line.indexOf('\t', scanPos);
63              if (tabPos < 0) {
64                  retBuf.append(line.substring(scanPos));
65                  scanPos = line.length();
66              } else {
67                  retBuf.append(line.substring(scanPos, tabPos));
68  
69                  int numSp = tabSize - (retBuf.length() % tabSize);
70                  for (int i = 0; i < numSp; i++) {
71                      retBuf.append(' ');
72                  }
73                  scanPos = tabPos + 1;
74              }
75          }
76          return retBuf.toString();
77      }
78  
79      /***
80       * Splits a string into lines using a newline as delim
81       *
82       * @param text The text to split
83       * @param delim The delim to use
84       *
85       * @return The array of splited lines
86       */
87      public static String[] splitIntoLines(
88          String text,
89          String delim) {
90          List retList = new ArrayList();
91  
92          if ((text != null) && (delim != null) && !"".equals(text)) {
93              StringTokenizer strTok = new StringTokenizer(text, delim);
94              while (strTok.hasMoreTokens()) {
95                  retList.add(strTok.nextToken());
96              }
97          } else if ("".equals(text)) {
98              // When given a empty string, does not give one token
99              // Lets force it :)
100             retList.add(text);
101         }
102         return (String[]) retList.toArray(new String[0]);
103     }
104 }
105 // EOF