KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.tools.generator.core.nodes;
2
3 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine;
4 import com.genimen.djeneric.tools.generator.core.ParseException;
5 import com.genimen.djeneric.tools.generator.core.SimpleNode;
6 import com.genimen.djeneric.tools.generator.core.util.ParseContext;
7
8 public class IfNode extends SimpleNode
9 {
10
11   public IfNode(int i)
12   {
13     super(i);
14   }
15
16   public IfNode(DjentelParserEngine p, int i)
17   {
18     super(p, i);
19   }
20
21   public String JavaDoc getName()
22   {
23     return "if";
24   }
25
26   public String JavaDoc toString()
27   {
28     return "";
29   }
30
31   public String JavaDoc evaluate(ParseContext context) throws ParseException
32   {
33     if (!(getChild(0) instanceof BooleanExpression))
34     {
35       if (getChild(0) instanceof ValueExpression)
36       {
37         ValueExpression ve = (ValueExpression) getChild(0);
38         Object JavaDoc o = ve.getValue(context);
39         if (o instanceof Boolean JavaDoc)
40         {
41           Boolean JavaDoc b = (Boolean JavaDoc) o;
42           if (b.booleanValue()) return handleTrue(context);
43           else return handleFalse(context);
44         }
45       }
46       throw new ParseException("Not a boolean expression", beginLine, beginColumn);
47     }
48
49     BooleanExpression expr = (BooleanExpression) getChild(0);
50     if (expr.isTrue(context)) return handleTrue(context);
51     else return handleFalse(context);
52   }
53
54   String JavaDoc handleTrue(ParseContext context) throws ParseException
55   {
56     return getChild(1).evaluate(context);
57   }
58
59   String JavaDoc handleFalse(ParseContext context) throws ParseException
60   {
61     // Does the if have an else?
62
if (getChildCount() == 3)
63     {
64       return getChild(2).evaluate(context);
65     }
66     return "";
67   }
68 }
Popular Tags