KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > Functions


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.L10N;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38
39 /**
40  * Tag representing the functions.
41  */

42 public class Functions {
43   private static L10N L = new L10N(Functions.class);
44
45   /**
46    * Returns the length of the object
47    */

48   public static int length(Object JavaDoc obj)
49   {
50     if (obj == null)
51       return 0;
52
53     else if (obj instanceof CharSequence JavaDoc)
54       return ((CharSequence JavaDoc) obj).length();
55
56     else if (obj instanceof Collection JavaDoc)
57       return ((Collection JavaDoc) obj).size();
58       
59     else if (obj instanceof Map JavaDoc)
60       return ((Map JavaDoc) obj).size();
61
62     else if (obj.getClass().isArray())
63       return ((Object JavaDoc []) obj).length;
64
65     else
66       return 0;
67   }
68
69   /**
70    * Returns true if the first string contains the second.
71    */

72   public static boolean contains(String JavaDoc whole, String JavaDoc part)
73   {
74     if (whole == null)
75       whole = "";
76
77     if (part == null)
78       part = "";
79
80     return whole.indexOf(part) >= 0;
81   }
82
83   /**
84    * Returns true if the first string contains the second.
85    */

86   public static boolean containsIgnoreCase(String JavaDoc whole, String JavaDoc part)
87   {
88     if (whole == null)
89       whole = "";
90
91     if (part == null)
92       part = "";
93
94     whole = whole.toUpperCase();
95     part = part.toUpperCase();
96     
97     return whole.indexOf(part) >= 0;
98   }
99
100   /**
101    * Returns true if the first string contains the second.
102    */

103   public static boolean endsWith(String JavaDoc whole, String JavaDoc part)
104   {
105     if (whole == null)
106       whole = "";
107
108     if (part == null)
109       part = "";
110
111     return whole.endsWith(part);
112   }
113
114   /**
115    * Escapes the XML string.
116    */

117   public static String JavaDoc escapeXml(String JavaDoc string)
118   {
119     if (string == null)
120       return "";
121
122     CharBuffer cb = CharBuffer.allocate();
123
124     for (int i = 0; i < string.length(); i++) {
125       int ch = string.charAt(i);
126
127       switch (ch) {
128       case '<':
129         cb.append("&lt;");
130         break;
131       case '>':
132         cb.append("&gt;");
133         break;
134       case '&':
135         cb.append("&amp;");
136         break;
137       case '\'':
138         cb.append("&#039;");
139         break;
140       case '"':
141         cb.append("&#034;");
142         break;
143       default:
144         cb.append((char) ch);
145         break;
146       }
147     }
148
149     return cb.close();
150   }
151
152   /**
153    * Returns the index of the part in the whole.
154    */

155   public static int indexOf(String JavaDoc whole, String JavaDoc part)
156   {
157     if (whole == null)
158       whole = "";
159
160     if (part == null)
161       part = "";
162
163     return whole.indexOf(part);
164   }
165
166   /**
167    * Joins the array.
168    */

169   public static String JavaDoc join(String JavaDoc []array, String JavaDoc sep)
170   {
171     if (array == null)
172       return "";
173
174     if (sep == null)
175       sep = "";
176
177     CharBuffer result = CharBuffer.allocate();
178
179     for (int i = 0; i < array.length; i++) {
180       if (i != 0)
181     result.append(sep);
182
183       result.append(array[i]);
184     }
185
186     return result.close();
187   }
188
189   /**
190    * Replaces substrings.
191    */

192   public static String JavaDoc replace(String JavaDoc input, String JavaDoc before, String JavaDoc after)
193   {
194     if (input == null)
195       return "";
196
197     if (before == null || before.equals(""))
198       return input;
199
200     if (after == null)
201       after = "";
202
203     CharBuffer result = CharBuffer.allocate();
204
205     int head = 0;
206     int next;
207     while ((next = input.indexOf(before, head)) >= 0) {
208       result.append(input.substring(head, next));
209
210       result.append(after);
211
212       head = next + before.length();
213     }
214
215     result.append(input.substring(head));
216
217     return result.close();
218   }
219
220   /**
221    * Splits into
222    */

223   public static String JavaDoc []split(String JavaDoc input, String JavaDoc delimiters)
224   {
225     if (input == null || input.equals("")) {
226       return new String JavaDoc[] {""};
227     }
228
229     if (delimiters == null || delimiters.equals("")) {
230       return new String JavaDoc[] { input };
231     }
232
233     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(input, delimiters);
234     ArrayList JavaDoc<String JavaDoc> values = new ArrayList JavaDoc<String JavaDoc>();
235
236     while (tokenizer.hasMoreTokens())
237       values.add(tokenizer.nextToken());
238
239     String JavaDoc []array = new String JavaDoc[values.size()];
240
241     return values.toArray(array);
242   }
243
244   /**
245    * Returns true if the first string starts with the second.
246    */

247   public static boolean startsWith(String JavaDoc whole, String JavaDoc part)
248   {
249     if (whole == null)
250       whole = "";
251
252     if (part == null)
253       part = "";
254
255     return whole.startsWith(part);
256   }
257
258   /**
259    * Returns true if the first string starts with the second.
260    */

261   public static String JavaDoc substring(String JavaDoc string, int begin, int end)
262   {
263     if (string == null || string.equals(""))
264       return "";
265
266     int length = string.length();
267     
268     if (begin < 0)
269       begin = 0;
270
271     if (length <= begin)
272       return "";
273
274     if (end < 0 || length < end)
275       end = length;
276
277     if (end <= begin)
278       return "";
279
280     return string.substring(begin, end);
281   }
282
283   /**
284    * Returns the substring after the match.
285    */

286   public static String JavaDoc substringAfter(String JavaDoc whole, String JavaDoc part)
287   {
288     if (whole == null || whole.equals(""))
289       return "";
290
291     if (part == null || part.equals(""))
292       return whole;
293
294     int p = whole.indexOf(part);
295
296     if (p < 0)
297       return "";
298     else
299       return whole.substring(p + part.length());
300   }
301
302   /**
303    * Returns the substring before the match.
304    */

305   public static String JavaDoc substringBefore(String JavaDoc whole, String JavaDoc part)
306   {
307     if (whole == null || whole.equals(""))
308       return "";
309
310     if (part == null || part.equals(""))
311       return "";
312
313     int p = whole.indexOf(part);
314
315     if (p < 0)
316       return "";
317     else
318       return whole.substring(0, p);
319   }
320
321   /**
322    * Convert to lower case.
323    */

324   public static String JavaDoc toLowerCase(String JavaDoc string)
325   {
326     if (string == null || string.equals(""))
327       return "";
328
329     return string.toLowerCase();
330   }
331
332   /**
333    * Convert to upper case.
334    */

335   public static String JavaDoc toUpperCase(String JavaDoc string)
336   {
337     if (string == null || string.equals(""))
338       return "";
339
340     return string.toUpperCase();
341   }
342
343   /**
344    * Trim
345    */

346   public static String JavaDoc trim(String JavaDoc string)
347   {
348     if (string == null || string.equals(""))
349       return "";
350
351     return string.trim();
352   }
353 }
354
Popular Tags