KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > core > nodes > SubExpressionNode


1 package com.genimen.djeneric.tools.generator.core.nodes;
2
3 import com.genimen.djeneric.repository.DjObject;
4 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
5 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine;
6 import com.genimen.djeneric.tools.generator.core.ParseException;
7 import com.genimen.djeneric.tools.generator.core.SimpleNode;
8 import com.genimen.djeneric.tools.generator.core.util.ParseContext;
9
10 public class SubExpressionNode extends SimpleNode implements BooleanExpression
11 {
12   public static final String JavaDoc UNARY = "unary";
13
14   String JavaDoc _operator = UNARY;
15
16   public SubExpressionNode(int i)
17   {
18     super(i);
19   }
20
21   public SubExpressionNode(DjentelParserEngine p, int i)
22   {
23     super(p, i);
24   }
25
26   public String JavaDoc getName()
27   {
28     return toString();
29   }
30
31   public void setOperator(String JavaDoc op)
32   {
33     _operator = op;
34   }
35
36   public String JavaDoc getOperator()
37   {
38     return _operator;
39   }
40
41   public String JavaDoc toString()
42   {
43     return getOperator() + " (sub)";
44   }
45
46   public boolean isUnary()
47   {
48     return getOperator().equals(UNARY);
49   }
50
51   public Object JavaDoc getValue(ParseContext context) throws ParseException
52   {
53     return new Boolean JavaDoc(isTrue(context));
54   }
55
56   public boolean isTrue(ParseContext context) throws ParseException
57   {
58     if (isUnary())
59     {
60       if (getChild(0) instanceof BooleanExpression)
61       {
62         return ((BooleanExpression) getChild(0)).isTrue(context);
63       }
64       throw new ParseException("Unsupported unary operation for non boolean expression", getChild(0).beginLine,
65           getChild(0).beginColumn);
66     }
67
68     // instanceof?
69
if (getOperator().equals("instanceof"))
70     {
71       if ((!(getChild(0) instanceof ValueExpression)) || (!(getChild(1) instanceof PropertyOrFunctionNode))) throw new ParseException(
72           "Can only apply instanceof to objects and types", beginLine, beginColumn);
73       PropertyOrFunctionNode pof = (PropertyOrFunctionNode) getChild(1);
74       if (pof.isFunction()) throw new ParseException("Can only apply instanceof to objects and types", beginLine,
75           beginColumn);
76
77       ValueExpression left = (ValueExpression) getChild(0);
78       PropertyNode right = (PropertyNode) pof.getChild(0);
79       Object JavaDoc leftVal = left.getValue(context);
80
81       String JavaDoc matchType = right.getPath();
82       String JavaDoc actualType = leftVal.getClass().getName();
83       if (leftVal instanceof DjObject)
84       {
85         actualType = ((DjObject) leftVal).getExtent().getObjectType();
86       }
87       if (matchType.indexOf(".") == -1)
88       {
89         int idx = actualType.lastIndexOf(".");
90         if (idx != -1)
91         {
92           actualType = actualType.substring(idx + 1);
93         }
94       }
95
96       // First try the extents
97
try
98       {
99         context.getManager().getExtentByObjectType(matchType);
100
101         return actualType.equals(matchType);
102       }
103       catch (ObjectNotDefinedException onde)
104       { // ignore
105
}
106
107       boolean validType = matchType.equals("String") || matchType.equals("java.lang.String")
108                           || matchType.equals("Integer") || matchType.equals("java.lang.Integer")
109                           || matchType.equals("Long") || matchType.equals("java.lang.Long")
110                           || matchType.equals("Timestamp") || matchType.equals("java.sql.Timestamp");
111       if (!validType) throw new ParseException("Unknown type: " + matchType, beginLine, beginColumn);
112
113       return actualType.equals(matchType);
114     }
115
116     // Boolean comparison?
117
if ((getChild(0) instanceof BooleanExpression) && (getChild(1) instanceof BooleanExpression))
118     {
119       BooleanExpression left = (BooleanExpression) getChild(0);
120       BooleanExpression right = (BooleanExpression) getChild(1);
121       if (getOperator().equals("=="))
122       {
123         return left.isTrue(context) == right.isTrue(context);
124       }
125       if (getOperator().equals("!="))
126       {
127         return left.isTrue(context) != right.isTrue(context);
128       }
129       throw new ParseException("Can only perform == and != to booleans", beginLine, beginColumn);
130     }
131
132     if ((getChild(0) instanceof ValueExpression) && (getChild(1) instanceof ValueExpression))
133     {
134       ValueExpression left = (ValueExpression) getChild(0);
135       ValueExpression right = (ValueExpression) getChild(1);
136       Object JavaDoc leftVal = left.getValue(context);
137       Object JavaDoc rightVal = right.getValue(context);
138
139       // First handle null stuff
140
if (leftVal == null || rightVal == null)
141       {
142         return false;
143       }
144
145       // String comparison?
146
if ((leftVal instanceof String JavaDoc) || (rightVal instanceof String JavaDoc))
147       {
148         if (getOperator().equals("=="))
149         {
150           return leftVal.toString().equals(rightVal.toString());
151         }
152         if (getOperator().equals("!="))
153         {
154           return !leftVal.toString().equals(rightVal.toString());
155         }
156         if (getOperator().equals(">="))
157         {
158           return leftVal.toString().compareTo(rightVal.toString()) >= 0;
159         }
160         if (getOperator().equals("<="))
161         {
162           return leftVal.toString().compareTo(rightVal.toString()) <= 0;
163         }
164         if (getOperator().equals("<"))
165         {
166           return leftVal.toString().compareTo(rightVal.toString()) < 0;
167         }
168         if (getOperator().equals(">"))
169         {
170           return leftVal.toString().compareTo(rightVal.toString()) > 0;
171         }
172       }
173
174       // Float comparison?
175
if ((leftVal instanceof Float JavaDoc) || (rightVal instanceof Float JavaDoc))
176       {
177         Float JavaDoc l = toFloat(leftVal);
178         Float JavaDoc r = toFloat(rightVal);
179
180         if (getOperator().equals("=="))
181         {
182           return l.equals(r);
183         }
184         if (getOperator().equals("!="))
185         {
186           return !l.equals(r);
187         }
188         if (getOperator().equals(">="))
189         {
190           return l.floatValue() >= r.floatValue();
191         }
192         if (getOperator().equals("<="))
193         {
194           return l.floatValue() <= r.floatValue();
195         }
196         if (getOperator().equals("<"))
197         {
198           return l.floatValue() < r.floatValue();
199         }
200         if (getOperator().equals(">"))
201         {
202           return l.floatValue() > r.floatValue();
203         }
204       }
205
206       // Integer comparison?
207
if ((leftVal instanceof Integer JavaDoc) || (rightVal instanceof Integer JavaDoc))
208       {
209         Integer JavaDoc l = toInteger(leftVal);
210         Integer JavaDoc r = toInteger(rightVal);
211
212         if (getOperator().equals("=="))
213         {
214           return l.equals(r);
215         }
216         if (getOperator().equals("!="))
217         {
218           return !l.equals(r);
219         }
220         if (getOperator().equals(">="))
221         {
222           return l.floatValue() >= r.floatValue();
223         }
224         if (getOperator().equals("<="))
225         {
226           return l.floatValue() <= r.floatValue();
227         }
228         if (getOperator().equals("<"))
229         {
230           return l.floatValue() < r.floatValue();
231         }
232         if (getOperator().equals(">"))
233         {
234           return l.floatValue() > r.floatValue();
235         }
236       }
237
238       // Boolean comparison?
239
if ((leftVal instanceof Boolean JavaDoc) && (rightVal instanceof Boolean JavaDoc))
240       {
241         boolean l = ((Boolean JavaDoc) leftVal).booleanValue();
242         boolean r = ((Boolean JavaDoc) rightVal).booleanValue();
243
244         if (getOperator().equals("=="))
245         {
246           return l == r;
247         }
248         if (getOperator().equals("!="))
249         {
250           return l != r;
251         }
252         throw new ParseException("Can only perform == and != to booleans", beginLine, beginColumn);
253       }
254       throw new ParseException("Can not perform " + classOnly(leftVal) + getOperator() + classOnly(leftVal), beginLine,
255           beginColumn);
256     }
257
258     throw new ParseException("Can not perform " + getOperator() + " on this type of expression", beginLine, beginColumn);
259
260   }
261
262 }
Popular Tags