1 package org.apache.dvsl;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.List;
23 import java.util.ArrayList;
24 import java.util.Map;
25 import java.util.HashMap;
26 import java.util.Stack;
27
28 import org.apache.velocity.context.Context;
29 import org.apache.velocity.VelocityContext;
30
31 /**
32 * <p>
33 * Context implementation that is the outer context
34 * during the transformation. Holds the node stack
35 * and also protects the 'special' context elements
36 * like 'node'
37 * </p>
38 * <p>
39 * There are special elements like 'node', which is
40 * readonly and corresponds to the current node, and
41 * 'attrib', which corresponds to a map of attributes
42 * for the current node.
43 * </p>
44 *
45 * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
46 */
47 class DVSLNodeContext extends VelocityContext
48 {
49
50 /**
51 * Magic context entity that corresponds
52 * to the current node
53 */
54 private final static String NODE = "node";
55
56 /**
57 * Magic context entity that corresponds to
58 * a Map of attributes for the current node
59 */
60 private final static String ATTRIB = "attrib";
61
62 /**
63 * Used to hold the nodes as we get invoked from
64 * within the document for applyTemplates() duties
65 */
66 private Stack nodeStack = new Stack();
67
68 protected Map ctx = new HashMap();
69
70
71 public DVSLNodeContext( Context context )
72 {
73 super(context );
74 }
75
76 private DVSLNodeContext()
77 {
78 }
79
80 /**
81 * retrieves value for key from internal
82 * storage
83 *
84 * @param key name of value to get
85 * @return value as object
86 */
87 public Object internalGet( String key )
88 {
89 Object o = null;
90
91 /*
92 * special token : NODE
93 *
94 * returns current node
95 */
96
97 if ( key.equals( NODE ))
98 {
99 return peekNode();
100 }
101
102 /*
103 * ATTRIB - returns attribute map
104 */
105
106 if( key.equals( ATTRIB ) )
107 {
108 DVSLNode n = peekNode();
109
110 return n.getAttribMap();
111 }
112
113 /*
114 * start with local storage
115 */
116
117 return ctx.get( key );
118 }
119
120 /**
121 * stores the value for key to internal
122 * storage
123 *
124 * @param key name of value to store
125 * @param value value to store
126 * @return previous value of key as Object
127 */
128 public Object internalPut( String key, Object value )
129 {
130 /*
131 * protect both NODE and ATTRIB for now. We
132 * might want to let people set ATTRIB, but
133 * I suspect not
134 */
135
136 if ( key.equals( NODE ))
137 return null;
138
139 if ( key.equals( ATTRIB ) )
140 return null;
141
142 return ctx.put( key, value );
143 }
144
145 /**
146 * determines if there is a value for the
147 * given key
148 *
149 * @param key name of value to check
150 * @return true if non-null value in store
151 */
152 public boolean internalContainsKey(Object key)
153 {
154 return ctx.containsKey( key );
155 }
156
157 /**
158 * returns array of keys
159 *
160 * $$$ GMJ todo
161 *
162 * @return keys as []
163 */
164 public Object[] internalGetKeys()
165 {
166 return null;
167 }
168
169 /**
170 * remove a key/value pair from the
171 * internal storage
172 *
173 * @param key name of value to remove
174 * @return value removed
175 */
176 public Object internalRemove(Object key)
177 {
178 return ctx.remove( key );
179 }
180
181
182 /* === routines to manage current node stack === */
183
184 DVSLNode pushNode( DVSLNode n )
185 {
186 nodeStack.push( n );
187 return n;
188 }
189
190 DVSLNode peekNode()
191 {
192 return (DVSLNode) nodeStack.peek();
193 }
194
195 DVSLNode popNode()
196 {
197 return (DVSLNode) nodeStack.pop();
198 }
199
200 void clearNode()
201 {
202 nodeStack.clear();
203 return;
204 }
205
206 }