001: package org.apache.velocity;
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 java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.velocity.context.AbstractContext;
026: import org.apache.velocity.context.Context;
027:
028: /**
029: * General purpose implemention of the application Context
030: * interface for general application use. This class should
031: * be used in place of the original Context class.
032: *
033: * This implementation uses a HashMap (@see java.util.HashMap )
034: * for data storage.
035: *
036: * This context implementation cannot be shared between threads
037: * without those threads synchronizing access between them, as
038: * the HashMap is not synchronized, nor are some of the fundamentals
039: * of AbstractContext. If you need to share a Context between
040: * threads with simultaneous access for some reason, please create
041: * your own and extend the interface Context
042: *
043: * @see org.apache.velocity.context.Context
044: *
045: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
046: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
047: * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
048: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
049: * @version $Id: VelocityContext.java 463298 2006-10-12 16:10:32Z henning $
050: */
051: public class VelocityContext extends AbstractContext implements
052: Cloneable {
053: /**
054: * Version Id for serializable
055: */
056: private static final long serialVersionUID = 9033846851064645037L;
057:
058: /**
059: * Storage for key/value pairs.
060: */
061: private Map context = null;
062:
063: /**
064: * Creates a new instance (with no inner context).
065: */
066: public VelocityContext() {
067: this (null, null);
068: }
069:
070: /**
071: * Creates a new instance with the provided storage (and no inner
072: * context).
073: * @param context
074: */
075: public VelocityContext(Map context) {
076: this (context, null);
077: }
078:
079: /**
080: * Chaining constructor, used when you want to
081: * wrap a context in another. The inner context
082: * will be 'read only' - put() calls to the
083: * wrapping context will only effect the outermost
084: * context
085: *
086: * @param innerContext The <code>Context</code> implementation to
087: * wrap.
088: */
089: public VelocityContext(Context innerContext) {
090: this (null, innerContext);
091: }
092:
093: /**
094: * Initializes internal storage (never to <code>null</code>), and
095: * inner context.
096: *
097: * @param context Internal storage, or <code>null</code> to
098: * create default storage.
099: * @param innerContext Inner context.
100: */
101: public VelocityContext(Map context, Context innerContext) {
102: super (innerContext);
103: this .context = (context == null ? new HashMap() : context);
104: }
105:
106: /**
107: * retrieves value for key from internal
108: * storage
109: *
110: * @param key name of value to get
111: * @return value as object
112: */
113: public Object internalGet(String key) {
114: return context.get(key);
115: }
116:
117: /**
118: * stores the value for key to internal
119: * storage
120: *
121: * @param key name of value to store
122: * @param value value to store
123: * @return previous value of key as Object
124: */
125: public Object internalPut(String key, Object value) {
126: return context.put(key, value);
127: }
128:
129: /**
130: * determines if there is a value for the
131: * given key
132: *
133: * @param key name of value to check
134: * @return true if non-null value in store
135: */
136: public boolean internalContainsKey(Object key) {
137: return context.containsKey(key);
138: }
139:
140: /**
141: * returns array of keys
142: *
143: * @return keys as []
144: */
145: public Object[] internalGetKeys() {
146: return context.keySet().toArray();
147: }
148:
149: /**
150: * remove a key/value pair from the
151: * internal storage
152: *
153: * @param key name of value to remove
154: * @return value removed
155: */
156: public Object internalRemove(Object key) {
157: return context.remove(key);
158: }
159:
160: /**
161: * Clones this context object.
162: *
163: * @return A deep copy of this <code>Context</code>.
164: */
165: public Object clone() {
166: VelocityContext clone = null;
167: try {
168: clone = (VelocityContext) super .clone();
169: clone.context = new HashMap(context);
170: } catch (CloneNotSupportedException ignored) {
171: }
172: return clone;
173: }
174: }
|