1 package com.genimen.djeneric.tools.generator.core.nodes; 2 3 import java.util.ArrayList ; 4 5 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine; 6 import com.genimen.djeneric.tools.generator.core.ParseException; 7 import com.genimen.djeneric.tools.generator.core.SimpleNode; 8 import com.genimen.djeneric.tools.generator.core.util.Convention; 9 import com.genimen.djeneric.tools.generator.core.util.ObjectPath; 10 import com.genimen.djeneric.tools.generator.core.util.ParseContext; 11 12 public class PropertyOrFunctionNode extends SimpleNode implements ValueExpression 13 { 14 boolean _isFunction = false; 15 16 public PropertyOrFunctionNode(int i) 17 { 18 super(i); 19 } 20 21 public PropertyOrFunctionNode(DjentelParserEngine p, int i) 22 { 23 super(p, i); 24 } 25 26 public String getName() 27 { 28 if (_isFunction) return "function"; 29 return "property"; 30 } 31 32 public String toString() 33 { 34 return getName(); 35 } 36 37 public boolean isFunction() 38 { 39 return _isFunction; 40 } 41 42 public void setFunction(boolean b) 43 { 44 _isFunction = b; 45 } 46 47 public Object getValue(ParseContext context) throws ParseException 48 { 49 if (isFunction()) 50 { 51 return evalFunction(context); 52 } 53 else 54 { 55 ValueExpression prop = (ValueExpression) getChild(0); 56 return prop.getValue(context); 57 } 58 } 59 60 protected Object [] getParams(ParseContext context) throws ParseException 61 { 62 ArrayList paramList = new ArrayList (); 63 for (int i = 1; i < getChildCount(); i++) 64 { 65 if (!(getChild(i) instanceof ValueExpression)) 66 { 67 throw new ParseException("Parameter " + getChild(i) + " is not a value expression", beginLine, beginColumn); 68 } 69 ValueExpression val = (ValueExpression) getChild(i); 70 paramList.add(val.getValue(context)); 71 } 72 return paramList.toArray(new Object [0]); 73 } 74 75 protected Object evalFunction(Object obj, ParseContext context) throws Exception 76 { 77 PropertyNode function = (PropertyNode) getChild(0); 78 String path = function.getPath(); 79 80 return evalFunction(obj, path, context); 81 } 82 83 protected Object evalFunction(Object obj, String functionName, ParseContext context) throws Exception 84 { 85 Convention convention = context.getConvention(); 86 return convention.call(obj, functionName, getParams(context), context); 87 } 88 89 protected Object evalFunction(ParseContext context) throws ParseException 90 { 91 try 92 { 93 PropertyNode function = (PropertyNode) getChild(0); 94 String path = function.getPath(); 95 String functionName = null; 96 97 int idx = path.lastIndexOf("."); 98 if (idx == -1) 99 { 100 functionName = path; 101 path = null; 102 } 103 else 104 { 105 functionName = path.substring(idx + 1); 106 path = path.substring(0, idx); 107 } 108 109 if (path != null) 110 { 111 Object obj = ObjectPath.evaluateProperty(path, context); 112 return evalFunction(obj, functionName, context); 113 } 114 else 115 { 116 return context.getConvention().staticCall(functionName, getParams(context), context); 117 } 118 } 119 catch (Exception x) 120 { 121 throw new ParseException(x, beginLine, beginColumn); 122 } 123 } 124 } | Popular Tags |