KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > util > DefaultFunctions


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.scriptengine.core.util;
31
32 import com.genimen.djeneric.language.Messages;
33 import com.genimen.djeneric.repository.DjObject;
34 import com.genimen.djeneric.util.DjStringReplacer;
35
36 public class DefaultFunctions implements FunctionSupport
37 {
38
39   public DefaultFunctions()
40   {
41   }
42
43   public boolean isStaticCall(String JavaDoc function)
44   {
45     return function.equals("switch") || function.equals("getParameter");
46   }
47
48   public Object JavaDoc staticCall(String JavaDoc function, Object JavaDoc[] params, DjScriptExecutionTimeScope context) throws Exception JavaDoc
49   {
50     if (function.equals("switch")) return doSwitch(params);
51     if (function.equals("getParameter")) return getParameter(params, context);
52
53     throw new Exception JavaDoc(Messages.getString("DefaultConvention.StaticFunction", function));
54   }
55
56   public boolean isCall(String JavaDoc function)
57   {
58     return function.equals("indexOf") || function.equals("lastIndexOf") || function.equals("length")
59            || function.equals("startsWith") || function.equals("substring") || function.equals("toLowerCase")
60            || function.equals("toUpperCase") || function.equals("trim") || function.equals("initcap")
61            || function.equals("getClassName") || function.equals("isNegative") || function.equals("isPositive")
62            || function.equals("replace");
63
64   }
65
66   public Object JavaDoc call(Object JavaDoc self, String JavaDoc function, Object JavaDoc[] params, DjScriptExecutionTimeScope context)
67       throws Exception JavaDoc
68   {
69     if (function.equals("indexOf")) return indexOf(self, params);
70     if (function.equals("lastIndexOf")) return lastIndexOf(self, params);
71     if (function.equals("length")) return length(self, params);
72     if (function.equals("startsWith")) return startsWith(self, params);
73     if (function.equals("substring")) return substring(self, params);
74     if (function.equals("toLowerCase")) return toLowerCase(self, params);
75     if (function.equals("toUpperCase")) return toUpperCase(self, params);
76     if (function.equals("trim")) return trim(self, params);
77     if (function.equals("initcap")) return initcap(self, params);
78     if (function.equals("getClassName")) return getClassName(self, params);
79     if (function.equals("isNegative")) return isNegative(self, params);
80     if (function.equals("isPositive")) return isPositive(self, params);
81     if (function.equals("replace")) return replace(self, params);
82
83     throw new Exception JavaDoc(Messages.getString("DefaultConvention.Function", function));
84   }
85
86   // Static functions
87
Object JavaDoc doSwitch(Object JavaDoc[] params) throws Exception JavaDoc
88   {
89     if (params.length < 3) throw new Exception JavaDoc(Messages.getString("DefaultConvention.switchExpects3"));
90     Object JavaDoc value = params[0];
91     for (int i = 1; i < params.length - 1; i += 2)
92     {
93       if (value.equals(params[i])) return params[i + 1];
94     }
95     if ((params.length % 2) != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.switchFailed", value));
96     return params[params.length - 1];
97   }
98
99   Object JavaDoc getParameter(Object JavaDoc[] params, DjScriptExecutionTimeScope context) throws Exception JavaDoc
100   {
101     if (params.length != 1) throw new Exception JavaDoc(Messages.getString("DefaultConvention.getParameterExpects1"));
102     String JavaDoc name = params[0].toString();
103     return context.getParameter(name);
104   }
105
106   // Normal functions
107

108   Object JavaDoc indexOf(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
109   {
110     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.indexOfOnlyOnStrings"));
111
112     if (params.length == 1)
113     {
114       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
115           .getString("DefaultConvention.indexOfTakesString"));
116       return new Integer JavaDoc(self.toString().indexOf(params[0].toString()));
117     }
118     if (params.length == 2)
119     {
120       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
121           .getString("DefaultConvention.indexOfTakesStringAsFirst"));
122       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc(Messages
123           .getString("DefaultConvention.indexOfTakesIntAsSecond"));
124       return new Integer JavaDoc(self.toString().indexOf(params[0].toString(), ((Integer JavaDoc) params[1]).intValue()));
125     }
126     throw new Exception JavaDoc(Messages.getString("DefaultConvention.indexOfTakes1or2Params"));
127   }
128
129   Object JavaDoc lastIndexOf(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
130   {
131     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.lastIndexOfOnlyStrings"));
132
133     if (params.length == 1)
134     {
135       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
136           .getString("DefaultConvention.lastIndexOfTakesAString"));
137       return new Integer JavaDoc(self.toString().lastIndexOf(params[0].toString()));
138     }
139     if (params.length == 2)
140     {
141       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
142           .getString("DefaultConvention.lastIndexOfTakesStringAtFirst"));
143       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc(Messages
144           .getString("DefaultConvention.lastIndexOfIntAsSecond"));
145       return new Integer JavaDoc(self.toString().lastIndexOf(params[0].toString(), ((Integer JavaDoc) params[1]).intValue()));
146     }
147     throw new Exception JavaDoc(Messages.getString("DefaultConvention.lastIndexOfTakes1or2Params"));
148   }
149
150   Object JavaDoc length(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
151   {
152     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.lengthTakesNoParams"));
153     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.lengthOnlyOnString"));
154     return new Integer JavaDoc(self.toString().length());
155   }
156
157   Object JavaDoc startsWith(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
158   {
159     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.startsWithOnlyStrings"));
160
161     if (params.length == 1)
162     {
163       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
164           .getString("DefaultConvention.startsWithTakesString"));
165       return new Boolean JavaDoc(self.toString().startsWith(params[0].toString()));
166     }
167     if (params.length == 2)
168     {
169       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
170           .getString("DefaultConvention.startsWithTakesStringAsFirst"));
171       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc(Messages
172           .getString("DefaultConvention.startsWithTakesIntAtSecond"));
173       return new Boolean JavaDoc(self.toString().startsWith(params[0].toString(), ((Integer JavaDoc) params[1]).intValue()));
174     }
175     throw new Exception JavaDoc(Messages.getString("DefaultConvention.startsWithTakes1or2Params"));
176   }
177
178   Object JavaDoc substring(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
179   {
180     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.substringOnlyOnStrings"));
181
182     if (params.length == 1)
183     {
184       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
185           .getString("DefaultConvention.substringTakesAString"));
186       return self.toString().substring(((Integer JavaDoc) params[0]).intValue());
187     }
188     if (params.length == 2)
189     {
190       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc(Messages
191           .getString("DefaultConvention.substringStringAsFirst"));
192       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc(Messages
193           .getString("DefaultConvention.substringTakesAnIntAsSecond"));
194       return self.toString().substring(((Integer JavaDoc) params[0]).intValue(), ((Integer JavaDoc) params[1]).intValue());
195     }
196     throw new Exception JavaDoc(Messages.getString("DefaultConvention.substringTakes1or2Params"));
197   }
198
199   Object JavaDoc toLowerCase(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
200   {
201     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.toLowerCaseTakesNoParams"));
202     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.toLowerCaseOnStrings"));
203     return self.toString().toLowerCase();
204   }
205
206   Object JavaDoc toUpperCase(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
207   {
208     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.toUpperCaseTakesNoParams"));
209     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.toUpperCaseOnStrings"));
210     return self.toString().toUpperCase();
211   }
212
213   Object JavaDoc trim(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
214   {
215     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.trimTakesNoParams"));
216     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.trimOnlyStrings"));
217     return self.toString().trim();
218   }
219
220   public final static String JavaDoc initCap(String JavaDoc src)
221   {
222     if ((src == null) || (src.length() == 0)) return src;
223
224     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(src);
225
226     sb.setCharAt(0, sb.substring(0, 1).toUpperCase().charAt(0));
227
228     for (int i = 0; i < sb.length(); i++)
229     {
230       if (i > 0)
231       {
232         if ((sb.charAt(i - 1) == ' ') || (sb.charAt(i - 1) == '_'))
233         {
234           sb.setCharAt(i, sb.substring(i, i + 1).toUpperCase().charAt(0));
235         }
236       }
237     }
238     return new String JavaDoc(sb);
239   }
240
241   Object JavaDoc initcap(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
242   {
243     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.initcapTakesNoParams"));
244     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc(Messages.getString("DefaultConvention.initcapOnlyOnStrings"));
245     return initCap(self.toString());
246   }
247
248   Object JavaDoc getClassName(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
249   {
250     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.GetClassNameNoParams"));
251     if (self instanceof DjObject)
252     {
253       DjObject obj = (DjObject) self;
254       return obj.getExtent().getObjectType();
255     }
256     String JavaDoc result = self.getClass().getName();
257     if (result.indexOf(".") == -1)
258     {
259       int idx = result.lastIndexOf(".");
260       if (idx != -1)
261       {
262         result = result.substring(idx + 1);
263       }
264     }
265
266     return result;
267   }
268
269   Object JavaDoc isNegative(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
270   {
271     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.isNegativeTakesNoParams"));
272     if (self instanceof Integer JavaDoc) return new Boolean JavaDoc(((Integer JavaDoc) self).intValue() < 0);
273     if (self instanceof Long JavaDoc) return new Boolean JavaDoc(((Long JavaDoc) self).longValue() < 0);
274     if (self instanceof Float JavaDoc) return new Boolean JavaDoc(((Float JavaDoc) self).floatValue() < 0);
275
276     throw new Exception JavaDoc(Messages.getString("DefaultConvention.isNegativeOnlyOnNumbers"));
277   }
278
279   Object JavaDoc isPositive(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
280   {
281     if (params.length != 0) throw new Exception JavaDoc(Messages.getString("DefaultConvention.isPositiveTakesNoParams"));
282     if (self instanceof Integer JavaDoc) return new Boolean JavaDoc(((Integer JavaDoc) self).intValue() >= 0);
283     if (self instanceof Long JavaDoc) return new Boolean JavaDoc(((Long JavaDoc) self).longValue() >= 0);
284     if (self instanceof Float JavaDoc) return new Boolean JavaDoc(((Float JavaDoc) self).floatValue() >= 0);
285
286     throw new Exception JavaDoc(Messages.getString("DefaultConvention.isPositiveOnlyOnNumbers"));
287   }
288
289   Object JavaDoc replace(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
290   {
291     if (params.length != 2) throw new Exception JavaDoc(Messages.getString("DefaultConvention.replaceTakesNoParams"));
292     DjStringReplacer sr = new DjStringReplacer(self.toString());
293     return sr.replace(params[0].toString(), params[1].toString());
294
295   }
296 }
Popular Tags