01: package net.sourceforge.squirrel_sql.plugins.hibernate.completion;
02:
03: import net.sourceforge.squirrel_sql.plugins.hibernate.mapping.MappedClassInfo;
04:
05: import java.util.ArrayList;
06:
07: public class HqlAliasParser {
08: private StringBuffer _token = new StringBuffer();
09:
10: public ArrayList<AliasInfo> parse(String hql,
11: MappingInfoProvider mappingInfoProvider) {
12: ArrayList<AliasInfo> ret = new ArrayList<AliasInfo>();
13:
14: int[] i = new int[1];
15:
16: MappedClassInfo lastMappedClass = null;
17:
18: while (i[0] < hql.length()) {
19: String token = nextToken(i, hql);
20:
21: if ("as".equals(token)) {
22: continue;
23: }
24:
25: if (mappingInfoProvider.mayBeClassOrAliasName(token)) {
26: if (null != lastMappedClass) {
27: ret.add(new AliasInfo(lastMappedClass, token));
28: lastMappedClass = null;
29: } else {
30: lastMappedClass = mappingInfoProvider
31: .getMappedClassInfoFor(token);
32: }
33: } else {
34: lastMappedClass = null;
35: }
36: }
37:
38: return ret;
39: }
40:
41: private String nextToken(int[] i, String hql) {
42: _token.setLength(0);
43: for (int j = i[0]; j < hql.length(); j++) {
44: char c = hql.charAt(j);
45: if (Character.isWhitespace(c)) {
46: if (0 == _token.length()) {
47: continue;
48: } else {
49: i[0] = j + 1;
50: return _token.toString();
51: }
52: }
53:
54: if (isSepartor(c)) {
55: if (0 == _token.length()) {
56: i[0] = j + 1;
57: return _token.append(c).toString();
58: } else {
59: i[0] = j;
60: return _token.toString();
61: }
62: }
63:
64: _token.append(c);
65:
66: }
67:
68: i[0] = hql.length();
69: return _token.toString();
70:
71: }
72:
73: private boolean isSepartor(char c) {
74: return ',' == c || '(' == c || ')' == c;
75: }
76:
77: }
|