View Javadoc

1   package org.apache.commons.jelly.tags.velocity;
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.Set;
20  import java.util.HashSet;
21  import java.util.HashMap;
22  
23  import org.apache.commons.jelly.JellyContext;
24  import org.apache.velocity.context.Context;
25  
26  /***
27   * Adapts a JellyContext for use as a Velocity Context.  This context
28   * can be used in either read-only or read-write mode.  When used as a
29   * read-only adapter, items <tt>put</tt> or <tt>remove</tt>ed from the
30   * Velocity context are not permitted to propogate to the JellyContext,
31   * which is the default behavior.  The adapter can also be used in a
32   * read-write mode.  This permits changes made by Velocity to propogate
33   * to the JellyContext.
34   *
35   * @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
36   * @version $Id: JellyContextAdapter.java 155420 2005-02-26 13:06:03Z dirkv $
37   */
38  public class JellyContextAdapter implements Context
39  {
40      /*** Flag to indicate read-only or read-write mode */
41      private boolean readOnly = true;
42  
43      /*** The JellyContext being adapted */
44      private JellyContext jellyContext;
45  
46      /*** The store for Velocity in the event the adpater is read-only */
47      private HashMap privateContext = new HashMap();
48  
49      /***
50       * Constructor.
51       *
52       * @param jellyContext The JellyContext to adapt
53       */
54      public JellyContextAdapter( JellyContext jellyContext )
55      {
56          this.jellyContext = jellyContext;
57      }
58  
59      /***
60       * Sets the read-only flag for this adapter.  If the read-only flag
61       * is set, changes to the Velocity Context will not be propogated to
62       * the JellyContext.  Turning the read-only flag off enables changes
63       * to propogate.
64       *
65       * @param readOnly If this parameter is <tt>true</tt>, the adapter
66       * becomes read-only.  Setting the parameter to <tt>false</tt> the
67       * adapter becomes read-write.
68       */
69      public void setReadOnly(boolean readOnly)
70      {
71          this.readOnly = readOnly;
72      }
73  
74      /***
75       * Tests if the adapter is read-only.
76       *
77       * @return <tt>true</tt> if the adpater is read-only; otherwise
78       * returns <tt>false</tt>.
79       */
80      public boolean isReadOnly()
81      {
82          return readOnly;
83      }
84  
85      public boolean containsKey( Object key )
86      {
87          if ( key == null )
88          {
89              return false;
90          }
91  
92          if ( readOnly && privateContext.containsKey( key ) )
93          {
94              return true;
95          }
96  
97          return jellyContext.getVariable( key.toString() ) != null ? true : false;
98      }
99  
100     public Object get( String key )
101     {
102         if ( key == null )
103         {
104             return null;
105         }
106 
107         if ( readOnly && privateContext.containsKey( key ) )
108         {
109             return privateContext.get( key );
110         }
111 
112         return jellyContext.getVariable( key );
113     }
114 
115     public Object[] getKeys()
116     {
117         Set keys = jellyContext.getVariables().keySet();
118 
119         if ( readOnly )
120         {
121             HashSet combinedKeys = new HashSet( keys );
122             combinedKeys.addAll( privateContext.keySet() );
123             keys = combinedKeys;
124         }
125 
126         return keys.toArray();
127     }
128 
129     public Object put( String key, Object value )
130     {
131         Object oldValue;
132 
133         if ( key == null || value == null )
134         {
135             return null;
136         }
137 
138         if ( readOnly )
139         {
140             oldValue = privateContext.put( key, value );
141         }
142         else
143         {
144             oldValue = jellyContext.getVariable( key );
145             jellyContext.setVariable( key, value );
146         }
147 
148         return oldValue;
149     }
150 
151     public Object remove( Object key )
152     {
153         Object oldValue;
154 
155         if ( key == null )
156         {
157             return null;
158         }
159 
160         if ( readOnly )
161         {
162             oldValue = privateContext.remove( key );
163         }
164         else
165         {
166             oldValue = jellyContext.getVariable( key.toString() );
167             jellyContext.removeVariable( key.toString() );
168         }
169 
170         return oldValue;
171     }
172 }
173