KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > util > DjString


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.util;
31
32 public class DjString
33 {
34   static public final String JavaDoc WHITESPACE = " \t\r\n";
35
36   static public final String JavaDoc reverse(String JavaDoc p_string)
37   {
38     StringBuffer JavaDoc arg = new StringBuffer JavaDoc(p_string);
39     return reverse(arg).toString();
40   }
41
42   static public final StringBuffer JavaDoc reverse(StringBuffer JavaDoc p_string)
43   {
44     return p_string.reverse();
45   }
46
47   static public final String JavaDoc left(String JavaDoc p_string, int p_len)
48   {
49     return p_string.length() > p_len ? p_string.substring(0, p_len) : p_string;
50   }
51
52   static public final String JavaDoc right(String JavaDoc p_string, int p_len)
53   {
54     return p_string.length() > p_len ? p_string.substring(p_string.length() - p_len) : p_string;
55   }
56
57   static public final String JavaDoc strip(String JavaDoc p_stringToStrip, String JavaDoc p_sepChars)
58   {
59     StringBuffer JavaDoc arg = new StringBuffer JavaDoc(p_stringToStrip);
60     strip(arg, p_sepChars);
61     return arg.toString();
62   }
63
64   static public final String JavaDoc strip(String JavaDoc p_toStrip)
65   {
66     return (strip(p_toStrip, WHITESPACE));
67   }
68
69   static public final String JavaDoc stripLeft(String JavaDoc p_stringToStrip, String JavaDoc p_sepChars)
70   {
71     StringBuffer JavaDoc arg = new StringBuffer JavaDoc(p_stringToStrip);
72     stripLeft(arg, p_sepChars);
73     return arg.toString();
74   }
75
76   static public final String JavaDoc stripLeft(String JavaDoc p_toStrip)
77   {
78     return (stripLeft(p_toStrip, WHITESPACE));
79   }
80
81   static public final String JavaDoc stripRight(String JavaDoc p_stringToStrip, String JavaDoc p_sepChars)
82   {
83     StringBuffer JavaDoc arg = new StringBuffer JavaDoc(p_stringToStrip);
84     stripRight(arg, p_sepChars);
85     return arg.toString();
86   }
87
88   static public final String JavaDoc stripRight(String JavaDoc p_toStrip)
89   {
90     return (stripRight(p_toStrip, WHITESPACE));
91   }
92
93   static public final String JavaDoc appendToLength(String JavaDoc p_original, String JavaDoc p_toAppend, int len)
94   {
95     if (p_toAppend == null || p_toAppend.length() == 0) throw new RuntimeException JavaDoc("append string is empty");
96     StringBuffer JavaDoc arg = new StringBuffer JavaDoc(p_original);
97     appendToLength(arg, p_toAppend, len);
98     return arg.toString();
99   }
100
101   static public final StringBuffer JavaDoc strip(StringBuffer JavaDoc p_toStrip, String JavaDoc p_sepChars)
102   {
103     stripLeft(p_toStrip, p_sepChars);
104     stripRight(p_toStrip, p_sepChars);
105     return (p_toStrip);
106   }
107
108   static public final StringBuffer JavaDoc stripLeft(StringBuffer JavaDoc p_toStrip, String JavaDoc p_sepChars)
109   {
110     int i = 0;
111     while (i < p_toStrip.length() && p_sepChars.indexOf(p_toStrip.charAt(i)) != -1)
112     {
113       i++;
114     }
115     if (i > 0)
116     {
117       String JavaDoc sres = new String JavaDoc(p_toStrip.toString().substring(i));
118       p_toStrip.setLength(0);
119       p_toStrip.append(sres);
120     }
121     return (p_toStrip);
122   }
123
124   static public final StringBuffer JavaDoc stripRight(StringBuffer JavaDoc p_toStrip, String JavaDoc p_sepChars)
125   {
126     p_toStrip.reverse();
127     stripLeft(p_toStrip, p_sepChars);
128     p_toStrip.reverse();
129     return (p_toStrip);
130   }
131
132   static public final StringBuffer JavaDoc stripRight(StringBuffer JavaDoc p_toStrip)
133   {
134     return (stripRight(p_toStrip, WHITESPACE));
135   }
136
137   static public final StringBuffer JavaDoc stripLeft(StringBuffer JavaDoc p_toStrip)
138   {
139     return (stripLeft(p_toStrip, WHITESPACE));
140   }
141
142   static public final StringBuffer JavaDoc appendToLength(StringBuffer JavaDoc p_original, String JavaDoc p_toAppend, int len)
143   { //p_original.setLength(len);
144
while (p_original.length() < len)
145     {
146       p_original.append(p_toAppend);
147     }
148     p_original.setLength(len);
149     return (p_original);
150   }
151
152   static public final StringBuffer JavaDoc left(StringBuffer JavaDoc p_string, int p_len)
153   {
154     if (p_string.length() > p_len) p_string.setLength(p_len);
155     return (p_string);
156   }
157
158   static public final StringBuffer JavaDoc right(StringBuffer JavaDoc p_string, int p_len)
159   {
160     if (p_string.length() > p_len)
161     {
162       p_string.reverse();
163       p_string.setLength(p_len);
164       p_string.reverse();
165     }
166     return (p_string);
167   }
168
169   static public final String JavaDoc initCapNoLowerCase(String JavaDoc src)
170   {
171     if ((src == null) || (src.length() == 0)) return src;
172     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(src);
173     sb.setCharAt(0, sb.substring(0, 1).toUpperCase().charAt(0));
174     for (int i = 0; i < sb.length(); i++)
175     {
176       if (i > 0)
177       {
178         if ((sb.charAt(i - 1) == ' ') || (sb.charAt(i - 1) == '_'))
179         {
180           sb.setCharAt(i, sb.substring(i, i + 1).toUpperCase().charAt(0));
181         }
182       }
183     }
184     return new String JavaDoc(sb);
185   }
186
187   static public final String JavaDoc initCap(String JavaDoc src)
188   {
189     return initCapNoLowerCase(src.toLowerCase());
190   }
191
192   static public final String JavaDoc getNextLine(StringBuffer JavaDoc srcBuf)
193   {
194     if ((srcBuf == null) || (srcBuf.length() == 0)) return (null);
195     String JavaDoc src = srcBuf.toString();
196     String JavaDoc result = null;
197     int offset = src.indexOf("\n");
198     if (offset >= 0)
199     {
200       result = srcBuf.substring(0, offset + 1);
201       srcBuf.delete(0, offset + 1);
202     }
203     else
204     {
205       result = srcBuf.toString();
206       srcBuf.setLength(0);
207     }
208     return (result);
209   }
210
211   public static final String JavaDoc getNextParam(StringBuffer JavaDoc srcBuf, String JavaDoc separator, char quoteChar, char escapeChar)
212   {
213     if ((srcBuf == null) || (srcBuf.length() == 0)) return (null);
214     String JavaDoc src = srcBuf.toString();
215     src = wipeConstantText(src, quoteChar, escapeChar, ' ');
216     String JavaDoc result = null;
217     int offset = src.indexOf(separator);
218     if (offset >= 0)
219     {
220       result = srcBuf.substring(0, offset);
221       srcBuf.delete(0, offset + separator.length());
222     }
223     else
224     {
225       result = srcBuf.toString();
226       srcBuf.setLength(0);
227     }
228     DjStringReplacer sr = new DjStringReplacer(result);
229     result = sr.replace("" + escapeChar + "" + quoteChar, "" + quoteChar, true); // \'
230
// ->
231
// '
232
result = sr.replace("" + escapeChar + "" + escapeChar, "" + escapeChar, true); // \\
233
// ->
234
// \
235
return (result);
236   }
237
238   public static final String JavaDoc replace(String JavaDoc srcBuf, String JavaDoc fromStr, String JavaDoc toStr)
239   {
240     DjStringReplacer sr = new DjStringReplacer(srcBuf);
241     return sr.replace(fromStr, toStr);
242   }
243
244   public static final String JavaDoc replace(String JavaDoc srcBuf, String JavaDoc fromStr, String JavaDoc toStr, boolean caseSensitive)
245   {
246     DjStringReplacer sr = new DjStringReplacer(srcBuf);
247     return sr.replace(fromStr, toStr, caseSensitive);
248   }
249
250   public static final String JavaDoc makeTitle(String JavaDoc srcBuf)
251   {
252     return replace(initCap(srcBuf), "_", " ");
253   }
254
255   static private final String JavaDoc _valids = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
256
257   public static final String JavaDoc makeClassName(String JavaDoc srcBuf)
258   {
259     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(srcBuf);
260     for (int i = 0; i < sb.length(); i++)
261     {
262       if (_valids.indexOf(sb.charAt(i)) == -1) sb.replace(i, i + 1, "_");
263     }
264     return replace(replace(initCapNoLowerCase(sb.toString()), "_", ""), " ", "");
265   }
266
267   public static String JavaDoc stripPackagePrefixFromClassName(String JavaDoc cls)
268   {
269     String JavaDoc result = cls;
270     cls = DjString.reverse(cls);
271     int offs = cls.indexOf(".");
272     if (offs != -1)
273     {
274       result = DjString.reverse(cls.substring(0, offs));
275     }
276     return result;
277   }
278
279   public static String JavaDoc stripFromBegin(String JavaDoc str, String JavaDoc what)
280   {
281     if (str.trim().toUpperCase().startsWith(what.toUpperCase()))
282     {
283       str = str.trim().substring(what.length()).trim();
284     }
285     return str;
286   }
287
288   // Returns true if a string is null or contains only blanks or is empty
289
public static boolean isEmpty(String JavaDoc str)
290   {
291     if (str == null) return true;
292     if (str.trim().length() == 0) return true;
293     return false;
294   }
295
296   public static String JavaDoc wipeConstantText(String JavaDoc src, char textMarker, char escapeChar, char wipeChar)
297   {
298     int offset = 0;
299     int len = src.length();
300     boolean inText = false;
301     int inc;
302     char[] ca = (src + " ").toCharArray(); // Add the space so we can do
303
// [index+1] stuff later on
304

305     // Wipe all (constant) "text" to make sure that textual stuff like "Here:
306
// <blabla>" doesn't
307
// mess up retrieval of parameters or html parsing
308

309     while (offset < len)
310     {
311       inc = 1;
312       if (inText)
313       {
314         if (ca[offset] == escapeChar && ca[offset + 1] == textMarker)
315         // Skip quotes in text i.e. "some quotes: \" in text"
316
{
317           inc = 2;
318         }
319         else if (ca[offset] == textMarker)
320         {
321           ca[offset] = ' ';
322           inText = false;
323         }
324       }
325       else
326       // !inText
327
{
328         if (ca[offset] == textMarker) inText = true;
329       }
330       if (inText)
331       {
332         ca[offset] = ' ';
333         if (inc != 1) ca[offset + 1] = ' ';
334       }
335       if (!inText)
336       {
337         offset = src.indexOf(textMarker, offset + inc);
338         if (offset == -1) offset = len;
339       }
340       else offset += inc;
341     }
342     return new String JavaDoc(ca);
343   }
344
345   public static boolean isValidInteger(String JavaDoc str)
346   {
347     try
348     {
349       Integer.parseInt(str);
350       return true;
351     }
352     catch (Exception JavaDoc x)
353     {
354       DjLogger.log(x);
355     }
356     return false; // If we get here it's not a valid integer.
357
}
358
359   public static int safeCompare(Object JavaDoc one, Object JavaDoc two)
360   {
361     if (one == null && two == null) return 0;
362     if (one == null) return -1;
363     if (two == null) return 1;
364     return one.toString().toLowerCase().compareTo(two.toString().toLowerCase());
365   }
366
367   public static String JavaDoc indentString(String JavaDoc val, int ind)
368   {
369     if (ind == 0) return val;
370
371     StringBuffer JavaDoc result = new StringBuffer JavaDoc(val);
372
373     if (ind > 0)
374     {
375       for (int x = 0; x < ind; x++)
376         result.insert(0, ' ');
377       int i = 0;
378       while (i < result.length())
379       {
380         if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n')
381         {
382           for (int x = 0; x < ind; x++)
383             result.insert(i + 1, ' ');
384           i += ind;
385         }
386         i++;
387       }
388     }
389     if (ind < 0)
390     {
391       ind = -ind;
392       for (int d = 0; d < ind; d++)
393         if (result.charAt(0) == ' ') result.deleteCharAt(0);
394       int i = 0;
395       while (i < result.length())
396       {
397         if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n')
398         {
399           int end = i + ind;
400           if (end > result.length()) end = result.length();
401           for (int d = i; d < end; d++)
402             if (result.charAt(i + 1) == ' ') result.deleteCharAt(i + 1);
403         }
404         i++;
405       }
406     }
407     return result.toString();
408   }
409 }
Popular Tags