Source code: com/tripi/asp/RedimNode.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 import java.util.Vector;
30
31 /**
32 * RedimNode handles the REDIM statement in vbscript.
33 *
34 * @author Terence Haddock
35 * @version 0.9
36 */
37 public class RedimNode extends DefaultNode
38 {
39 /** Debugging class */
40 static private Category DBG = Category.getInstance(RedimNode.class);
41
42 /** Identifier of variable to redim */
43 IdentNode ident;
44
45 /** List of new dimensions */
46 VarListNode dimensions;
47
48 /** Preserve the existing entries? */
49 boolean preserve;
50
51 /**
52 * Constructor.
53 * @param ident Identifier of variable to redim.
54 * @param dimensions list of sizes of dimensions
55 * @param preserve Preserve the existing entries?
56 */
57 public RedimNode(IdentNode ident, VarListNode dimensions, boolean preserve)
58 {
59 this.ident = ident;
60 this.dimensions = dimensions;
61 this.preserve = preserve;
62 }
63
64 /**
65 * Get the ident
66 * @param ident Identifier
67 */
68 public IdentNode getIdent()
69 {
70 return ident;
71 }
72
73 /**
74 * Get the dimension list.
75 * @return dimension list
76 */
77 public VarListNode getDimensionList()
78 {
79 return dimensions;
80 }
81
82 /**
83 * Should this redimension preserve the values?
84 * @return <b>true</b> or <b>false</b>
85 */
86 public boolean isPreserve()
87 {
88 return preserve;
89 }
90
91 /**
92 * Dumps the source representation of this node.
93 * @throws AspException on error
94 * @see Node#dump
95 */
96 public void dump() throws AspException
97 {
98 System.out.print("REDIM ");
99 ident.dump();
100 System.out.print("(");
101 dimensions.dump();
102 System.out.println(")");
103 }
104
105 /**
106 * Executes this node.
107 * @param context AspContext under which this node is to be execute.
108 * @return return value, ReDimNode-s have no return value.
109 * @throws AspException on error
110 * @see Node#execute
111 */
112 public Object execute(AspContext context) throws AspException
113 {
114 if (DBG.isDebugEnabled()) {
115 DBG.debug("Redim-ing: " + ident + " to " + dimensions.size() + " dims");
116 DBG.debug("Preseve: " + preserve);
117 }
118 Vector vecValues = (Vector)dimensions.execute(context);
119
120 if (preserve)
121 {
122 Object obj = context.getValue(ident);
123 if (DBG.isDebugEnabled())
124 DBG.debug("Sub-element: " + obj.getClass());
125 if (obj instanceof ArrayNode)
126 {
127 ArrayNode an = (ArrayNode)obj;
128 an.internResizeArray(vecValues, 0);
129 return null;
130 }
131 }
132
133 if (dimensions.size() > 0) {
134 ArrayNode an = createArray(vecValues, 0);
135 context.setValue(ident, an);
136 }
137 return null;
138 }
139
140 /**
141 * Internal function which creates an array from a list of dimensions.
142 * @param vl List of dimensions
143 * @param index Current position, initially should be 0
144 * @return new ArrayNode
145 */
146 static ArrayNode createArray(Vector vl, int index) throws AspException {
147 Integer size = Types.coerceToInteger(vl.get(index));
148 ArrayNode array = new ArrayNode(size.intValue()+1);
149 if (index < vl.size() - 1) {
150 for (int i = 0; i <= size.intValue(); i++) {
151 ArrayNode an = createArray(vl, index+1);
152 array._setValue(i, an);
153 }
154 }
155 return array;
156 }
157 };
158