001: package org.apache.velocity.context;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.app.event.EventCartridge;
023: import org.apache.velocity.runtime.resource.Resource;
024: import org.apache.velocity.util.introspection.IntrospectionCacheData;
025:
026: /**
027: * This adapter class is the container for all context types for internal
028: * use. The AST now uses this class rather than the app-level Context
029: * interface to allow flexibility in the future.
030: *
031: * Currently, we have two context interfaces which must be supported :
032: * <ul>
033: * <li> Context : used for application/template data access
034: * <li> InternalHousekeepingContext : used for internal housekeeping and caching
035: * <li> InternalWrapperContext : used for getting root cache context and other
036: * such.
037: * <li> InternalEventContext : for event handling.
038: * </ul>
039: *
040: * This class implements the two interfaces to ensure that all methods are
041: * supported. When adding to the interfaces, or adding more context
042: * functionality, the interface is the primary definition, so alter that first
043: * and then all classes as necessary. As of this writing, this would be
044: * the only class affected by changes to InternalContext
045: *
046: * This class ensures that an InternalContextBase is available for internal
047: * use. If an application constructs their own Context-implementing
048: * object w/o subclassing AbstractContext, it may be that support for
049: * InternalContext is not available. Therefore, InternalContextAdapter will
050: * create an InternalContextBase if necessary for this support. Note that
051: * if this is necessary, internal information such as node-cache data will be
052: * lost from use to use of the context. This may or may not be important,
053: * depending upon application.
054: *
055: *
056: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
057: * @version $Id: InternalContextAdapterImpl.java 471908 2006-11-06 22:39:28Z henning $
058: */
059: public final class InternalContextAdapterImpl implements
060: InternalContextAdapter {
061: /**
062: * the user data Context that we are wrapping
063: */
064: Context context = null;
065:
066: /**
067: * the ICB we are wrapping. We may need to make one
068: * if the user data context implementation doesn't
069: * support one. The default AbstractContext-derived
070: * VelocityContext does, and it's recommended that
071: * people derive new contexts from AbstractContext
072: * rather than piecing things together
073: */
074: InternalHousekeepingContext icb = null;
075:
076: /**
077: * The InternalEventContext that we are wrapping. If
078: * the context passed to us doesn't support it, no
079: * biggie. We don't make it for them - since its a
080: * user context thing, nothing gained by making one
081: * for them now
082: */
083: InternalEventContext iec = null;
084:
085: /**
086: * CTOR takes a Context and wraps it, delegating all 'data' calls
087: * to it.
088: *
089: * For support of internal contexts, it will create an InternalContextBase
090: * if need be.
091: * @param c
092: */
093: public InternalContextAdapterImpl(Context c) {
094: context = c;
095:
096: if (!(c instanceof InternalHousekeepingContext)) {
097: icb = new InternalContextBase();
098: } else {
099: icb = (InternalHousekeepingContext) context;
100: }
101:
102: if (c instanceof InternalEventContext) {
103: iec = (InternalEventContext) context;
104: }
105: }
106:
107: /* --- InternalHousekeepingContext interface methods --- */
108:
109: /**
110: * @see org.apache.velocity.context.InternalHousekeepingContext#pushCurrentTemplateName(java.lang.String)
111: */
112: public void pushCurrentTemplateName(String s) {
113: icb.pushCurrentTemplateName(s);
114: }
115:
116: /**
117: * @see org.apache.velocity.context.InternalHousekeepingContext#popCurrentTemplateName()
118: */
119: public void popCurrentTemplateName() {
120: icb.popCurrentTemplateName();
121: }
122:
123: /**
124: * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentTemplateName()
125: */
126: public String getCurrentTemplateName() {
127: return icb.getCurrentTemplateName();
128: }
129:
130: /**
131: * @see org.apache.velocity.context.InternalHousekeepingContext#getTemplateNameStack()
132: */
133: public Object[] getTemplateNameStack() {
134: return icb.getTemplateNameStack();
135: }
136:
137: /**
138: * @see org.apache.velocity.context.InternalHousekeepingContext#icacheGet(java.lang.Object)
139: */
140: public IntrospectionCacheData icacheGet(Object key) {
141: return icb.icacheGet(key);
142: }
143:
144: /**
145: * @see org.apache.velocity.context.InternalHousekeepingContext#icachePut(java.lang.Object, org.apache.velocity.util.introspection.IntrospectionCacheData)
146: */
147: public void icachePut(Object key, IntrospectionCacheData o) {
148: icb.icachePut(key, o);
149: }
150:
151: /**
152: * @see org.apache.velocity.context.InternalHousekeepingContext#setCurrentResource(org.apache.velocity.runtime.resource.Resource)
153: */
154: public void setCurrentResource(Resource r) {
155: icb.setCurrentResource(r);
156: }
157:
158: /**
159: * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentResource()
160: */
161: public Resource getCurrentResource() {
162: return icb.getCurrentResource();
163: }
164:
165: /**
166: * @see org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
167: */
168: public boolean getAllowRendering() {
169: return icb.getAllowRendering();
170: }
171:
172: /**
173: * @see org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
174: */
175: public void setAllowRendering(boolean v) {
176: icb.setAllowRendering(v);
177: }
178:
179: /* --- Context interface methods --- */
180:
181: /**
182: * @see org.apache.velocity.context.Context#put(java.lang.String, java.lang.Object)
183: */
184: public Object put(String key, Object value) {
185: return context.put(key, value);
186: }
187:
188: /**
189: * @see InternalWrapperContext#localPut(String, Object)
190: */
191: public Object localPut(final String key, final Object value) {
192: return put(key, value);
193: }
194:
195: /**
196: * @see org.apache.velocity.context.Context#get(java.lang.String)
197: */
198: public Object get(String key) {
199: return context.get(key);
200: }
201:
202: /**
203: * @see org.apache.velocity.context.Context#containsKey(java.lang.Object)
204: */
205: public boolean containsKey(Object key) {
206: return context.containsKey(key);
207: }
208:
209: /**
210: * @see org.apache.velocity.context.Context#getKeys()
211: */
212: public Object[] getKeys() {
213: return context.getKeys();
214: }
215:
216: /**
217: * @see org.apache.velocity.context.Context#remove(java.lang.Object)
218: */
219: public Object remove(Object key) {
220: return context.remove(key);
221: }
222:
223: /* ---- InternalWrapperContext --- */
224:
225: /**
226: * returns the user data context that
227: * we are wrapping
228: * @return The internal user data context.
229: */
230: public Context getInternalUserContext() {
231: return context;
232: }
233:
234: /**
235: * Returns the base context that we are
236: * wrapping. Here, its this, but for other thing
237: * like VM related context contortions, it can
238: * be something else
239: * @return The base context.
240: */
241: public InternalContextAdapter getBaseContext() {
242: return this ;
243: }
244:
245: /* ----- InternalEventContext ---- */
246:
247: /**
248: * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
249: */
250: public EventCartridge attachEventCartridge(EventCartridge ec) {
251: if (iec != null) {
252: return iec.attachEventCartridge(ec);
253: }
254:
255: return null;
256: }
257:
258: /**
259: * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
260: */
261: public EventCartridge getEventCartridge() {
262: if (iec != null) {
263: return iec.getEventCartridge();
264: }
265:
266: return null;
267: }
268: }
|