| 1 | package org.apache.velocity.context; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Stack; |
| 21 | import java.io.Serializable; |
| 22 | |
| 23 | import org.apache.velocity.util.introspection.IntrospectionCacheData; |
| 24 | |
| 25 | import org.apache.velocity.app.event.EventCartridge; |
| 26 | import org.apache.velocity.runtime.resource.Resource; |
| 27 | |
| 28 | /** |
| 29 | * class to encapsulate the 'stuff' for internal operation of velocity. |
| 30 | * We use the context as a thread-safe storage : we take advantage of the |
| 31 | * fact that it's a visitor of sorts to all nodes (that matter) of the |
| 32 | * AST during init() and render(). |
| 33 | * Currently, it carries the template name for namespace |
| 34 | * support, as well as node-local context data introspection caching. |
| 35 | * |
| 36 | * Note that this is not a public class. It is for package access only to |
| 37 | * keep application code from accessing the internals, as AbstractContext |
| 38 | * is derived from this. |
| 39 | * |
| 40 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 41 | * @version $Id: InternalContextBase.java,v 1.8.12.1 2004/03/03 23:22:54 geirm Exp $ |
| 42 | */ |
| 43 | class InternalContextBase implements InternalHousekeepingContext, InternalEventContext, Serializable |
| 44 | { |
| 45 | /** |
| 46 | * cache for node/context specific introspection information |
| 47 | */ |
| 48 | private HashMap introspectionCache = new HashMap(33); |
| 49 | |
| 50 | /** |
| 51 | * Template name stack. The stack top contains the current template name. |
| 52 | */ |
| 53 | private Stack templateNameStack = new Stack(); |
| 54 | |
| 55 | /** |
| 56 | * EventCartridge we are to carry. Set by application |
| 57 | */ |
| 58 | private EventCartridge eventCartridge = null; |
| 59 | |
| 60 | /** |
| 61 | * Current resource - used for carrying encoding and other |
| 62 | * information down into the rendering process |
| 63 | */ |
| 64 | private Resource currentResource = null; |
| 65 | |
| 66 | /** |
| 67 | * set the current template name on top of stack |
| 68 | * |
| 69 | * @param s current template name |
| 70 | */ |
| 71 | public void pushCurrentTemplateName( String s ) |
| 72 | { |
| 73 | templateNameStack.push(s); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * remove the current template name from stack |
| 79 | */ |
| 80 | public void popCurrentTemplateName() |
| 81 | { |
| 82 | templateNameStack.pop(); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * get the current template name |
| 88 | * |
| 89 | * @return String current template name |
| 90 | */ |
| 91 | public String getCurrentTemplateName() |
| 92 | { |
| 93 | if ( templateNameStack.empty() ) |
| 94 | return "<undef>"; |
| 95 | else |
| 96 | return (String) templateNameStack.peek(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * get the current template name stack |
| 101 | * |
| 102 | * @return Object[] with the template name stack contents. |
| 103 | */ |
| 104 | public Object[] getTemplateNameStack() |
| 105 | { |
| 106 | return templateNameStack.toArray(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * returns an IntrospectionCache Data (@see IntrospectionCacheData) |
| 111 | * object if exists for the key |
| 112 | * |
| 113 | * @param key key to find in cache |
| 114 | * @return cache object |
| 115 | */ |
| 116 | public IntrospectionCacheData icacheGet( Object key ) |
| 117 | { |
| 118 | return ( IntrospectionCacheData ) introspectionCache.get( key ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * places an IntrospectionCache Data (@see IntrospectionCacheData) |
| 123 | * element in the cache for specified key |
| 124 | * |
| 125 | * @param key key |
| 126 | * @param o IntrospectionCacheData object to place in cache |
| 127 | */ |
| 128 | public void icachePut( Object key, IntrospectionCacheData o ) |
| 129 | { |
| 130 | introspectionCache.put( key, o ); |
| 131 | } |
| 132 | |
| 133 | public void setCurrentResource( Resource r ) |
| 134 | { |
| 135 | currentResource = r; |
| 136 | } |
| 137 | |
| 138 | public Resource getCurrentResource() |
| 139 | { |
| 140 | return currentResource; |
| 141 | } |
| 142 | |
| 143 | public EventCartridge attachEventCartridge( EventCartridge ec ) |
| 144 | { |
| 145 | EventCartridge temp = eventCartridge; |
| 146 | |
| 147 | eventCartridge = ec; |
| 148 | |
| 149 | return temp; |
| 150 | } |
| 151 | |
| 152 | public EventCartridge getEventCartridge() |
| 153 | { |
| 154 | return eventCartridge; |
| 155 | } |
| 156 | } |
| 157 | |