KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > FunctionSignature


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.cfg;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.util.L10N;
34
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  * A builtin SQL function expression
39  */

40 public class FunctionSignature {
41   static L10N L = new L10N(FunctionSignature.class);
42   
43   private String JavaDoc _signature;
44   // function name
45
private String JavaDoc _name;
46   // arguments
47
private Class JavaDoc []_parameterTypes;
48   // return type
49
private Class JavaDoc _returnType;
50
51   private int _index;
52
53   private String JavaDoc _sql;
54
55   /**
56    * Creates a function definition.
57    *
58    * @param signature the function signature in java syntax
59    */

60   public FunctionSignature(String JavaDoc signature)
61     throws ConfigException
62   {
63     _signature = signature;
64
65     parseSignature();
66   }
67
68   /**
69    * Returns the function name.
70    */

71   public String JavaDoc getName()
72   {
73     return _name;
74   }
75
76   /**
77    * Returns the function signature.
78    */

79   public String JavaDoc getSignature()
80   {
81     return _signature;
82   }
83
84   /**
85    * Returns the function arguments.
86    */

87   public Class JavaDoc []getParameterTypes()
88   {
89     return _parameterTypes;
90   }
91
92   /**
93    * Returns the return type;
94    */

95   public Class JavaDoc getReturnType()
96   {
97     return _returnType;
98   }
99
100   /**
101    * Sets the SQL.
102    */

103   public void setSQL(String JavaDoc sql)
104   {
105     _sql = sql;
106   }
107
108   /**
109    * Gets the SQL.
110    */

111   public String JavaDoc getSQL()
112   {
113     return _sql;
114   }
115
116   /**
117    * Parses the function signature.
118    */

119   private void parseSignature()
120     throws ConfigException
121   {
122     _index = 0;
123
124     _returnType = parseType(skipWhitespace(read()));
125
126     CharBuffer cb = CharBuffer.allocate();
127     int ch = skipWhitespace(read());
128
129     for (; Character.isJavaIdentifierPart((char) ch); ch = read())
130       cb.append((char) ch);
131
132     if (cb.length() == 0)
133       throw new ConfigException(L.l("unexpected empty function name in `{0}'",
134                                         _signature));
135
136     _name = cb.toString();
137
138     ch = skipWhitespace(ch);
139
140     if (ch != '(')
141       throw new ConfigException(L.l("function syntax is `ret-type name(arg1, ..., argn)' in `{0}'",
142                                         _signature));
143
144     ArrayList JavaDoc<Class JavaDoc> argList = new ArrayList JavaDoc<Class JavaDoc>();
145     
146     ch = read();
147     while (Character.isJavaIdentifierStart((char) (ch = skipWhitespace(ch)))) {
148       Class JavaDoc type = parseType(ch);
149
150       argList.add(type);
151
152       ch = skipWhitespace(read());
153
154       if (ch == ',')
155         ch = read();
156     }
157
158     _parameterTypes = argList.toArray(new Class JavaDoc[argList.size()]);
159
160     if (ch != ')')
161       throw new ConfigException(L.l("function syntax is `ret-type name(arg1, ..., argn)' in `{0}'",
162                                         _signature));
163
164     ch = skipWhitespace(read());
165
166     if (ch != -1)
167       throw new ConfigException(L.l("function syntax is `ret-type name(arg1, ..., argn)' in `{0}'",
168                                         _signature));
169   }
170
171   /**
172    * Parses the type.
173    */

174   private Class JavaDoc parseType(int ch)
175     throws ConfigException
176   {
177     CharBuffer cb = CharBuffer.allocate();
178
179     for (; Character.isJavaIdentifierPart((char) ch); ch = read())
180       cb.append((char) ch);
181
182     if (cb.length() == 0)
183       throw new ConfigException(L.l("unexpected empty type in `{0}'",
184                                         _signature));
185
186     String JavaDoc className = cb.toString();
187
188     unread(ch);
189
190     return findClass(className);
191   }
192
193   /**
194    * Converts the type to a classname.
195    */

196   private Class JavaDoc findClass(String JavaDoc className)
197     throws ConfigException
198   {
199     if ("int".equals(className))
200       return int.class;
201     else if ("boolean".equals(className))
202       return boolean.class;
203     else if ("double".equals(className))
204       return double.class;
205     else if ("String".equals(className))
206       return String JavaDoc.class;
207     else if ("Date".equals(className))
208       return java.util.Date JavaDoc.class;
209     else if ("any".equals(className))
210       return Object JavaDoc.class;
211
212     throw new ConfigException(L.l("unknown type `{0}' in `{1}'",
213                                       className, _signature));
214   }
215
216   /**
217    * Skips whitespace to get to the next valid value.
218    */

219   private int skipWhitespace(int ch)
220   {
221     for (; Character.isWhitespace((char) ch); ch = read()) {
222     }
223
224     return ch;
225   }
226
227   /**
228    * Reads the next character.
229    */

230   private int read()
231   {
232     if (_index < _signature.length())
233       return _signature.charAt(_index++);
234     else
235       return -1;
236   }
237
238   /**
239    * Unreads the last character.
240    */

241   private void unread(int ch)
242   {
243     if (ch >= 0)
244       _index--;
245   }
246
247   /**
248    * Returns a hash-code.
249    */

250   public int hashCode()
251   {
252     return _name.hashCode();
253   }
254
255   /**
256    * True if the function signatures are equal.
257    */

258   public boolean equals(Object JavaDoc o)
259   {
260     if (! (o instanceof FunctionSignature))
261       return false;
262
263     FunctionSignature sig = (FunctionSignature) o;
264
265     if (! _name.equalsIgnoreCase(sig._name))
266       return false;
267
268     if (_parameterTypes.length != sig._parameterTypes.length)
269       return false;
270
271     for (int i = 0; i < _parameterTypes.length; i++)
272       if (! _parameterTypes[i].equals(sig._parameterTypes[i]))
273         return false;
274
275     return true;
276   }
277
278   /**
279    * Returns a string value.
280    */

281   public String JavaDoc toString()
282   {
283     return "Function[" + _signature + "]";
284   }
285 }
286
Popular Tags