001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jstl;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.util.L10N;
033:
034: import java.lang.reflect.*;
035: import java.util.*;
036:
037: /**
038: * Tag representing the functions.
039: */
040: public class Functions {
041: private static L10N L = new L10N(Functions.class);
042:
043: /**
044: * Returns the length of the object
045: */
046: public static int length(Object obj) {
047: if (obj == null)
048: return 0;
049:
050: else if (obj instanceof CharSequence)
051: return ((CharSequence) obj).length();
052:
053: else if (obj instanceof Collection)
054: return ((Collection) obj).size();
055:
056: else if (obj instanceof Map)
057: return ((Map) obj).size();
058:
059: else if (obj.getClass().isArray())
060: return Array.getLength(obj);
061:
062: else if (obj instanceof Iterator) {
063: Iterator iter = (Iterator) obj;
064:
065: int count = 0;
066: while (iter.hasNext()) {
067: count++;
068:
069: iter.next();
070: }
071:
072: return count;
073: }
074:
075: else
076: return 0;
077: }
078:
079: /**
080: * Returns true if the first string contains the second.
081: */
082: public static boolean contains(String whole, String part) {
083: if (whole == null)
084: whole = "";
085:
086: if (part == null)
087: part = "";
088:
089: return whole.indexOf(part) >= 0;
090: }
091:
092: /**
093: * Returns true if the first string contains the second.
094: */
095: public static boolean containsIgnoreCase(String whole, String part) {
096: if (whole == null)
097: whole = "";
098:
099: if (part == null)
100: part = "";
101:
102: whole = whole.toUpperCase();
103: part = part.toUpperCase();
104:
105: return whole.indexOf(part) >= 0;
106: }
107:
108: /**
109: * Returns true if the first string contains the second.
110: */
111: public static boolean endsWith(String whole, String part) {
112: if (whole == null)
113: whole = "";
114:
115: if (part == null)
116: part = "";
117:
118: return whole.endsWith(part);
119: }
120:
121: /**
122: * Escapes the XML string.
123: */
124: public static String escapeXml(String string) {
125: if (string == null)
126: return "";
127:
128: CharBuffer cb = CharBuffer.allocate();
129:
130: for (int i = 0; i < string.length(); i++) {
131: int ch = string.charAt(i);
132:
133: switch (ch) {
134: case '<':
135: cb.append("<");
136: break;
137: case '>':
138: cb.append(">");
139: break;
140: case '&':
141: cb.append("&");
142: break;
143: case '\'':
144: cb.append("'");
145: break;
146: case '"':
147: cb.append(""");
148: break;
149: default:
150: cb.append((char) ch);
151: break;
152: }
153: }
154:
155: return cb.close();
156: }
157:
158: /**
159: * Returns the index of the part in the whole.
160: */
161: public static int indexOf(String whole, String part) {
162: if (whole == null)
163: whole = "";
164:
165: if (part == null)
166: part = "";
167:
168: return whole.indexOf(part);
169: }
170:
171: /**
172: * Joins the array.
173: */
174: public static String join(String[] array, String sep) {
175: if (array == null)
176: return "";
177:
178: if (sep == null)
179: sep = "";
180:
181: CharBuffer result = CharBuffer.allocate();
182:
183: for (int i = 0; i < array.length; i++) {
184: if (i != 0)
185: result.append(sep);
186:
187: result.append(array[i]);
188: }
189:
190: return result.close();
191: }
192:
193: /**
194: * Replaces substrings.
195: */
196: public static String replace(String input, String before,
197: String after) {
198: if (input == null)
199: return "";
200:
201: if (before == null || before.equals(""))
202: return input;
203:
204: if (after == null)
205: after = "";
206:
207: CharBuffer result = CharBuffer.allocate();
208:
209: int head = 0;
210: int next;
211: while ((next = input.indexOf(before, head)) >= 0) {
212: result.append(input.substring(head, next));
213:
214: result.append(after);
215:
216: head = next + before.length();
217: }
218:
219: result.append(input.substring(head));
220:
221: return result.close();
222: }
223:
224: /**
225: * Splits into
226: */
227: public static String[] split(String input, String delimiters) {
228: if (input == null || input.equals("")) {
229: return new String[] { "" };
230: }
231:
232: if (delimiters == null || delimiters.equals("")) {
233: return new String[] { input };
234: }
235:
236: StringTokenizer tokenizer = new StringTokenizer(input,
237: delimiters);
238: ArrayList<String> values = new ArrayList<String>();
239:
240: while (tokenizer.hasMoreTokens())
241: values.add(tokenizer.nextToken());
242:
243: String[] array = new String[values.size()];
244:
245: return values.toArray(array);
246: }
247:
248: /**
249: * Returns true if the first string starts with the second.
250: */
251: public static boolean startsWith(String whole, String part) {
252: if (whole == null)
253: whole = "";
254:
255: if (part == null)
256: part = "";
257:
258: return whole.startsWith(part);
259: }
260:
261: /**
262: * Returns true if the first string starts with the second.
263: */
264: public static String substring(String string, int begin, int end) {
265: if (string == null || string.equals(""))
266: return "";
267:
268: int length = string.length();
269:
270: if (begin < 0)
271: begin = 0;
272:
273: if (length <= begin)
274: return "";
275:
276: if (end < 0 || length < end)
277: end = length;
278:
279: if (end <= begin)
280: return "";
281:
282: return string.substring(begin, end);
283: }
284:
285: /**
286: * Returns the substring after the match.
287: */
288: public static String substringAfter(String whole, String part) {
289: if (whole == null || whole.equals(""))
290: return "";
291:
292: if (part == null || part.equals(""))
293: return whole;
294:
295: int p = whole.indexOf(part);
296:
297: if (p < 0)
298: return "";
299: else
300: return whole.substring(p + part.length());
301: }
302:
303: /**
304: * Returns the substring before the match.
305: */
306: public static String substringBefore(String whole, String part) {
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 toLowerCase(String string) {
325: if (string == null || string.equals(""))
326: return "";
327:
328: return string.toLowerCase();
329: }
330:
331: /**
332: * Convert to upper case.
333: */
334: public static String toUpperCase(String string) {
335: if (string == null || string.equals(""))
336: return "";
337:
338: return string.toUpperCase();
339: }
340:
341: /**
342: * Trim
343: */
344: public static String trim(String string) {
345: if (string == null || string.equals(""))
346: return "";
347:
348: return string.trim();
349: }
350: }
|