KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > VelocityContext


1 package org.apache.velocity;
2
3 /*
4  * Copyright 2000-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 JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.velocity.context.AbstractContext;
23 import org.apache.velocity.context.Context;
24
25 /**
26  * General purpose implemention of the application Context
27  * interface for general application use. This class should
28  * be used in place of the original Context class.
29  *
30  * This implementation uses a HashMap (@see java.util.HashMap )
31  * for data storage.
32  *
33  * This context implementation cannot be shared between threads
34  * without those threads synchronizing access between them, as
35  * the HashMap is not synchronized, nor are some of the fundamentals
36  * of AbstractContext. If you need to share a Context between
37  * threads with simultaneous access for some reason, please create
38  * your own and extend the interface Context
39  *
40  * @see org.apache.velocity.context.Context
41  *
42  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
43  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
44  * @author <a HREF="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
45  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
46  * @version $Id: VelocityContext.java,v 1.6.8.1 2004/03/03 22:27:36 geirm Exp $
47  */

48 public class VelocityContext extends AbstractContext implements Cloneable JavaDoc
49 {
50     /**
51      * Storage for key/value pairs.
52      */

53     private Map JavaDoc context = null;
54
55     /**
56      * Creates a new instance (with no inner context).
57      */

58     public VelocityContext()
59     {
60         this(null, null);
61     }
62
63     /**
64      * Creates a new instance with the provided storage (and no inner
65      * context).
66      */

67     public VelocityContext(Map JavaDoc context)
68     {
69         this(context, null);
70     }
71
72     /**
73      * Chaining constructor, used when you want to
74      * wrap a context in another. The inner context
75      * will be 'read only' - put() calls to the
76      * wrapping context will only effect the outermost
77      * context
78      *
79      * @param innerContext The <code>Context</code> implementation to
80      * wrap.
81      */

82     public VelocityContext( Context innerContext )
83     {
84         this(null, innerContext);
85     }
86
87     /**
88      * Initializes internal storage (never to <code>null</code>), and
89      * inner context.
90      *
91      * @param context Internal storage, or <code>null</code> to
92      * create default storage.
93      * @param innerContext Inner context.
94      */

95     public VelocityContext(Map JavaDoc context, Context innerContext)
96     {
97         super(innerContext);
98         this.context = (context == null ? new HashMap JavaDoc() : context);
99     }
100
101     /**
102      * retrieves value for key from internal
103      * storage
104      *
105      * @param key name of value to get
106      * @return value as object
107      */

108     public Object JavaDoc internalGet( String JavaDoc key )
109     {
110         return context.get( key );
111     }
112
113     /**
114      * stores the value for key to internal
115      * storage
116      *
117      * @param key name of value to store
118      * @param value value to store
119      * @return previous value of key as Object
120      */

121     public Object JavaDoc internalPut( String JavaDoc key, Object JavaDoc value )
122     {
123         return context.put( key, value );
124     }
125
126     /**
127      * determines if there is a value for the
128      * given key
129      *
130      * @param key name of value to check
131      * @return true if non-null value in store
132      */

133     public boolean internalContainsKey(Object JavaDoc key)
134     {
135         return context.containsKey( key );
136     }
137
138     /**
139      * returns array of keys
140      *
141      * @return keys as []
142      */

143     public Object JavaDoc[] internalGetKeys()
144     {
145         return context.keySet().toArray();
146     }
147     
148     /**
149      * remove a key/value pair from the
150      * internal storage
151      *
152      * @param key name of value to remove
153      * @return value removed
154      */

155     public Object JavaDoc internalRemove(Object JavaDoc key)
156     {
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 JavaDoc clone()
166     {
167         VelocityContext clone = null;
168         try
169         {
170             clone = (VelocityContext) super.clone();
171             clone.context = new HashMap JavaDoc(context);
172         }
173         catch (CloneNotSupportedException JavaDoc ignored)
174         {
175         }
176         return clone;
177     }
178 }
179
Popular Tags