KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > core > util > DefaultConvention


1 package com.genimen.djeneric.tools.generator.core.util;
2
3 import com.genimen.djeneric.repository.DjObject;
4 import com.genimen.djeneric.util.DjStringReplacer;
5
6 public class DefaultConvention implements Convention
7 {
8
9   public DefaultConvention()
10   {
11   }
12
13   public Object JavaDoc staticCall(String JavaDoc function, Object JavaDoc[] params, ParseContext context) throws Exception JavaDoc
14   {
15     if (function.equals("switch")) return doSwitch(params);
16
17     throw new Exception JavaDoc("Static function " + function + " not defined");
18   }
19
20   public Object JavaDoc call(Object JavaDoc self, String JavaDoc function, Object JavaDoc[] params, ParseContext context) throws Exception JavaDoc
21   {
22     if (function.equals("indexOf")) return indexOf(self, params);
23     if (function.equals("lastIndexOf")) return lastIndexOf(self, params);
24     if (function.equals("length")) return length(self, params);
25     if (function.equals("startsWith")) return startsWith(self, params);
26     if (function.equals("substring")) return substring(self, params);
27     if (function.equals("toLowerCase")) return toLowerCase(self, params);
28     if (function.equals("toUpperCase")) return toUpperCase(self, params);
29     if (function.equals("trim")) return trim(self, params);
30     if (function.equals("initcap")) return initcap(self, params);
31     if (function.equals("getClassName")) return getClassName(self, params);
32     if (function.equals("isNegative")) return isNegative(self, params);
33     if (function.equals("isPositive")) return isPositive(self, params);
34     if (function.equals("replace")) return replace(self, params);
35     if (function.equals("getIndented")) return getIndented(self, params);
36
37     throw new Exception JavaDoc("Function " + function + " not defined");
38   }
39
40   // Static functions
41
Object JavaDoc doSwitch(Object JavaDoc[] params) throws Exception JavaDoc
42   {
43     if (params.length < 3) throw new Exception JavaDoc("switch expects at least 3 parameters (value, compare, result)");
44     Object JavaDoc value = params[0];
45     for (int i = 1; i < params.length - 1; i += 2)
46     {
47       if (value.equals(params[i])) return params[i + 1];
48     }
49     if ((params.length % 2) != 0) throw new Exception JavaDoc("switch failed; value \"" + value
50                                                       + "\" not handled; you might want to use a default");
51     return params[params.length - 1];
52   }
53
54   // Normal functions
55

56   Object JavaDoc indexOf(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
57   {
58     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("indexOf only operates on strings");
59
60     if (params.length == 1)
61     {
62       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("indexOf takes a String as parameter");
63       return new Integer JavaDoc(self.toString().indexOf(params[0].toString()));
64     }
65     if (params.length == 2)
66     {
67       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("indexOf takes a String as first parameter");
68       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc("indexOf takes an integer as second parameter");
69       return new Integer JavaDoc(self.toString().indexOf(params[0].toString(), ((Integer JavaDoc) params[1]).intValue()));
70     }
71     throw new Exception JavaDoc("indexOf takes 1 or 2 parameters");
72   }
73
74   Object JavaDoc lastIndexOf(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
75   {
76     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("lastIndexOf only operates on strings");
77
78     if (params.length == 1)
79     {
80       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("lastIndexOf takes a String as parameter");
81       return new Integer JavaDoc(self.toString().lastIndexOf(params[0].toString()));
82     }
83     if (params.length == 2)
84     {
85       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("lastIndexOf takes a String as first parameter");
86       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc("lastIndexOf takes an integer as second parameter");
87       return new Integer JavaDoc(self.toString().lastIndexOf(params[0].toString(), ((Integer JavaDoc) params[1]).intValue()));
88     }
89     throw new Exception JavaDoc("lastIndexOf takes 1 or 2 parameters");
90   }
91
92   Object JavaDoc length(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
93   {
94     if (params.length != 0) throw new Exception JavaDoc("length takes no parameters");
95     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("length only operates on strings");
96     return new Integer JavaDoc(self.toString().length());
97   }
98
99   Object JavaDoc startsWith(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
100   {
101     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("startsWith only operates on strings");
102
103     if (params.length == 1)
104     {
105       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("startsWith takes a String as parameter");
106       return new Boolean JavaDoc(self.toString().startsWith(params[0].toString()));
107     }
108     if (params.length == 2)
109     {
110       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("startsWith takes a String as first parameter");
111       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc("startsWith takes an integer as second parameter");
112       return new Boolean JavaDoc(self.toString().startsWith(params[0].toString(), ((Integer JavaDoc) params[1]).intValue()));
113     }
114     throw new Exception JavaDoc("startsWith takes 1 or 2 parameters");
115   }
116
117   Object JavaDoc substring(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
118   {
119     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("substring only operates on strings");
120
121     if (params.length == 1)
122     {
123       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("substring takes a String as parameter");
124       return self.toString().substring(((Integer JavaDoc) params[0]).intValue());
125     }
126     if (params.length == 2)
127     {
128       if (!(params[0] instanceof String JavaDoc)) throw new Exception JavaDoc("substring takes a String as first parameter");
129       if (!(params[1] instanceof Integer JavaDoc)) throw new Exception JavaDoc("substring takes an integer as second parameter");
130       return self.toString().substring(((Integer JavaDoc) params[0]).intValue(), ((Integer JavaDoc) params[1]).intValue());
131     }
132     throw new Exception JavaDoc("substring takes 1 or 2 parameters");
133   }
134
135   Object JavaDoc toLowerCase(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
136   {
137     if (params.length != 0) throw new Exception JavaDoc("toLowerCase takes no parameters");
138     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("toLowerCase only operates on strings");
139     return self.toString().toLowerCase();
140   }
141
142   Object JavaDoc toUpperCase(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
143   {
144     if (params.length != 0) throw new Exception JavaDoc("toUpperCase takes no parameters");
145     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("toUpperCase only operates on strings");
146     return self.toString().toUpperCase();
147   }
148
149   Object JavaDoc trim(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
150   {
151     if (params.length != 0) throw new Exception JavaDoc("trim takes no parameters");
152     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("trim only operates on strings");
153     return self.toString().trim();
154   }
155
156   public final static String JavaDoc initCap(String JavaDoc src)
157   {
158     if ((src == null) || (src.length() == 0)) return src;
159
160     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(src);
161
162     sb.setCharAt(0, sb.substring(0, 1).toUpperCase().charAt(0));
163
164     for (int i = 0; i < sb.length(); i++)
165     {
166       if (i > 0)
167       {
168         if ((sb.charAt(i - 1) == ' ') || (sb.charAt(i - 1) == '_'))
169         {
170           sb.setCharAt(i, sb.substring(i, i + 1).toUpperCase().charAt(0));
171         }
172       }
173     }
174     return new String JavaDoc(sb);
175   }
176
177   Object JavaDoc initcap(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
178   {
179     if (params.length != 0) throw new Exception JavaDoc("initcap takes no parameters");
180     if (!(self instanceof String JavaDoc)) throw new Exception JavaDoc("initcap only operates on strings");
181     return initCap(self.toString());
182   }
183
184   Object JavaDoc getClassName(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
185   {
186     if (params.length != 0) throw new Exception JavaDoc("getClassName takes no parameters");
187     if (self instanceof DjObject)
188     {
189       DjObject obj = (DjObject) self;
190       return obj.getExtent().getObjectType();
191     }
192     String JavaDoc result = self.getClass().getName();
193     if (result.indexOf(".") == -1)
194     {
195       int idx = result.lastIndexOf(".");
196       if (idx != -1)
197       {
198         result = result.substring(idx + 1);
199       }
200     }
201
202     return result;
203   }
204
205   Object JavaDoc isNegative(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
206   {
207     if (params.length != 0) throw new Exception JavaDoc("isNegative takes no parameters");
208     if (self instanceof Integer JavaDoc) return new Boolean JavaDoc(((Integer JavaDoc) self).intValue() < 0);
209     if (self instanceof Long JavaDoc) return new Boolean JavaDoc(((Long JavaDoc) self).longValue() < 0);
210     if (self instanceof Float JavaDoc) return new Boolean JavaDoc(((Float JavaDoc) self).floatValue() < 0);
211
212     throw new Exception JavaDoc("isNegative only operates on numbers");
213   }
214
215   Object JavaDoc isPositive(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
216   {
217     if (params.length != 0) throw new Exception JavaDoc("isPositive takes no parameters");
218     if (self instanceof Integer JavaDoc) return new Boolean JavaDoc(((Integer JavaDoc) self).intValue() >= 0);
219     if (self instanceof Long JavaDoc) return new Boolean JavaDoc(((Long JavaDoc) self).longValue() >= 0);
220     if (self instanceof Float JavaDoc) return new Boolean JavaDoc(((Float JavaDoc) self).floatValue() >= 0);
221
222     throw new Exception JavaDoc("isPositive only operates on numbers");
223   }
224
225   Object JavaDoc replace(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
226   {
227     if (params.length != 2) throw new Exception JavaDoc("replace takes 2 parameters");
228     DjStringReplacer sr = new DjStringReplacer(self.toString());
229     return sr.replace(params[0].toString(), params[1].toString());
230
231   }
232
233   Object JavaDoc getIndented(Object JavaDoc self, Object JavaDoc[] params) throws Exception JavaDoc
234   {
235     if (params.length != 1) throw new Exception JavaDoc("getIndented takes 1 parameter");
236     if (!(params[0] instanceof Integer JavaDoc)) throw new Exception JavaDoc("getIndented takes an integer as parameter");
237
238     int ind = ((Integer JavaDoc) params[0]).intValue();
239     if (ind == 0 || self == null) return self;
240
241     String JavaDoc val = self.toString();
242
243     StringBuffer JavaDoc result = new StringBuffer JavaDoc(val);
244
245     if (ind > 0)
246     {
247       for (int x = 0; x < ind; x++)
248         result.insert(0, ' ');
249       int i = 0;
250       while (i < result.length())
251       {
252         if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n')
253         {
254           for (int x = 0; x < ind; x++)
255             result.insert(i + 1, ' ');
256           i += ind;
257         }
258         i++;
259       }
260     }
261     if (ind < 0)
262     {
263       ind = -ind;
264       for (int d = 0; d < ind; d++)
265         if (result.charAt(0) == ' ') result.deleteCharAt(0);
266       int i = 0;
267       while (i < result.length())
268       {
269         if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n')
270         {
271           int end = i + ind;
272           if (end > result.length()) end = result.length();
273           for (int d = i; d < end; d++)
274             if (result.charAt(i + 1) == ' ') result.deleteCharAt(i + 1);
275         }
276         i++;
277       }
278     }
279     return result.toString();
280   }
281 }
Popular Tags