%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.util.StringUtils |
|
|
1 | package org.apache.turbine.util; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * 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, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.NoSuchElementException; |
|
20 | import java.util.StringTokenizer; |
|
21 | ||
22 | import org.apache.commons.lang.exception.ExceptionUtils; |
|
23 | ||
24 | /** |
|
25 | * This is where common String manipulation routines should go. |
|
26 | * |
|
27 | * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> |
|
28 | * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> |
|
29 | * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> |
|
30 | * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a> |
|
31 | * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
|
32 | * @version $Id: StringUtils.java 264148 2005-08-29 14:21:04Z henning $ |
|
33 | * @deprecated This class will be removed after the 2.3 release. Please |
|
34 | * use the <a href="http://jakarta.apache.org/commons/">commons-lang</a> component. |
|
35 | */ |
|
36 | 0 | public class StringUtils |
37 | { |
|
38 | /** |
|
39 | * Deal with null strings converting them to "" instead. It also |
|
40 | * invokes String.trim() on the output. |
|
41 | * |
|
42 | * @param foo A String. |
|
43 | * @return A String. |
|
44 | * @deprecated Use org.apache.commons.lang.StringUtils.defaultString() |
|
45 | */ |
|
46 | public static final String makeString(String foo) |
|
47 | { |
|
48 | 0 | return org.apache.commons.lang.StringUtils.defaultString(foo); |
49 | } |
|
50 | ||
51 | /** |
|
52 | * Validates that the supplied string is neither <code>null</code> |
|
53 | * nor the empty string. |
|
54 | * |
|
55 | * @param foo The text to check. |
|
56 | * @return Whether valid. |
|
57 | * @deprecated Use org.apache.commons.lang.StringUtils.isNotEmpty() |
|
58 | */ |
|
59 | public static final boolean isValid(String foo) |
|
60 | { |
|
61 | 0 | return org.apache.commons.lang.StringUtils.isNotEmpty(foo); |
62 | } |
|
63 | ||
64 | /** |
|
65 | * Determine whether a (trimmed) string is empty |
|
66 | * |
|
67 | * @param foo The text to check. |
|
68 | * @return Whether empty. |
|
69 | * @deprecated use org.apache.commons.lang.StringUtils.isEmpty() instead |
|
70 | */ |
|
71 | public static final boolean isEmpty(String foo) |
|
72 | { |
|
73 | 0 | return org.apache.commons.lang.StringUtils.isEmpty(foo); |
74 | } |
|
75 | ||
76 | /** |
|
77 | * Returns the output of printStackTrace as a String. |
|
78 | * |
|
79 | * @param e A Throwable. |
|
80 | * @return A String. |
|
81 | * @deprecated use org.apache.commons.lang.ExceptionUtils.getStackTrace() instead |
|
82 | */ |
|
83 | public static final String stackTrace(Throwable e) |
|
84 | { |
|
85 | 0 | return ExceptionUtils.getStackTrace(e); |
86 | } |
|
87 | ||
88 | /** |
|
89 | * Returns the output of printStackTrace as a String. |
|
90 | * |
|
91 | * @param e A Throwable. |
|
92 | * @param addPre a boolean to add HTML <pre> tags around the stacktrace |
|
93 | * @return A String. |
|
94 | */ |
|
95 | public static final String stackTrace(Throwable e, boolean addPre) |
|
96 | { |
|
97 | 0 | if (addPre) |
98 | { |
|
99 | 0 | return "<pre>" + ExceptionUtils.getStackTrace(e) + "</pre>"; |
100 | } |
|
101 | else |
|
102 | { |
|
103 | 0 | return ExceptionUtils.getStackTrace(e); |
104 | } |
|
105 | } |
|
106 | ||
107 | /** |
|
108 | * Compares two Strings, returns true if their values are the |
|
109 | * same. |
|
110 | * |
|
111 | * @param s1 The first string. |
|
112 | * @param s2 The second string. |
|
113 | * @return True if the values of both strings are the same. |
|
114 | * @deprecated use org.apache.commons.lang.StringUtils.equals() instead |
|
115 | */ |
|
116 | public static boolean equals(String s1, String s2) |
|
117 | { |
|
118 | 0 | return org.apache.commons.lang.StringUtils.equals(s1, s2); |
119 | } |
|
120 | ||
121 | public static final int PPKEY_CLASSNAME = 0; |
|
122 | public static final int PPKEY_ID = 1; |
|
123 | public static final int PPKEY_PROPERTY = 2; |
|
124 | ||
125 | /** |
|
126 | * Takes a String of the form substring[substring]subtring and |
|
127 | * returns the 3 substrings |
|
128 | * |
|
129 | * @return a three element String array |
|
130 | */ |
|
131 | public static String[] parseObjectKey(String s) |
|
132 | { |
|
133 | 0 | String[] p = new String[3]; |
134 | 0 | StringTokenizer st = new StringTokenizer(s, "[]"); |
135 | 0 | int count = st.countTokens(); |
136 | 0 | if (count > 1) |
137 | { |
|
138 | 0 | p[0] = st.nextToken(); |
139 | 0 | p[1] = st.nextToken(); |
140 | 0 | if (count == 3) |
141 | { |
|
142 | 0 | p[2] = st.nextToken(); |
143 | } |
|
144 | } |
|
145 | 0 | return p; |
146 | } |
|
147 | ||
148 | /** |
|
149 | * Remove Underscores from a string and replaces first |
|
150 | * Letters with Capitals. foo_bar becomes FooBar |
|
151 | */ |
|
152 | public static String removeUnderScores(String data) |
|
153 | { |
|
154 | ||
155 | 0 | String temp = null; |
156 | 0 | StringBuffer out = new StringBuffer(); |
157 | 0 | temp = data; |
158 | ||
159 | 0 | StringTokenizer st = new StringTokenizer(temp, "_"); |
160 | 0 | while (st.hasMoreTokens()) |
161 | { |
|
162 | 0 | String element = (String) st.nextElement(); |
163 | 0 | out.append(org.apache.commons.lang.StringUtils.capitalise(element)); |
164 | } |
|
165 | 0 | return out.toString(); |
166 | } |
|
167 | ||
168 | /** |
|
169 | * Makes the first letter caps and leaves the rest as is. |
|
170 | * |
|
171 | * @deprecated use org.apache.commons.lang.StringUtils.capitalise() instead |
|
172 | */ |
|
173 | public static String firstLetterCaps(String data) |
|
174 | { |
|
175 | 0 | return org.apache.commons.lang.StringUtils.capitalise(data); |
176 | } |
|
177 | ||
178 | /** |
|
179 | * Splits the provided CSV text into a list. |
|
180 | * |
|
181 | * @param text The CSV list of values to split apart. |
|
182 | * @param separator The separator character. |
|
183 | * @return The list of values. |
|
184 | * @deprecated use org.apache.commons.lang.StringUtils.split() instead |
|
185 | */ |
|
186 | public static String[] split(String text, String separator) |
|
187 | { |
|
188 | 0 | return org.apache.commons.lang.StringUtils.split(text, separator); |
189 | } |
|
190 | ||
191 | /** |
|
192 | * Joins the elements of the provided array into a single string |
|
193 | * containing a list of CSV elements. |
|
194 | * |
|
195 | * @param list The list of values to join together. |
|
196 | * @param separator The separator character. |
|
197 | * @return The CSV text. |
|
198 | * @deprecated Use org.apache.commons.lang.StringUtils.join() |
|
199 | */ |
|
200 | public static String join(String[] list, String separator) |
|
201 | { |
|
202 | 0 | return org.apache.commons.lang.StringUtils.join(list, separator); |
203 | } |
|
204 | ||
205 | /** |
|
206 | * Takes a block of text which might have long lines in it and wraps |
|
207 | * the long lines based on the supplied wrapColumn parameter. It was |
|
208 | * initially implemented for use by VelocityEmail. If there are tabs |
|
209 | * in inString, you are going to get results that are a bit strange, |
|
210 | * since tabs are a single character but are displayed as 4 or 8 |
|
211 | * spaces. Remove the tabs. |
|
212 | * |
|
213 | * @param inString Text which is in need of word-wrapping. |
|
214 | * @param newline The characters that define a newline. |
|
215 | * @param wrapColumn The column to wrap the words at. |
|
216 | * @return The text with all the long lines word-wrapped. |
|
217 | */ |
|
218 | ||
219 | public static String wrapText(String inString, String newline, |
|
220 | int wrapColumn) |
|
221 | { |
|
222 | 0 | StringTokenizer lineTokenizer = new StringTokenizer( |
223 | inString, newline, true); |
|
224 | 0 | StringBuffer stringBuffer = new StringBuffer(); |
225 | ||
226 | 0 | while (lineTokenizer.hasMoreTokens()) |
227 | { |
|
228 | try |
|
229 | { |
|
230 | 0 | String nextLine = lineTokenizer.nextToken(); |
231 | ||
232 | 0 | if (nextLine.length() > wrapColumn) |
233 | { |
|
234 | // This line is long enough to be wrapped. |
|
235 | 0 | nextLine = wrapLine(nextLine, newline, wrapColumn); |
236 | } |
|
237 | ||
238 | 0 | stringBuffer.append(nextLine); |
239 | } |
|
240 | 0 | catch (NoSuchElementException nsee) |
241 | { |
|
242 | // thrown by nextToken(), but I don't know why it would |
|
243 | 0 | break; |
244 | 0 | } |
245 | } |
|
246 | ||
247 | 0 | return (stringBuffer.toString()); |
248 | } |
|
249 | ||
250 | /** |
|
251 | * Wraps a single line of text. Called by wrapText(). I can't |
|
252 | * think of any good reason for exposing this to the public, |
|
253 | * since wrapText should always be used AFAIK. |
|
254 | * |
|
255 | * @param line A line which is in need of word-wrapping. |
|
256 | * @param newline The characters that define a newline. |
|
257 | * @param wrapColumn The column to wrap the words at. |
|
258 | * @return A line with newlines inserted. |
|
259 | */ |
|
260 | ||
261 | protected static String wrapLine(String line, String newline, |
|
262 | int wrapColumn) |
|
263 | { |
|
264 | 0 | StringBuffer wrappedLine = new StringBuffer(); |
265 | ||
266 | 0 | while (line.length() > wrapColumn) |
267 | { |
|
268 | 0 | int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn); |
269 | ||
270 | 0 | if (spaceToWrapAt >= 0) |
271 | { |
|
272 | 0 | wrappedLine.append(line.substring(0, spaceToWrapAt)); |
273 | 0 | wrappedLine.append(newline); |
274 | 0 | line = line.substring(spaceToWrapAt + 1); |
275 | } |
|
276 | ||
277 | // This must be a really long word or URL. Pass it |
|
278 | // through unchanged even though it's longer than the |
|
279 | // wrapColumn would allow. This behavior could be |
|
280 | // dependent on a parameter for those situations when |
|
281 | // someone wants long words broken at line length. |
|
282 | else |
|
283 | { |
|
284 | 0 | spaceToWrapAt = line.indexOf(' ', wrapColumn); |
285 | ||
286 | 0 | if (spaceToWrapAt >= 0) |
287 | { |
|
288 | 0 | wrappedLine.append(line.substring(0, spaceToWrapAt)); |
289 | 0 | wrappedLine.append(newline); |
290 | 0 | line = line.substring(spaceToWrapAt + 1); |
291 | } |
|
292 | else |
|
293 | { |
|
294 | 0 | wrappedLine.append(line); |
295 | 0 | line = ""; |
296 | } |
|
297 | } |
|
298 | } |
|
299 | ||
300 | // Whatever is left in line is short enough to just pass through, |
|
301 | // just like a small small kidney stone |
|
302 | 0 | wrappedLine.append(line); |
303 | ||
304 | 0 | return wrappedLine.toString(); |
305 | } |
|
306 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |