KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > jstl > fn > JstlFunction


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag.jstl.fn;
16
17 import java.lang.reflect.Array JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * Implementations of JSTL Functions
23  *
24  * @author Jacob Hookom
25  * @version $Id: JstlFunction.java,v 1.3 2005/08/24 04:38:59 jhook Exp $
26  */

27 public final class JstlFunction {
28
29     private JstlFunction() {
30     }
31
32     public static boolean contains(String JavaDoc name, String JavaDoc searchString) {
33         if (name == null || searchString == null) {
34             return false;
35         }
36
37         return -1 != name.indexOf(searchString);
38     }
39
40     public static boolean containsIgnoreCase(String JavaDoc name, String JavaDoc searchString) {
41         if (name == null || searchString == null) {
42             return false;
43         }
44         return -1 != name.toUpperCase().indexOf(searchString.toUpperCase());
45     }
46
47     public static boolean endsWith(String JavaDoc name, String JavaDoc searchString) {
48         if (name == null || searchString == null) {
49             return false;
50         }
51         return name.endsWith(searchString);
52     }
53
54     public static String JavaDoc escapeXml(String JavaDoc value) {
55         if (value == null) {
56             return null;
57         }
58         return value.replaceAll("<", "&lt;");
59     }
60
61     public static int indexOf(String JavaDoc name, String JavaDoc searchString) {
62         if (name == null || searchString == null) {
63             return -1;
64         }
65         return name.indexOf(searchString);
66     }
67
68     public static String JavaDoc join(String JavaDoc[] a, String JavaDoc delim) {
69         if (a == null || delim == null) {
70             return null;
71         }
72         if (a.length == 0) {
73             return "";
74         }
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(a.length
76                 * (a[0].length() + delim.length()));
77         for (int i = 0; i < a.length; i++) {
78             sb.append(a[i]).append(delim);
79         }
80         return sb.toString();
81     }
82
83     public static int length(Object JavaDoc obj) {
84         if (obj == null) {
85             return 0;
86         }
87         if (obj instanceof Collection JavaDoc) {
88             return ((Collection JavaDoc) obj).size();
89         }
90         if (obj.getClass().isArray()) {
91             return Array.getLength(obj);
92         }
93         if (obj instanceof String JavaDoc) {
94             return ((String JavaDoc) obj).length();
95         }
96         if (obj instanceof Map JavaDoc) {
97             return ((Map JavaDoc) obj).size();
98         }
99         throw new IllegalArgumentException JavaDoc("Object type not supported: "
100                 + obj.getClass().getName());
101     }
102     
103     public static String JavaDoc replace(String JavaDoc value, String JavaDoc a, String JavaDoc b) {
104         if (value == null || a == null || b == null) {
105             return null;
106         }
107         return value.replaceAll(a, b);
108     }
109     
110     public static String JavaDoc[] split(String JavaDoc value, String JavaDoc d) {
111         if (value == null || d == null) {
112             return null;
113         }
114         return value.split(d);
115     }
116     
117     public static boolean startsWith(String JavaDoc value, String JavaDoc p) {
118         if (value == null || p == null) {
119             return false;
120         }
121         return value.startsWith(p);
122     }
123     
124     public static String JavaDoc substring(String JavaDoc v, int s, int e) {
125         if (v == null) {
126             return null;
127         }
128         return v.substring(s, e);
129     }
130     
131     public static String JavaDoc substringAfter(String JavaDoc v, String JavaDoc p) {
132         if (v == null) {
133             return null;
134         }
135         int i = v.indexOf(p);
136         if (i >= 0) {
137             return v.substring(i+p.length());
138         }
139         return null;
140     }
141     
142     public static String JavaDoc substringBefore(String JavaDoc v, String JavaDoc s) {
143         if (v == null) {
144             return null;
145         }
146         int i = v.indexOf(s);
147         if (i > 0) {
148             return v.substring(0, i-1);
149         }
150         return null;
151     }
152     
153     public static String JavaDoc toLowerCase(String JavaDoc v) {
154         if (v == null) {
155             return null;
156         }
157         return v.toLowerCase();
158     }
159     
160     public static String JavaDoc toUpperCase(String JavaDoc v) {
161         if (v == null) {
162             return null;
163         }
164         return v.toUpperCase();
165     }
166     
167     public static String JavaDoc trim(String JavaDoc v) {
168         if (v == null) {
169             return null;
170         }
171         return v.trim();
172     }
173
174 }
175
Popular Tags