Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC1 Coverage timestamp: 星期五 八月 19 2005 13:21:55 CST FRAMES NO FRAMES |
file stats: | LOC: | 337 |  | Methods: | 30
| NCLOC: | 249 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
StringHelper.java | 0% | 0% | 0% | 0% |

1 |  |
     /**
2 |  |
      * @(#)StringHelper.java
3 |  |
      *
4 |  |
      * JFoxSOAF, Service-Oriented Application Framework
5 |  |
      *
6 |  |
      * Copyright(c) JFoxSOAF Team
7 |  |
      *
8 |  |
      * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 |  |
      * you may not use this file except in compliance with the License.
10 |  |
      * You may obtain a copy of the License at
11 |  |
      *
12 |  |
      * http://www.gnu.org/copyleft/lesser.html
13 |  |
      *
14 |  |
      * Unless required by applicable law or agreed to in writing, software
15 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
16 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 |  |
      * See the License for the specific language governing permissions and
18 |  |
      * limitations under the License.
19 |  |
      *
20 |  |
      * For more information, please visit:
21 |  |
      * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 |  |
      * http://www.huihoo.org/jfox/jfoxsoaf
23 |  |
      */
24 |  |
25 |  |
     package org.huihoo.jfox.soaf.util.resource;
26 |  |
27 |  |
     import java.util.Iterator;
28 |  |
     import java.util.StringTokenizer;
29 |  |
30 |  |
     /**
31 |  |
      * String helper.
32 |  |
      *
33 |  |
      * @version $Revision: 1.1 $ $Date: 2005/05/22 06:50:55 $
34 |  |
      * @version Revision: 1.0
35 |  |
      */
36 |  |
37 |  |
     public final class StringHelper {
38 |  |
39 |  |
         public static final String EMPTY_STRING = "";
40 |  |
41 |  |
         public static final char DOT = '.';
42 |  |
43 |  |
         public static final char UNDERSCORE = '_';
44 |  |
45 |  |
         public static final String COMMA_SPACE = ", ";
46 |  |
47 |  |
         public static final String COMMA = ",";
48 |  |
49 |  |
         public static final String OPEN_PAREN = "(";
50 |  |
51 |  |
         public static final String CLOSE_PAREN = ")";
52 |  |
53 |  |
         public static final char SINGLE_QUOTE = '\'';
54 |  |
55 | 0 |
         public static String join(String seperator, String[] strings) {
56 | 0 |
             int length = strings.length;
57 | 0 |
             if (length == 0)
58 | 0 |
                 return EMPTY_STRING;
59 | 0 |
             StringBuffer buf = new StringBuffer(length * strings[0].length())
60 |  |
                     .append(strings[0]);
61 | 0 |
             for (int i = 1; i < length; i++) {
62 | 0 |
                 buf.append(seperator).append(strings[i]);
63 |  |
             }
64 | 0 |
             return buf.toString();
65 |  |
         }
66 |  |
67 | 0 |
         public static String join(String seperator, Iterator objects) {
68 | 0 |
             StringBuffer buf = new StringBuffer();
69 | 0 |
             if (objects.hasNext())
70 | 0 |
                 buf.append(objects.next());
71 | 0 |
             while (objects.hasNext()) {
72 | 0 |
                 buf.append(seperator).append(objects.next());
73 |  |
             }
74 | 0 |
             return buf.toString();
75 |  |
         }
76 |  |
77 | 0 |
         public static String[] add(String[] x, String sep, String[] y) {
78 | 0 |
             String[] result = new String[x.length];
79 | 0 |
             for (int i = 0; i < x.length; i++) {
80 | 0 |
                 result[i] = x[i] + sep + y[i];
81 |  |
             }
82 | 0 |
             return result;
83 |  |
         }
84 |  |
85 | 0 |
         public static String repeat(String string, int times) {
86 | 0 |
             StringBuffer buf = new StringBuffer(string.length() * times);
87 | 0 |
             for (int i = 0; i < times; i++)
88 | 0 |
                 buf.append(string);
89 | 0 |
             return buf.toString();
90 |  |
         }
91 |  |
92 | 0 |
         public static String replace(String template, String placeholder,
93 |  |
                 String replacement) {
94 | 0 |
             return replace(template, placeholder, replacement, false);
95 |  |
         }
96 |  |
97 | 0 |
         public static String replace(String template, String placeholder,
98 |  |
                 String replacement, boolean wholeWords) {
99 | 0 |
             int loc = template.indexOf(placeholder);
100 | 0 |
             if (loc < 0) {
101 | 0 |
                 return template;
102 |  |
             } else {
103 | 0 |
                 final boolean actuallyReplace = !wholeWords
104 |  |
                         || loc + placeholder.length() == template.length()
105 |  |
                         || !Character.isJavaIdentifierPart(template.charAt(loc
106 |  |
                                 + placeholder.length()));
107 | 0 |
                 String actualReplacement = actuallyReplace ? replacement
108 |  |
                         : placeholder;
109 | 0 |
                 return new StringBuffer(template.substring(0, loc)).append(
110 |  |
                         actualReplacement).append(
111 |  |
                         replace(template.substring(loc + placeholder.length()),
112 |  |
                                 placeholder, replacement, wholeWords)).toString();
113 |  |
             }
114 |  |
         }
115 |  |
116 | 0 |
         public static String replaceOnce(String template, String placeholder,
117 |  |
                 String replacement) {
118 | 0 |
             int loc = template.indexOf(placeholder);
119 | 0 |
             if (loc < 0) {
120 | 0 |
                 return template;
121 |  |
             } else {
122 | 0 |
                 return new StringBuffer(template.substring(0, loc)).append(
123 |  |
                         replacement).append(
124 |  |
                         template.substring(loc + placeholder.length())).toString();
125 |  |
             }
126 |  |
         }
127 |  |
128 | 0 |
         public static String[] split(String seperators, String list) {
129 | 0 |
             return split(seperators, list, false);
130 |  |
         }
131 |  |
132 | 0 |
         public static String[] split(String seperators, String list, boolean include) {
133 | 0 |
             StringTokenizer tokens = new StringTokenizer(list, seperators, include);
134 | 0 |
             String[] result = new String[tokens.countTokens()];
135 | 0 |
             int i = 0;
136 | 0 |
             while (tokens.hasMoreTokens()) {
137 | 0 |
                 result[i++] = tokens.nextToken();
138 |  |
             }
139 | 0 |
             return result;
140 |  |
         }
141 |  |
142 | 0 |
         public static String unqualify(String qualifiedName) {
143 | 0 |
             return unqualify(qualifiedName, ".");
144 |  |
         }
145 |  |
146 | 0 |
         public static String unqualify(String qualifiedName, String seperator) {
147 | 0 |
             return qualifiedName
148 |  |
                     .substring(qualifiedName.lastIndexOf(seperator) + 1);
149 |  |
         }
150 |  |
151 | 0 |
         public static String qualifier(String qualifiedName) {
152 | 0 |
             int loc = qualifiedName.lastIndexOf(".");
153 | 0 |
             if (loc < 0) {
154 | 0 |
                 return EMPTY_STRING;
155 |  |
             } else {
156 | 0 |
                 return qualifiedName.substring(0, loc);
157 |  |
             }
158 |  |
         }
159 |  |
160 | 0 |
         public static String[] suffix(String[] columns, String suffix) {
161 | 0 |
             if (suffix == null)
162 | 0 |
                 return columns;
163 | 0 |
             String[] qualified = new String[columns.length];
164 | 0 |
             for (int i = 0; i < columns.length; i++) {
165 | 0 |
                 qualified[i] = suffix(columns[i], suffix);
166 |  |
             }
167 | 0 |
             return qualified;
168 |  |
         }
169 |  |
170 | 0 |
         public static String suffix(String name, String suffix) {
171 | 0 |
             return (suffix == null) ? name : name + suffix;
172 |  |
         }
173 |  |
174 | 0 |
         public static String[] prefix(String[] columns, String prefix) {
175 | 0 |
             if (prefix == null)
176 | 0 |
                 return columns;
177 | 0 |
             String[] qualified = new String[columns.length];
178 | 0 |
             for (int i = 0; i < columns.length; i++) {
179 | 0 |
                 qualified[i] = prefix + columns[i];
180 |  |
             }
181 | 0 |
             return qualified;
182 |  |
         }
183 |  |
184 | 0 |
         public static String root(String qualifiedName) {
185 | 0 |
             int loc = qualifiedName.indexOf(".");
186 | 0 |
             return (loc < 0) ? qualifiedName : qualifiedName.substring(0, loc);
187 |  |
         }
188 |  |
189 | 0 |
         public static boolean booleanValue(String tfString) {
190 | 0 |
             String trimmed = tfString.trim().toLowerCase();
191 | 0 |
             return trimmed.equals("true") || trimmed.equals("t");
192 |  |
         }
193 |  |
194 | 0 |
         public static String toString(Object[] array) {
195 | 0 |
             int len = array.length;
196 | 0 |
             if (len == 0)
197 | 0 |
                 return StringHelper.EMPTY_STRING;
198 | 0 |
             StringBuffer buf = new StringBuffer(len * 12);
199 | 0 |
             for (int i = 0; i < len - 1; i++) {
200 | 0 |
                 buf.append(array[i]).append(StringHelper.COMMA_SPACE);
201 |  |
             }
202 | 0 |
             return buf.append(array[len - 1]).toString();
203 |  |
         }
204 |  |
205 | 0 |
         public static String[] multiply(String string, Iterator placeholders,
206 |  |
                 Iterator replacements) {
207 | 0 |
             String[] result = new String[] { string };
208 | 0 |
             while (placeholders.hasNext()) {
209 | 0 |
                 result = multiply(result, (String) placeholders.next(),
210 |  |
                         (String[]) replacements.next());
211 |  |
             }
212 | 0 |
             return result;
213 |  |
         }
214 |  |
215 | 0 |
         private static String[] multiply(String[] strings, String placeholder,
216 |  |
                 String[] replacements) {
217 | 0 |
             String[] results = new String[replacements.length * strings.length];
218 | 0 |
             int n = 0;
219 | 0 |
             for (int i = 0; i < replacements.length; i++) {
220 | 0 |
                 for (int j = 0; j < strings.length; j++) {
221 | 0 |
                     results[n++] = replaceOnce(strings[j], placeholder,
222 |  |
                             replacements[i]);
223 |  |
                 }
224 |  |
             }
225 | 0 |
             return results;
226 |  |
         }
227 |  |
228 |  |
         /*
229 |  |
          * public static String unQuote(String name) { return (
230 |  |
          * Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 ) ? name.substring(1,
231 |  |
          * name.length()-1) : name; } public static void unQuoteInPlace(String[]
232 |  |
          * names) { for ( int i=0; i <names.length; i++ ) names[i] = unQuote(
233 |  |
          * names[i] ); } public static String[] unQuote(String[] names) { String[]
234 |  |
          * unquoted = new String[ names.length ]; for ( int i=0; i <names.length;
235 |  |
          * i++ ) unquoted[i] = unQuote( names[i] ); return unquoted; }
236 |  |
          */
237 |  |
238 | 0 |
         public static int count(String string, char character) {
239 | 0 |
             int n = 0;
240 | 0 |
             for (int i = 0; i < string.length(); i++) {
241 | 0 |
                 if (string.charAt(i) == character)
242 | 0 |
                     n++;
243 |  |
             }
244 | 0 |
             return n;
245 |  |
         }
246 |  |
247 | 0 |
         public static int countUnquoted(String string, char character) {
248 | 0 |
             if (SINGLE_QUOTE == character) {
249 | 0 |
                 throw new IllegalArgumentException(
250 |  |
                         "Unquoted count of quotes is invalid");
251 |  |
             }
252 |  |
             // Impl note: takes advantage of the fact that an escpaed single quote
253 |  |
             // embedded within a quote-block can really be handled as two seperate
254 |  |
             // quote-blocks for the purposes of this method...
255 | 0 |
             int count = 0;
256 | 0 |
             int stringLength = string == null ? 0 : string.length();
257 | 0 |
             boolean inQuote = false;
258 | 0 |
             for (int indx = 0; indx < stringLength; indx++) {
259 | 0 |
                 if (inQuote) {
260 | 0 |
                     if (SINGLE_QUOTE == string.charAt(indx)) {
261 | 0 |
                         inQuote = false;
262 |  |
                     }
263 | 0 |
                 } else if (SINGLE_QUOTE == string.charAt(indx)) {
264 | 0 |
                     inQuote = true;
265 | 0 |
                 } else if (string.charAt(indx) == character) {
266 | 0 |
                     count++;
267 |  |
                 }
268 |  |
             }
269 | 0 |
             return count;
270 |  |
         }
271 |  |
272 | 0 |
         public static boolean isNotEmpty(String string) {
273 | 0 |
             return string != null && string.length() > 0;
274 |  |
         }
275 |  |
276 | 0 |
         public static String qualify(String prefix, String name) {
277 | 0 |
             char first = name.charAt(0);
278 | 0 |
             if (first == SINGLE_QUOTE || // a SQLstring literal
279 |  |
                     Character.isDigit(first) // a SQL numeric literal
280 |  |
281 |  |
             ) {
282 | 0 |
                 return name;
283 |  |
             } else {
284 | 0 |
                 return new StringBuffer(prefix.length() + name.length() + 1)
285 |  |
                         .append(prefix).append(DOT).append(name).toString();
286 |  |
             }
287 |  |
         }
288 |  |
289 | 0 |
         public static String[] qualify(String prefix, String[] names) {
290 | 0 |
             if (prefix == null)
291 | 0 |
                 return names;
292 | 0 |
             int len = names.length;
293 | 0 |
             String[] qualified = new String[len];
294 | 0 |
             for (int i = 0; i < len; i++) {
295 | 0 |
                 qualified[i] = qualify(prefix, names[i]);
296 |  |
             }
297 | 0 |
             return qualified;
298 |  |
         }
299 |  |
300 | 0 |
         private StringHelper() { /* static methods only - hide constructor */
301 |  |
         }
302 |  |
303 | 0 |
         public static int firstIndexOfChar(String sqlString, String string,
304 |  |
                 int startindex) {
305 | 0 |
             int matchAt = -1;
306 | 0 |
             for (int i = 0; i < string.length(); i++) {
307 | 0 |
                 int curMatch = sqlString.indexOf(string.charAt(i), startindex);
308 | 0 |
                 if (curMatch >= 0) {
309 | 0 |
                     if (matchAt == -1) { // first time we find match!
310 | 0 |
                         matchAt = curMatch;
311 |  |
                     } else {
312 | 0 |
                         matchAt = Math.min(matchAt, curMatch);
313 |  |
                     }
314 |  |
                 }
315 |  |
             }
316 | 0 |
             return matchAt;
317 |  |
         }
318 |  |
319 | 0 |
         public static String truncate(String string, int length) {
320 | 0 |
             if (string.length() <= length) {
321 | 0 |
                 return string;
322 |  |
             } else {
323 | 0 |
                 return string.substring(0, length);
324 |  |
             }
325 |  |
         }
326 |  |
327 | 0 |
         public static String toUpperCase(String str) {
328 | 0 |
             return str == null ? null : str.toUpperCase();
329 |  |
         }
330 |  |
331 | 0 |
         public static String toLowerCase(String str) {
332 | 0 |
             return str == null ? null : str.toLowerCase();
333 |  |
         }
334 |  |
335 |  |
     }
336 |  |
337 |  |