1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 * <= 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
61
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
99
100 retList.add(text);
101 }
102 return (String[]) retList.toArray(new String[0]);
103 }
104 }
105