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.ejb.cfg;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.util.CharBuffer;
033: import com.caucho.util.L10N;
034:
035: import java.util.ArrayList;
036:
037: /**
038: * A builtin SQL function expression
039: */
040: public class FunctionSignature {
041: static L10N L = new L10N(FunctionSignature.class);
042:
043: private String _signature;
044: // function name
045: private String _name;
046: // arguments
047: private Class[] _parameterTypes;
048: // return type
049: private Class _returnType;
050:
051: private int _index;
052:
053: private String _sql;
054:
055: /**
056: * Creates a function definition.
057: *
058: * @param signature the function signature in java syntax
059: */
060: public FunctionSignature(String signature) throws ConfigException {
061: _signature = signature;
062:
063: parseSignature();
064: }
065:
066: /**
067: * Returns the function name.
068: */
069: public String getName() {
070: return _name;
071: }
072:
073: /**
074: * Returns the function signature.
075: */
076: public String getSignature() {
077: return _signature;
078: }
079:
080: /**
081: * Returns the function arguments.
082: */
083: public Class[] getParameterTypes() {
084: return _parameterTypes;
085: }
086:
087: /**
088: * Returns the return type;
089: */
090: public Class getReturnType() {
091: return _returnType;
092: }
093:
094: /**
095: * Sets the SQL.
096: */
097: public void setSQL(String sql) {
098: _sql = sql;
099: }
100:
101: /**
102: * Gets the SQL.
103: */
104: public String getSQL() {
105: return _sql;
106: }
107:
108: /**
109: * Parses the function signature.
110: */
111: private void parseSignature() throws ConfigException {
112: _index = 0;
113:
114: _returnType = parseType(skipWhitespace(read()));
115:
116: CharBuffer cb = CharBuffer.allocate();
117: int ch = skipWhitespace(read());
118:
119: for (; Character.isJavaIdentifierPart((char) ch); ch = read())
120: cb.append((char) ch);
121:
122: if (cb.length() == 0)
123: throw new ConfigException(L.l(
124: "unexpected empty function name in '{0}'",
125: _signature));
126:
127: _name = cb.toString();
128:
129: ch = skipWhitespace(ch);
130:
131: if (ch != '(')
132: throw new ConfigException(
133: L
134: .l(
135: "function syntax is 'ret-type name(arg1, ..., argn)' in '{0}'",
136: _signature));
137:
138: ArrayList<Class> argList = new ArrayList<Class>();
139:
140: ch = read();
141: while (Character
142: .isJavaIdentifierStart((char) (ch = skipWhitespace(ch)))) {
143: Class type = parseType(ch);
144:
145: argList.add(type);
146:
147: ch = skipWhitespace(read());
148:
149: if (ch == ',')
150: ch = read();
151: }
152:
153: _parameterTypes = argList.toArray(new Class[argList.size()]);
154:
155: if (ch != ')')
156: throw new ConfigException(
157: L
158: .l(
159: "function syntax is 'ret-type name(arg1, ..., argn)' in '{0}'",
160: _signature));
161:
162: ch = skipWhitespace(read());
163:
164: if (ch != -1)
165: throw new ConfigException(
166: L
167: .l(
168: "function syntax is 'ret-type name(arg1, ..., argn)' in '{0}'",
169: _signature));
170: }
171:
172: /**
173: * Parses the type.
174: */
175: private Class parseType(int ch) throws ConfigException {
176: CharBuffer cb = CharBuffer.allocate();
177:
178: for (; Character.isJavaIdentifierPart((char) ch); ch = read())
179: cb.append((char) ch);
180:
181: if (cb.length() == 0)
182: throw new ConfigException(L.l(
183: "unexpected empty type in '{0}'", _signature));
184:
185: String className = cb.toString();
186:
187: unread(ch);
188:
189: return findClass(className);
190: }
191:
192: /**
193: * Converts the type to a classname.
194: */
195: private Class findClass(String className) throws ConfigException {
196: if ("int".equals(className))
197: return int.class;
198: else if ("boolean".equals(className))
199: return boolean.class;
200: else if ("double".equals(className))
201: return double.class;
202: else if ("String".equals(className))
203: return String.class;
204: else if ("Date".equals(className))
205: return java.util.Date.class;
206: else if ("any".equals(className))
207: return Object.class;
208:
209: throw new ConfigException(L.l("unknown type '{0}' in '{1}'",
210: className, _signature));
211: }
212:
213: /**
214: * Skips whitespace to get to the next valid value.
215: */
216: private int skipWhitespace(int ch) {
217: for (; Character.isWhitespace((char) ch); ch = read()) {
218: }
219:
220: return ch;
221: }
222:
223: /**
224: * Reads the next character.
225: */
226: private int read() {
227: if (_index < _signature.length())
228: return _signature.charAt(_index++);
229: else
230: return -1;
231: }
232:
233: /**
234: * Unreads the last character.
235: */
236: private void unread(int ch) {
237: if (ch >= 0)
238: _index--;
239: }
240:
241: /**
242: * Returns a hash-code.
243: */
244: public int hashCode() {
245: return _name.hashCode();
246: }
247:
248: /**
249: * True if the function signatures are equal.
250: */
251: public boolean equals(Object o) {
252: if (!(o instanceof FunctionSignature))
253: return false;
254:
255: FunctionSignature sig = (FunctionSignature) o;
256:
257: if (!_name.equalsIgnoreCase(sig._name))
258: return false;
259:
260: if (_parameterTypes.length != sig._parameterTypes.length)
261: return false;
262:
263: for (int i = 0; i < _parameterTypes.length; i++)
264: if (!_parameterTypes[i].equals(sig._parameterTypes[i]))
265: return false;
266:
267: return true;
268: }
269:
270: /**
271: * Returns a string value.
272: */
273: public String toString() {
274: return "Function[" + _signature + "]";
275: }
276: }
|