Source code: com/tripi/asp/GetFieldNode.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp;
26
27 import org.apache.log4j.Category;
28
29 /**
30 * This node handles the evaluating of a field expression evaluation.
31 * Such as Object.Value.
32 *
33 * @author Terence Haddock
34 * @version 0.9
35 */
36 public class GetFieldNode extends DefaultNode
37 {
38 /** Debugging category */
39 static private final Category DBG =
40 Category.getInstance(GetFieldNode.class);
41
42 /** Identifier of the field expression */
43 IdentNode ident;
44
45 /** Expression which to find the field of */
46 Node expr;
47
48 /**
49 * Constructor.
50 *
51 * @param expr Expression of the field.
52 * @param ident Identifier of the field to evaluate.
53 */
54 public GetFieldNode(Node expr, IdentNode ident)
55 {
56 this.ident = ident;
57 this.expr = (Node)expr;
58 }
59
60 /**
61 * Get the ident.
62 * TODO: This should be getIdentiifer()
63 * @return field this object is getting.
64 */
65 public IdentNode getIdent()
66 {
67 return ident;
68 }
69
70 /**
71 * Expression this object is getting the field of.
72 * @return expression this object is getting the field index of.
73 */
74 public Node getExpression()
75 {
76 return expr;
77 }
78
79 /**
80 * Dump this expression.
81 * @see Node#dump
82 * @throws AspException if an error occurs.
83 */
84 public void dump() throws AspException
85 {
86 System.out.print("{F}");
87 expr.dump();
88 System.out.print(".");
89 ident.dump();
90 }
91
92 /**
93 * Executes this field expression.
94 *
95 * @param context AspContext under which to evaluate the expression.
96 * @return return value of this expression
97 * @throws AspException If an error occurs
98 * @see Node#execute(AspContext)
99 */
100 public Object execute(AspContext context) throws AspException
101 {
102 if (DBG.isDebugEnabled())
103 DBG.debug("execute");
104 Object value = expr.execute(context);
105 while (value instanceof SimpleReference &&
106 !(value instanceof ObjectNode))
107 {
108 if (DBG.isDebugEnabled())
109 DBG.debug("De-referencing " + value);
110 value = ((SimpleReference)value).getValue();
111 }
112 if (value instanceof ObjectNode)
113 {
114 ObjectNode obj = (ObjectNode)value;
115 if (DBG.isDebugEnabled())
116 DBG.debug("Get field of " + ident + " from " + obj);
117 return Types.coerceToNode(obj.getField(ident));
118 } else {
119 throw new AspException("Invalid class for field get: " + value.getClass().getName());
120 }
121 }
122 };
123