KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.tools.generator.core.nodes;
2
3 import java.util.ArrayList JavaDoc;
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 JavaDoc getName()
27   {
28     if (_isFunction) return "function";
29     return "property";
30   }
31
32   public String JavaDoc 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 JavaDoc 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 JavaDoc[] getParams(ParseContext context) throws ParseException
61   {
62     ArrayList JavaDoc paramList = new ArrayList JavaDoc();
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 JavaDoc[0]);
73   }
74
75   protected Object JavaDoc evalFunction(Object JavaDoc obj, ParseContext context) throws Exception JavaDoc
76   {
77     PropertyNode function = (PropertyNode) getChild(0);
78     String JavaDoc path = function.getPath();
79
80     return evalFunction(obj, path, context);
81   }
82
83   protected Object JavaDoc evalFunction(Object JavaDoc obj, String JavaDoc functionName, ParseContext context) throws Exception JavaDoc
84   {
85     Convention convention = context.getConvention();
86     return convention.call(obj, functionName, getParams(context), context);
87   }
88
89   protected Object JavaDoc evalFunction(ParseContext context) throws ParseException
90   {
91     try
92     {
93       PropertyNode function = (PropertyNode) getChild(0);
94       String JavaDoc path = function.getPath();
95       String JavaDoc 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 JavaDoc 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 JavaDoc x)
120     {
121       throw new ParseException(x, beginLine, beginColumn);
122     }
123   }
124 }
Popular Tags