1 package com.genimen.djeneric.repository.oql.core.nodes; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 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 _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 getName() 28 { 29 return toString(); 30 } 31 32 public String toString() 33 { 34 if (isNegated()) return "not " + _path; 35 return _path; 36 } 37 38 public void setPath(String path) 39 { 40 this._path = path; 41 } 42 43 public String 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 result, HashMap path2AliasMapping) throws ParseException 59 { 60 appendOpenBrackets(result); 61 if (isNegated()) result.append("not "); 62 63 String name = _path; 64 String 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 alias = (String ) 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 allPaths) 82 { 83 if (!allPaths.contains(getPath())) allPaths.add(getPath()); 84 } 85 86 public Object getValue(MatchingContext context) throws MatchException 87 { 88 try 89 { 90 if (negated) 91 { 92 Object o = ObjectPath.evaluateProperty(getPath(), context, this); 93 if (!(o instanceof Boolean )) throw new ParseException("Can not negate a non-boolean value", beginLine, 94 beginColumn); 95 Boolean b = (Boolean ) o; 96 return new Boolean (!b.booleanValue()); 97 } 98 else return ObjectPath.evaluateProperty(getPath(), context, this); 99 } 100 catch (Exception x) 101 { 102 throw new MatchException(x, beginLine, beginColumn); 103 } 104 } 105 } | Popular Tags |