View Javadoc

1   package org.apache.velocity.context;
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.HashMap;
23  import java.util.Stack;
24  
25  import org.apache.velocity.app.event.EventCartridge;
26  import org.apache.velocity.runtime.resource.Resource;
27  import org.apache.velocity.util.introspection.IntrospectionCacheData;
28  
29  /**
30   *  class to encapsulate the 'stuff' for internal operation of velocity.
31   *  We use the context as a thread-safe storage : we take advantage of the
32   *  fact that it's a visitor  of sorts  to all nodes (that matter) of the
33   *  AST during init() and render().
34   *  Currently, it carries the template name for namespace
35   *  support, as well as node-local context data introspection caching.
36   *
37   *  Note that this is not a public class.  It is for package access only to
38   *  keep application code from accessing the internals, as AbstractContext
39   *  is derived from this.
40   *
41   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
42   * @version $Id: InternalContextBase.java 463298 2006-10-12 16:10:32Z henning $
43   */
44  class InternalContextBase implements InternalHousekeepingContext, InternalEventContext
45  {
46      /**
47       * Version Id for serializable
48       */
49      private static final long serialVersionUID = -245905472770843470L;
50  
51      /**
52       *  cache for node/context specific introspection information
53       */
54      private HashMap introspectionCache = new HashMap(33);
55  
56      /**
57       *  Template name stack. The stack top contains the current template name.
58       */
59      private Stack templateNameStack = new Stack();
60  
61      /**
62       *  EventCartridge we are to carry.  Set by application
63       */
64      private EventCartridge eventCartridge = null;
65  
66      /**
67       *  Current resource - used for carrying encoding and other
68       *  information down into the rendering process
69       */
70      private Resource currentResource = null;
71  
72      /**
73       *  Is rendering allowed?  Defaults to true, can be changed by #stop directive.
74       */
75      private boolean allowRendering = true;
76  
77      /**
78       *  set the current template name on top of stack
79       *
80       *  @param s current template name
81       */
82      public void pushCurrentTemplateName( String s )
83      {
84          templateNameStack.push(s);
85      }
86  
87      /**
88       *  remove the current template name from stack
89       */
90      public void popCurrentTemplateName()
91      {
92          templateNameStack.pop();
93      }
94  
95      /**
96       *  get the current template name
97       *
98       *  @return String current template name
99       */
100     public String getCurrentTemplateName()
101     {
102         if ( templateNameStack.empty() )
103             return "<undef>";
104         else
105             return (String) templateNameStack.peek();
106     }
107 
108     /**
109      *  get the current template name stack
110      *
111      *  @return Object[] with the template name stack contents.
112      */
113     public Object[] getTemplateNameStack()
114     {
115         return templateNameStack.toArray();
116     }
117 
118     /**
119      *  returns an IntrospectionCache Data (@see IntrospectionCacheData)
120      *  object if exists for the key
121      *
122      *  @param key  key to find in cache
123      *  @return cache object
124      */
125     public IntrospectionCacheData icacheGet( Object key )
126     {
127         return ( IntrospectionCacheData ) introspectionCache.get( key );
128     }
129 
130     /**
131      *  places an IntrospectionCache Data (@see IntrospectionCacheData)
132      *  element in the cache for specified key
133      *
134      *  @param key  key
135      *  @param o  IntrospectionCacheData object to place in cache
136      */
137     public void icachePut( Object key, IntrospectionCacheData o )
138     {
139         introspectionCache.put( key, o );
140     }
141 
142     /**
143      * @see org.apache.velocity.context.InternalHousekeepingContext#setCurrentResource(org.apache.velocity.runtime.resource.Resource)
144      */
145     public void setCurrentResource( Resource r )
146     {
147         currentResource = r;
148     }
149 
150     /**
151      * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentResource()
152      */
153     public Resource getCurrentResource()
154     {
155         return currentResource;
156     }
157 
158 
159      /**
160      * @see org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
161      */
162     public boolean getAllowRendering()
163      {
164         return allowRendering;
165      }
166 
167     /**
168      * @see org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
169      */
170     public void setAllowRendering(boolean v)
171     {
172         allowRendering = v;
173     }
174 
175 
176     /**
177      * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
178      */
179     public EventCartridge attachEventCartridge( EventCartridge ec )
180     {
181         EventCartridge temp = eventCartridge;
182 
183         eventCartridge = ec;
184 
185         return temp;
186     }
187 
188     /**
189      * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
190      */
191     public EventCartridge getEventCartridge()
192     {
193         return eventCartridge;
194     }
195 }
196