KickJava   Java API By Example, From Geeks To Geeks.

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


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 AndOrNode extends SimpleNode implements BooleanExpression, ValueExpression
9 {
10
11   public AndOrNode(int i)
12   {
13     super(i);
14   }
15
16   public AndOrNode(DjentelParserEngine p, int i)
17   {
18     super(p, i);
19   }
20
21   public String JavaDoc getName()
22   {
23     return toString();
24   }
25
26   public String JavaDoc toString()
27   {
28     return getOperator();
29   }
30
31   public void setOperator(String JavaDoc operator)
32   {
33     this.operator = operator;
34   }
35
36   public String JavaDoc getOperator()
37   {
38     return operator;
39   }
40
41   private String JavaDoc operator;
42
43   public Object JavaDoc getValue(ParseContext context) throws ParseException
44   {
45     return new Boolean JavaDoc(evaluate(context));
46   }
47
48   public boolean isTrue(ParseContext context) throws ParseException
49   {
50     if (!(getChild(0) instanceof BooleanExpression))
51     {
52       throw new ParseException("Expression is not boolean", getChild(0).beginLine, getChild(0).beginColumn);
53     }
54     if (!(getChild(1) instanceof BooleanExpression))
55     {
56       throw new ParseException("Expression is not boolean", getChild(1).beginLine, getChild(1).beginColumn);
57     }
58
59     BooleanExpression left = (BooleanExpression) getChild(0);
60     BooleanExpression right = (BooleanExpression) getChild(1);
61
62     if (getOperator().equals("&&"))
63     {
64       return left.isTrue(context) && right.isTrue(context);
65     }
66     else if (getOperator().equals("||"))
67     {
68       return left.isTrue(context) || right.isTrue(context);
69     }
70     throw new ParseException("Unsupported operator: " + getOperator(), beginLine, beginColumn);
71   }
72
73 }
74
Popular Tags