KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > oql > core > nodes > SubExpressionNode


1 package com.genimen.djeneric.repository.oql.core.nodes;
2
3 import java.math.BigDecimal JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import com.genimen.djeneric.repository.DjObject;
7 import com.genimen.djeneric.repository.exceptions.DjenericException;
8 import com.genimen.djeneric.repository.oql.core.DjOqlParserEngine;
9 import com.genimen.djeneric.repository.oql.core.MatchException;
10 import com.genimen.djeneric.repository.oql.core.ParseException;
11 import com.genimen.djeneric.repository.oql.core.SimpleNode;
12 import com.genimen.djeneric.util.DjLikeComparator;
13
14 public class SubExpressionNode extends SimpleNode implements BooleanExpression
15 {
16   public static final String JavaDoc UNARY = "unary";
17   boolean _negated = false;
18   String JavaDoc _operator = UNARY;
19
20   public SubExpressionNode(int i)
21   {
22     super(i);
23   }
24
25   public SubExpressionNode(DjOqlParserEngine p, int i)
26   {
27     super(p, i);
28   }
29
30   public String JavaDoc getName()
31   {
32     return toString();
33   }
34
35   public void setOperator(String JavaDoc op)
36   {
37     op = op.trim();
38     if ("=".equals(op)) op = "==";
39     if ("<>".equals(op)) op = "!=";
40
41     _operator = op;
42   }
43
44   public String JavaDoc getOperator()
45   {
46     return _operator;
47   }
48
49   public String JavaDoc toString()
50   {
51     return getOperator() + " (sub)";
52   }
53
54   public boolean isUnary()
55   {
56     return getOperator().equals(UNARY);
57   }
58
59   public void translate(StringBuffer JavaDoc result, HashMap JavaDoc path2AliasMapping) throws ParseException
60   {
61     appendOpenBrackets(result);
62     getChild(0).translate(result, path2AliasMapping);
63
64     if (_operator.equals("==")) result.append(" = ");
65     else if (_operator.equals("!=")) result.append(" <> ");
66     else result.append(" " + _operator + " ");
67
68     // support the 'is not' operator
69
if (isNegated()) result.append("not ");
70
71     getChild(1).translate(result, path2AliasMapping);
72     appendCloseBrackets(result);
73   }
74
75   public boolean isNegated()
76   {
77     return _negated;
78   }
79
80   public void setNegated(boolean b)
81   {
82     _negated = b;
83   }
84
85   public Object JavaDoc getValue(MatchingContext context) throws MatchException
86   {
87     return new Boolean JavaDoc(isTrue(context));
88   }
89
90   public boolean isTrue(MatchingContext context) throws MatchException
91   {
92     if (isUnary())
93     {
94       if (getChild(0) instanceof BooleanExpression)
95       {
96         return ((BooleanExpression) getChild(0)).isTrue(context);
97       }
98       throw new MatchException("Unsupported unary operation for non boolean expression", getChild(0).beginLine,
99           getChild(0).beginColumn);
100     }
101
102     // Boolean comparison?
103
if ((getChild(0) instanceof BooleanExpression) && (getChild(1) instanceof BooleanExpression))
104     {
105       BooleanExpression left = (BooleanExpression) getChild(0);
106       BooleanExpression right = (BooleanExpression) getChild(1);
107       if (getOperator().equals("=="))
108       {
109         return left.isTrue(context) == right.isTrue(context);
110       }
111       if (getOperator().equals("!="))
112       {
113         return left.isTrue(context) != right.isTrue(context);
114       }
115       throw new MatchException("Can only perform == and != to booleans", beginLine, beginColumn);
116     }
117
118     if ((getChild(0) instanceof ValueExpression) && (getChild(1) instanceof ValueExpression))
119     {
120       ValueExpression left = (ValueExpression) getChild(0);
121       ValueExpression right = (ValueExpression) getChild(1);
122       Object JavaDoc leftVal = left.getValue(context);
123       Object JavaDoc rightVal = right.getValue(context);
124
125       // first handle is null and is notnull clauses
126
if (getOperator().equals("is"))
127       {
128         if (isNegated()) return leftVal != null;
129         else return leftVal == null;
130       }
131
132       // then handle null stuff
133
if (leftVal == null || rightVal == null)
134       {
135         if (getOperator().equals("==")) return leftVal == rightVal;
136         if (getOperator().equals("!=")) return leftVal != rightVal;
137         return false;
138       }
139
140       // Object comparison?
141
if ((leftVal instanceof DjObject) || (rightVal instanceof DjObject))
142       {
143         long l, r;
144
145         try
146         {
147           if (leftVal instanceof DjObject) l = ((DjObject) leftVal).getObjectId();
148           else l = Long.parseLong(leftVal.toString());
149
150           if (rightVal instanceof DjObject) r = ((DjObject) rightVal).getObjectId();
151           else r = Long.parseLong(rightVal.toString());
152         }
153         catch (DjenericException e)
154         {
155           throw new MatchException(e, beginLine, beginColumn);
156         }
157
158         if (getOperator().equals("=="))
159         {
160           return l == r;
161         }
162         if (getOperator().equals("!="))
163         {
164           return l != r;
165         }
166         throw new MatchException("Can only perform == and != to Model Objects", beginLine, beginColumn);
167       }
168
169       // String comparison?
170
if ((leftVal instanceof String JavaDoc) || (rightVal instanceof String JavaDoc))
171       {
172         if (getOperator().equals("=="))
173         {
174           return leftVal.toString().equals(rightVal.toString());
175         }
176         if (getOperator().equals("!="))
177         {
178           return !leftVal.toString().equals(rightVal.toString());
179         }
180         if (getOperator().equals(">="))
181         {
182           return leftVal.toString().compareTo(rightVal.toString()) >= 0;
183         }
184         if (getOperator().equals("<="))
185         {
186           return leftVal.toString().compareTo(rightVal.toString()) <= 0;
187         }
188         if (getOperator().equals("<"))
189         {
190           return leftVal.toString().compareTo(rightVal.toString()) < 0;
191         }
192         if (getOperator().equals(">"))
193         {
194           return leftVal.toString().compareTo(rightVal.toString()) > 0;
195         }
196
197         if (getOperator().equals("like"))
198         {
199           DjLikeComparator like = new DjLikeComparator(rightVal.toString(), '\\', false);
200           return like.compare(leftVal.toString());
201         }
202       }
203
204       // Float comparison?
205
if ((leftVal instanceof Float JavaDoc) || (rightVal instanceof Float JavaDoc))
206       {
207         Float JavaDoc l = toFloat(leftVal);
208         Float JavaDoc r = toFloat(rightVal);
209
210         if (getOperator().equals("=="))
211         {
212           return l.equals(r);
213         }
214         if (getOperator().equals("!="))
215         {
216           return !l.equals(r);
217         }
218         if (getOperator().equals(">="))
219         {
220           return l.floatValue() >= r.floatValue();
221         }
222         if (getOperator().equals("<="))
223         {
224           return l.floatValue() <= r.floatValue();
225         }
226         if (getOperator().equals("<"))
227         {
228           return l.floatValue() < r.floatValue();
229         }
230         if (getOperator().equals(">"))
231         {
232           return l.floatValue() > r.floatValue();
233         }
234       }
235
236       // BigDecimal comparison?
237
if ((leftVal instanceof BigDecimal JavaDoc) || (rightVal instanceof BigDecimal JavaDoc))
238       {
239         BigDecimal JavaDoc l = toBigDecimal(leftVal);
240         BigDecimal JavaDoc r = toBigDecimal(rightVal);
241
242         if (getOperator().equals("=="))
243         {
244           return l.equals(r);
245         }
246         if (getOperator().equals("!="))
247         {
248           return !l.equals(r);
249         }
250         if (getOperator().equals(">="))
251         {
252           return l.floatValue() >= r.floatValue();
253         }
254         if (getOperator().equals("<="))
255         {
256           return l.floatValue() <= r.floatValue();
257         }
258         if (getOperator().equals("<"))
259         {
260           return l.floatValue() < r.floatValue();
261         }
262         if (getOperator().equals(">"))
263         {
264           return l.floatValue() > r.floatValue();
265         }
266       }
267
268       // Integer comparison?
269
if ((leftVal instanceof Long JavaDoc) || (rightVal instanceof Long JavaDoc))
270       {
271         Long JavaDoc l = toLong(leftVal);
272         Long JavaDoc r = toLong(rightVal);
273
274         if (getOperator().equals("=="))
275         {
276           return l.equals(r);
277         }
278         if (getOperator().equals("!="))
279         {
280           return !l.equals(r);
281         }
282         if (getOperator().equals(">="))
283         {
284           return l.floatValue() >= r.floatValue();
285         }
286         if (getOperator().equals("<="))
287         {
288           return l.floatValue() <= r.floatValue();
289         }
290         if (getOperator().equals("<"))
291         {
292           return l.floatValue() < r.floatValue();
293         }
294         if (getOperator().equals(">"))
295         {
296           return l.floatValue() > r.floatValue();
297         }
298       }
299
300       // Boolean comparison?
301
if ((leftVal instanceof Boolean JavaDoc) && (rightVal instanceof Boolean JavaDoc))
302       {
303         boolean l = ((Boolean JavaDoc) leftVal).booleanValue();
304         boolean r = ((Boolean JavaDoc) rightVal).booleanValue();
305
306         if (getOperator().equals("=="))
307         {
308           return l == r;
309         }
310         if (getOperator().equals("!="))
311         {
312           return l != r;
313         }
314         throw new MatchException("Can only perform == and != to booleans", beginLine, beginColumn);
315       }
316
317       throw new MatchException("Can not perform " + classOnly(leftVal) + getOperator() + classOnly(leftVal), beginLine,
318           beginColumn);
319     }
320
321     throw new MatchException("Can not perform " + getOperator() + " on this type of expression", beginLine, beginColumn);
322
323   }
324
325   public boolean match(MatchingContext context) throws MatchException
326   {
327     return isTrue(context);
328   }
329
330 }
Popular Tags