Source code: com/tripi/asp/SetValueNode.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 java.util.Vector;
28
29 import org.apache.log4j.Category;
30
31 /**
32 * SetValueNode implements setting of variables, the following construct:
33 * <pre>
34 * Set A = "Value"
35 * </pre>
36 * And:
37 * <pre>
38 * A = "Value"
39 * </pre>
40 *
41 * @author Terence Haddock
42 */
43 public class SetValueNode implements Node
44 {
45 /** Debugging category */
46 static Category DBG = Category.getInstance(SetValueNode.class);
47
48 /** The identifier we wish to set */
49 Node ident;
50
51 /** The identifier's new expression when executed */
52 Node expr;
53
54 /** Was the "SET" keyword used? */
55 boolean setUsed;
56
57 /**
58 * Constructor.
59 * @param ident Identifier we wish to set, can be an IdentNode,
60 * or a compound expression which evaluates to a SimpleReference or
61 * FunctionNode.
62 * @param expr Target expression for the identifier.
63 * @param setUsed was the SET keyword used?
64 */
65 public SetValueNode(Node ident, Node expr, boolean setUsed)
66 {
67 this.ident = ident;
68 this.expr = expr;
69 this.setUsed = setUsed;
70 }
71
72 /**
73 * Gets the identifier which is being set.
74 * @return identifier being set
75 */
76 public Node getIdent()
77 {
78 return ident;
79 }
80
81 /**
82 * Gets the expression.
83 * @return expression
84 */
85 public Node getExpression()
86 {
87 return expr;
88 }
89
90 /**
91 * Dumps the debugging information about this node.
92 * @throws AspException on error
93 */
94 public void dump() throws AspException
95 {
96 System.out.print("{S}");
97 if (setUsed) System.out.print("SET ");
98 ident.dump();
99 System.out.print("=");
100 expr.dump();
101 }
102
103 /**
104 * Prepare this node for execution. This does nothing for a SetValueNode.
105 * @param context AspContext for this node.
106 * @throws AspException on error
107 */
108 public void prepare(AspContext context) throws AspException
109 {
110 /* Nothing to do, the arguments to SetValueNodes should not
111 * need prepared.
112 */
113 }
114
115 /**
116 * Executes this node, setting the value of the node.
117 * @param context Context for the node.
118 * @return Always returns null.
119 * @throws AspException on error
120 */
121 public Object execute(AspContext context) throws AspException
122 {
123 /* Executes the expression, obtaning the dereferenced value */
124 Object value = Types.dereference(expr.execute(context));
125 if (DBG.isDebugEnabled()) {
126 DBG.debug("Execute " + ident + " = " + value);
127 }
128 /* Check if the expression is valid for this expression, depending on
129 if the SET keyword was used */
130 if (setUsed && !(value instanceof ObjectNode))
131 {
132 DBG.debug("SET was used where not allowed");
133 /* XXX throw new AspInvalidArgumentsException("SET"); */
134 } else if (!setUsed && (value instanceof ObjectNode))
135 {
136 DBG.debug("SET was not used where required");
137 /* XXX throw new AspInvalidArgumentsException("SET"); */
138 }
139 /* Test for IdentNode */
140 if (ident instanceof IdentNode) {
141 Object identVal = context.getValue((IdentNode)ident);
142 if (identVal instanceof ConstValue)
143 throw new AspReadOnlyException("Constant value");
144 context.setValue((IdentNode)ident, value);
145 } else {
146 /* If it's not an IdentNode, we execute the node and see what
147 * the result is. */
148 Object identVal = ident.execute(context);
149 if (identVal instanceof SimpleReference) {
150 ((SimpleReference)identVal).setValue(value);
151 } else if (identVal instanceof FunctionNode) {
152 VarListNode vl = new VarListNode();
153 vl.append(value);
154 ((FunctionNode)identVal).execute(vl, context);
155 } else {
156 throw new AspException("Unknown object to set: " +
157 identVal.getClass().getName());
158 }
159 }
160 return null;
161 }
162 };
163