KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.repository.oql.core.nodes;
2
3 import java.util.HashMap JavaDoc;
4
5 import com.genimen.djeneric.repository.oql.core.DjOqlParserEngine;
6 import com.genimen.djeneric.repository.oql.core.MatchException;
7 import com.genimen.djeneric.repository.oql.core.ParseException;
8 import com.genimen.djeneric.repository.oql.core.SimpleNode;
9
10 public class AndOrNode extends SimpleNode implements BooleanExpression, ValueExpression
11 {
12   private String JavaDoc _operator;
13
14   public AndOrNode(int i)
15   {
16     super(i);
17   }
18
19   public AndOrNode(DjOqlParserEngine p, int i)
20   {
21     super(p, i);
22   }
23
24   public String JavaDoc getName()
25   {
26     return toString();
27   }
28
29   public String JavaDoc toString()
30   {
31     return getOperator();
32   }
33
34   public void setOperator(String JavaDoc operator)
35   {
36     if (operator.equals("and")) operator = "&&";
37     if (operator.equals("or")) operator = "||";
38
39     this._operator = operator;
40   }
41
42   public String JavaDoc getOperator()
43   {
44     return _operator;
45   }
46
47   public void translate(StringBuffer JavaDoc result, HashMap JavaDoc path2AliasMapping) throws ParseException
48   {
49     appendOpenBrackets(result);
50     getChild(0).translate(result, path2AliasMapping);
51
52     if (isAnd()) result.append("\nand ");
53     else if (isOr()) result.append("\nor ");
54     else throw new ParseException("Unsupported operator " + _operator);
55
56     getChild(1).translate(result, path2AliasMapping);
57     appendCloseBrackets(result);
58   }
59
60   private boolean isAnd()
61   {
62     return _operator.equals("&&");
63   }
64
65   private boolean isOr()
66   {
67     return _operator.equals("||");
68   }
69
70   public Object JavaDoc getValue(MatchingContext context) throws MatchException
71   {
72     return new Boolean JavaDoc(isTrue(context));
73   }
74
75   public boolean isTrue(MatchingContext context) throws MatchException
76   {
77     if (!(getChild(0) instanceof BooleanExpression))
78     {
79       throw new MatchException("Expression is not boolean", getChild(0).beginLine, getChild(0).beginColumn);
80     }
81     if (!(getChild(1) instanceof BooleanExpression))
82     {
83       throw new MatchException("Expression is not boolean", getChild(1).beginLine, getChild(1).beginColumn);
84     }
85
86     BooleanExpression left = (BooleanExpression) getChild(0);
87     BooleanExpression right = (BooleanExpression) getChild(1);
88
89     if (isAnd())
90     {
91       return left.isTrue(context) && right.isTrue(context);
92     }
93     else if (isOr())
94     {
95       return left.isTrue(context) || right.isTrue(context);
96     }
97     throw new MatchException("Unsupported operator: " + getOperator(), beginLine, beginColumn);
98   }
99
100   public boolean match(MatchingContext context) throws MatchException
101   {
102     return isTrue(context);
103   }
104
105 }
Popular Tags