KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > functions > Functions


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.functions;
18
19 import java.lang.reflect.Array JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.servlet.jsp.JspTagException JavaDoc;
27
28 import org.apache.taglibs.standard.resources.Resources;
29 import org.apache.taglibs.standard.tag.common.core.Util;
30
31 /**
32  * <p>JSTL Functions</p>
33  *
34  * @author Pierre Delisle
35  */

36
37 public class Functions {
38
39     //*********************************************************************
40
// String capitalization
41

42     /**
43      * Converts all of the characters of the input string to upper case.
44      */

45     public static String JavaDoc toUpperCase(String JavaDoc input) {
46         return input.toUpperCase();
47     }
48
49     /**
50      * Converts all of the characters of the input string to lower case.
51      */

52     public static String JavaDoc toLowerCase(String JavaDoc input) {
53         return input.toLowerCase();
54     }
55     
56     //*********************************************************************
57
// Substring processing
58

59     public static int indexOf(String JavaDoc input, String JavaDoc substring) {
60         if (input == null) input = "";
61         if (substring == null) substring = "";
62         return input.indexOf(substring);
63     }
64
65     public static boolean contains(String JavaDoc input, String JavaDoc substring) {
66         return indexOf(input, substring) != -1;
67     }
68
69     public static boolean containsIgnoreCase(String JavaDoc input, String JavaDoc substring) {
70         if (input == null) input = "";
71         if (substring == null) substring = "";
72         String JavaDoc inputUC = input.toUpperCase();
73         String JavaDoc substringUC = substring.toUpperCase();
74         return indexOf(inputUC, substringUC) != -1;
75     }
76
77     public static boolean startsWith(String JavaDoc input, String JavaDoc substring) {
78         if (input == null) input = "";
79         if (substring == null) substring = "";
80         return input.startsWith(substring);
81     }
82         
83     public static boolean endsWith(String JavaDoc input, String JavaDoc substring) {
84         if (input == null) input = "";
85         if (substring == null) substring = "";
86         int index = input.lastIndexOf(substring);
87         if (index == -1) return false;
88         if (index == 0 && substring.length() == 0) return true;
89         return (index == input.length() - substring.length());
90     }
91     
92     public static String JavaDoc substring(String JavaDoc input, int beginIndex, int endIndex) {
93         if (input == null) input = "";
94         if (beginIndex >= input.length()) return "";
95         if (beginIndex < 0) beginIndex = 0;
96         if (endIndex < 0 || endIndex > input.length()) endIndex = input.length();
97         if (endIndex < beginIndex) return "";
98         return input.substring(beginIndex, endIndex);
99     }
100     
101     public static String JavaDoc substringAfter(String JavaDoc input, String JavaDoc substring) {
102         if (input == null) input = "";
103         if (input.length() == 0) return "";
104         if (substring == null) substring = "";
105         if (substring.length() == 0) return input;
106         
107         int index = input.indexOf(substring);
108         if (index == -1) {
109             return "";
110         } else {
111             return input.substring(index+substring.length());
112         }
113     }
114         
115     public static String JavaDoc substringBefore(String JavaDoc input, String JavaDoc substring) {
116         if (input == null) input = "";
117         if (input.length() == 0) return "";
118         if (substring == null) substring = "";
119         if (substring.length() == 0) return "";
120
121         int index = input.indexOf(substring);
122         if (index == -1) {
123             return "";
124         } else {
125             return input.substring(0, index);
126         }
127     }
128
129     //*********************************************************************
130
// Character replacement
131

132     public static String JavaDoc escapeXml(String JavaDoc input) {
133         if (input == null) return "";
134         return Util.escapeXml(input);
135     }
136     
137     public static String JavaDoc trim(String JavaDoc input) {
138         if (input == null) return "";
139         return input.trim();
140     }
141
142     public static String JavaDoc replace(
143     String JavaDoc input,
144     String JavaDoc substringBefore,
145     String JavaDoc substringAfter)
146     {
147         if (input == null) input = "";
148         if (input.length() == 0) return "";
149         if (substringBefore == null) substringBefore = "";
150         if (substringBefore.length() == 0) return input;
151                 
152         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(input.length());
153         int startIndex = 0;
154         int index;
155         while ((index = input.indexOf(substringBefore, startIndex)) != -1) {
156             buf.append(input.substring(startIndex, index)).append(substringAfter);
157             startIndex = index + substringBefore.length();
158         }
159         return buf.append(input.substring(startIndex)).toString();
160     }
161     
162     public static String JavaDoc[] split(
163     String JavaDoc input,
164     String JavaDoc delimiters)
165     {
166         String JavaDoc[] array;
167         if (input == null) input = "";
168         if (input.length() == 0) {
169             array = new String JavaDoc[1];
170             array[0] = "";
171             return array;
172         }
173         
174         if (delimiters == null) delimiters = "";
175
176         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(input, delimiters);
177         int count = tok.countTokens();
178         array = new String JavaDoc[count];
179         int i = 0;
180         while (tok.hasMoreTokens()) {
181             array[i++] = tok.nextToken();
182         }
183         return array;
184     }
185         
186     //*********************************************************************
187
// Collections processing
188

189     public static int length(Object JavaDoc obj) throws JspTagException JavaDoc {
190         if (obj == null) return 0;
191         
192         if (obj instanceof String JavaDoc) return ((String JavaDoc)obj).length();
193         if (obj instanceof Collection JavaDoc) return ((Collection JavaDoc)obj).size();
194         if (obj instanceof Map JavaDoc) return ((Map JavaDoc)obj).size();
195         
196         int count = 0;
197         if (obj instanceof Iterator JavaDoc) {
198             Iterator JavaDoc iter = (Iterator JavaDoc)obj;
199             count = 0;
200             while (iter.hasNext()) {
201                 count++;
202                 iter.next();
203             }
204             return count;
205         }
206         if (obj instanceof Enumeration JavaDoc) {
207             Enumeration JavaDoc enum_ = (Enumeration JavaDoc)obj;
208             count = 0;
209             while (enum_.hasMoreElements()) {
210                 count++;
211                 enum_.nextElement();
212             }
213             return count;
214         }
215         try {
216             count = Array.getLength(obj);
217             return count;
218         } catch (IllegalArgumentException JavaDoc ex) {}
219         throw new JspTagException JavaDoc(Resources.getMessage("PARAM_BAD_VALUE"));
220     }
221
222     public static String JavaDoc join(String JavaDoc[] array, String JavaDoc separator) {
223         if (array == null) return "";
224         if (separator == null) separator = "";
225         
226         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
227         for (int i=0; i<array.length; i++) {
228             buf.append(array[i]);
229             if (i < array.length-1) buf.append(separator);
230         }
231         
232         return buf.toString();
233     }
234 }
235
Popular Tags