KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > Strings


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 package org.lobobrowser.util;
22
23 import java.io.*;
24 import java.security.*;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28  * Summary description for Strings.
29  */

30 public class Strings
31 {
32     private static final MessageDigest MESSAGE_DIGEST;
33     public static final String JavaDoc[] EMPTY_ARRAY = new String JavaDoc[0];
34
35     static {
36         MessageDigest md;
37         try {
38             md = MessageDigest.getInstance("MD5");
39         } catch(NoSuchAlgorithmException err) {
40             throw new IllegalStateException JavaDoc();
41         }
42         MESSAGE_DIGEST = md;
43     }
44
45     private Strings() {}
46
47     public static int compareVersions(String JavaDoc version1, String JavaDoc version2) {
48         return version1.compareTo(version2);
49     }
50     
51     public static boolean isBlank(String JavaDoc text) {
52         return text == null || "".equals(text);
53     }
54     
55     public static int countLines(String JavaDoc text)
56     {
57         int startIdx = 0;
58         int lineCount = 1;
59         for(;;)
60         {
61             int lbIdx = text.indexOf('\n', startIdx);
62             if(lbIdx == -1)
63             {
64                 break;
65             }
66             lineCount++;
67             startIdx = lbIdx + 1;
68         }
69         return lineCount;
70     }
71
72     public static boolean isJavaIdentifier(String JavaDoc id)
73     {
74         if(id == null)
75         {
76             return false;
77         }
78         int len = id.length();
79         if(len == 0)
80         {
81             return false;
82         }
83         if(!Character.isJavaIdentifierStart(id.charAt(0)))
84         {
85             return false;
86         }
87         for(int i = 1; i < len; i++)
88         {
89             if(!Character.isJavaIdentifierPart(id.charAt(i)))
90             {
91                 return false;
92             }
93         }
94         return true;
95     }
96     
97     public static String JavaDoc getTextFromStream(InputStream in) throws IOException {
98         Reader reader = new InputStreamReader(in, "UTF-8");
99         char[] buffer = new char[256];
100         int numRead;
101         int offset = 0;
102         while((numRead = reader.read(buffer, offset, buffer.length - offset)) != -1) {
103             offset += numRead;
104             if(offset >= (buffer.length * 2) / 3) {
105                 char[] newBuffer = new char[buffer.length * 2];
106                 System.arraycopy(buffer, 0, newBuffer, 0, offset);
107                 buffer = newBuffer;
108             }
109         }
110         return new String JavaDoc(buffer, 0, offset);
111     }
112     
113     public static String JavaDoc getJavaStringLiteral(String JavaDoc text) {
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115         buf.append('"');
116         int len = text.length();
117         for(int i = 0; i < len; i++) {
118             char ch = text.charAt(i);
119             switch(ch) {
120             case '\\':
121                 buf.append("\\\\");
122                 break;
123             case '\n':
124                 buf.append("\\n");
125                 break;
126             case '\r':
127                 buf.append("\\r");
128                 break;
129             case '\t':
130                 buf.append("\\t");
131                 break;
132             case '"':
133                 buf.append("\\\"");
134                 break;
135             default:
136                 buf.append(ch);
137                 break;
138             }
139         }
140         buf.append('"');
141         return buf.toString();
142     }
143     
144     public static String JavaDoc getJavaIdentifier(String JavaDoc candidateID) {
145         int len = candidateID.length();
146         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
147         for(int i = 0; i < len; i++)
148         {
149             char ch = candidateID.charAt(i);
150             boolean good = i == 0 ? Character.isJavaIdentifierStart(ch) : Character.isJavaIdentifierPart(ch);
151             if(good)
152             {
153                 buf.append(ch);
154             }
155             else {
156                 buf.append('_');
157             }
158         }
159         return buf.toString();
160     }
161     
162     private static final String JavaDoc HEX_CHARS = "0123456789ABCDEF";
163     
164     public static String JavaDoc getMD5(String JavaDoc source) throws UnsupportedEncodingException {
165         byte[] bytes = source.getBytes("UTF8");
166         byte[] result;
167         synchronized(MESSAGE_DIGEST) {
168             MESSAGE_DIGEST.update(bytes);
169             result = MESSAGE_DIGEST.digest();
170         }
171         char[] resChars = new char[32];
172         int len = result.length;
173         for(int i = 0; i < len; i++) {
174             byte b = result[i];
175             int lo4 = b & 0x0F;
176             int hi4 = (b & 0xF0) >> 4;
177             resChars[i*2] = HEX_CHARS.charAt(hi4);
178             resChars[i*2 + 1] = HEX_CHARS.charAt(lo4);
179         }
180         return new String JavaDoc(resChars);
181     }
182     
183     public static String JavaDoc getHash32(String JavaDoc source) throws UnsupportedEncodingException {
184         String JavaDoc md5 = getMD5(source);
185         return md5.substring(0, 8);
186     }
187
188     public static String JavaDoc getHash64(String JavaDoc source) throws UnsupportedEncodingException {
189         String JavaDoc md5 = getMD5(source);
190         return md5.substring(0, 16);
191     }
192     
193     public static int countChars(String JavaDoc text, char ch) {
194         int len = text.length();
195         int count = 0;
196         for(int i = 0; i < len; i++) {
197             if(ch == text.charAt(i)) {
198                 count++;
199             }
200         }
201         return count;
202     }
203     
204 // public static boolean isTrimmable(char ch) {
205
// switch(ch) {
206
// case ' ':
207
// case '\t':
208
// case '\r':
209
// case '\n':
210
// return true;
211
// }
212
// return false;
213
// }
214
//
215
// /**
216
// * Trims blanks, line breaks and tabs.
217
// * @param text
218
// * @return
219
// */
220
// public static String trim(String text) {
221
// int len = text.length();
222
// int startIdx;
223
// for(startIdx = 0; startIdx < len; startIdx++) {
224
// char ch = text.charAt(startIdx);
225
// if(!isTrimmable(ch)) {
226
// break;
227
// }
228
// }
229
// int endIdx;
230
// for(endIdx = len; --endIdx > startIdx; ) {
231
// char ch = text.charAt(endIdx);
232
// if(!isTrimmable(ch)) {
233
// break;
234
// }
235
// }
236
// return text.substring(startIdx, endIdx + 1);
237
// }
238

239     public static String JavaDoc unquote(String JavaDoc text) {
240         if(text.startsWith("\"") && text.endsWith("\"")) {
241             return text.substring(1, text.length() - 2);
242         }
243         return text;
244     }
245     
246     public static String JavaDoc[] split(String JavaDoc phrase) {
247         int length = phrase.length();
248         ArrayList JavaDoc wordList = new ArrayList JavaDoc();
249         StringBuffer JavaDoc word = null;
250         for(int i = 0; i < length; i++) {
251             char ch = phrase.charAt(i);
252             switch(ch) {
253             case ' ':
254             case '\t':
255             case '\r':
256             case '\n':
257                 if(word != null) {
258                     wordList.add(word.toString());
259                     word = null;
260                 }
261                 break;
262             default:
263                 if(word == null) {
264                     word = new StringBuffer JavaDoc();
265                 }
266                 word.append(ch);
267             }
268         }
269         if(word != null) {
270             wordList.add(word.toString());
271         }
272         return (String JavaDoc[]) wordList.toArray(EMPTY_ARRAY);
273     }
274     
275     public static String JavaDoc truncate(String JavaDoc text, int maxLength) {
276         if(text == null) {
277             return null;
278         }
279         if(text.length() <= maxLength) {
280             return text;
281         }
282         return text.substring(0, Math.max(maxLength - 3, 0)) + "...";
283     }
284     
285     public static String JavaDoc strictHtmlEncode(String JavaDoc rawText) {
286         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
287         int length = rawText.length();
288         for(int i = 0; i < length; i++) {
289             char ch = rawText.charAt(i);
290             switch(ch) {
291             case '&':
292                 output.append("&amp;");
293                 break;
294             case '"':
295                 output.append("&quot;");
296                 break;
297             case '<':
298                 output.append("&lt;");
299                 break;
300             case '>':
301                 output.append("&gt;");
302                 break;
303             default:
304                 output.append(ch);
305             }
306         }
307         return output.toString();
308     }
309     
310     public static String JavaDoc trimForAlphaNumDash(String JavaDoc rawText) {
311         int length = rawText.length();
312         for(int i = 0; i < length; i++) {
313             char ch = rawText.charAt(i);
314             if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '-') {
315                 continue;
316             }
317             return rawText.substring(0, i);
318         }
319         return rawText;
320     }
321 }
322
Popular Tags