View Javadoc

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   *  Context implementation that handles wrapping several
33   *  contexts simultaneously.  The style context gets
34   *  special treatment, getting checked first.
35   *
36   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
37   */
38  class DVSLContext extends VelocityContext
39  {
40      protected Context styleContext = null;
41      protected List contextList = new ArrayList();
42  
43      /**
44       *  Used to hold the nodes as we get invoked from
45       *  within the document for applyTemplates() duties
46       */
47      private Stack nodeStack = new Stack();
48  
49      protected Map ctx = new HashMap();
50  
51      public DVSLNode pushNode( DVSLNode n )
52      {
53          nodeStack.push( n );
54          return n;
55      }
56  
57      public DVSLNode peekNode()
58      {
59          return (DVSLNode) nodeStack.peek();
60      }
61  
62      public DVSLNode popNode()
63      {
64          return (DVSLNode) nodeStack.pop();
65      }
66  
67      public void clearNode()
68      {
69          nodeStack.clear();
70          return;
71      }
72  
73      public void clearContexts()
74      {
75          styleContext = null;
76          contextList.clear();
77      }
78  
79      public void addContext( Context c )
80      {
81          if (c != null)
82              contextList.add( c );
83      }
84  
85      public void setStyleContext( Context c )
86      {
87          styleContext = c;
88      }
89  
90     /**
91       *  retrieves value for key from internal
92       *  storage
93       *
94       *  @param key name of value to get
95       *  @return value as object
96       */
97      public Object internalGet( String key )
98      {
99          Object o = null;
100 
101         /*
102          *  special tokens
103          */
104 
105         if ( key.equals("node"))
106         {
107             return peekNode();
108         }
109 
110         /*
111          *  start with local storage
112          */
113 
114         o = ctx.get( key );
115 
116         if ( o != null)
117             return o;
118 
119         /*
120          *  if that doesn't work, try style first
121          *  then others
122          */
123 
124         if ( styleContext != null)
125         {
126             o = styleContext.get( key );
127 
128             if ( o != null )
129                 return o;
130         }
131 
132         for( int i = 0; i < contextList.size(); i++)
133         {
134 
135             Context c = (Context) contextList.get( i );
136 
137             o = c.get( key );
138 
139             if ( o != null )
140                 return o;
141         }
142 
143         return null;
144     }
145 
146     /**
147      *  stores the value for key to internal
148      *  storage
149      *
150      *  @param key name of value to store
151      *  @param value value to store
152      *  @return previous value of key as Object
153      */
154     public Object internalPut( String key, Object value )
155     {
156         if ( key.equals("node"))
157             return null;
158 
159         return ctx.put( key, value );
160     }
161 
162     /**
163      *  determines if there is a value for the
164      *  given key
165      *
166      *  @param key name of value to check
167      *  @return true if non-null value in store
168      */
169     public  boolean internalContainsKey(Object key)
170     {
171         /*
172          *  start with local storage
173          */
174 
175         if ( ctx.containsKey( key ))
176             return true;
177 
178         /*
179          *  if that doesn't work, try style first
180          *  then others
181          */
182 
183         if ( styleContext != null && styleContext.containsKey( key ) )
184             return true;
185 
186         for( int i = 0; i < contextList.size(); i++)
187         {
188             Context c = (Context) contextList.get( i );
189 
190             if ( c.containsKey( key ))
191                 return true;
192         }
193 
194         return false;
195     }
196 
197     /**
198      *  returns array of keys
199      *
200      *  $$$ GMJ todo
201      *
202      *  @return keys as []
203      */
204     public  Object[] internalGetKeys()
205     {
206         return null;
207     }
208 
209     /**
210      *  remove a key/value pair from the
211      *  internal storage
212      *
213      *  @param key name of value to remove
214      *  @return value removed
215      */
216     public  Object internalRemove(Object key)
217     {
218         return ctx.remove( key );
219     }
220 
221 
222 }