KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.repository.oql.core.nodes;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import com.genimen.djeneric.repository.oql.core.DjOqlParserEngine;
7 import com.genimen.djeneric.repository.oql.core.MatchException;
8 import com.genimen.djeneric.repository.oql.core.ParseException;
9 import com.genimen.djeneric.repository.oql.core.SimpleNode;
10 import com.genimen.djeneric.repository.oql.core.util.ObjectPath;
11
12 public class PropertyNode extends SimpleNode implements ValueExpression
13 {
14   private String JavaDoc _path;
15   private boolean negated = false;
16
17   public PropertyNode(int i)
18   {
19     super(i);
20   }
21
22   public PropertyNode(DjOqlParserEngine p, int i)
23   {
24     super(p, i);
25   }
26
27   public String JavaDoc getName()
28   {
29     return toString();
30   }
31
32   public String JavaDoc toString()
33   {
34     if (isNegated()) return "not " + _path;
35     return _path;
36   }
37
38   public void setPath(String JavaDoc path)
39   {
40     this._path = path;
41   }
42
43   public String JavaDoc getPath()
44   {
45     return _path;
46   }
47
48   public void setNegated(boolean negated)
49   {
50     this.negated = negated;
51   }
52
53   public boolean isNegated()
54   {
55     return negated;
56   }
57
58   public void translate(StringBuffer JavaDoc result, HashMap JavaDoc path2AliasMapping) throws ParseException
59   {
60     appendOpenBrackets(result);
61     if (isNegated()) result.append("not ");
62
63     String JavaDoc name = _path;
64     String JavaDoc path = "";
65
66     int idx = _path.lastIndexOf('.');
67     if (idx != -1)
68     {
69       path = _path.substring(0, idx);
70       name = _path.substring(idx + 1);
71     }
72
73     String JavaDoc alias = (String JavaDoc) path2AliasMapping.get(path);
74     if (alias == null) throw new ParseException("Unmapped path encountered: " + path);
75     result.append(alias);
76     result.append(".");
77     result.append(name);
78     appendCloseBrackets(result);
79   }
80
81   public void addPaths(ArrayList JavaDoc allPaths)
82   {
83     if (!allPaths.contains(getPath())) allPaths.add(getPath());
84   }
85
86   public Object JavaDoc getValue(MatchingContext context) throws MatchException
87   {
88     try
89     {
90       if (negated)
91       {
92         Object JavaDoc o = ObjectPath.evaluateProperty(getPath(), context, this);
93         if (!(o instanceof Boolean JavaDoc)) throw new ParseException("Can not negate a non-boolean value", beginLine,
94             beginColumn);
95         Boolean JavaDoc b = (Boolean JavaDoc) o;
96         return new Boolean JavaDoc(!b.booleanValue());
97       }
98       else return ObjectPath.evaluateProperty(getPath(), context, this);
99     }
100     catch (Exception JavaDoc x)
101     {
102       throw new MatchException(x, beginLine, beginColumn);
103     }
104   }
105 }
Popular Tags