View Javadoc

1   /*
2    * $Id: StrutsVelocityContext.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 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  package org.apache.struts2.views.velocity;
19  
20  import org.apache.velocity.VelocityContext;
21  
22  import com.opensymphony.xwork2.util.ValueStack;
23  
24  
25  /***
26   */
27  public class StrutsVelocityContext extends VelocityContext {
28  
29      private static final long serialVersionUID = 8497212428904436963L;
30      ValueStack stack;
31      VelocityContext[] chainedContexts;
32  
33  
34      public StrutsVelocityContext(ValueStack stack) {
35          this(null, stack);
36      }
37  
38      public StrutsVelocityContext(VelocityContext[] chainedContexts, ValueStack stack) {
39          this.chainedContexts = chainedContexts;
40          this.stack = stack;
41      }
42  
43  
44      public boolean internalContainsKey(Object key) {
45          boolean contains = super.internalContainsKey(key);
46  
47          // first let's check to see if we contain the requested key
48          if (contains) {
49              return true;
50          }
51  
52          // if not, let's search for the key in the ognl value stack
53          if (stack != null) {
54              Object o = stack.findValue(key.toString());
55  
56              if (o != null) {
57                  return true;
58              }
59  
60              o = stack.getContext().get(key.toString());
61              if (o != null) {
62                  return true;
63              }
64          }
65  
66          // if we still haven't found it, le's search through our chained contexts
67          if (chainedContexts != null) {
68              for (int index = 0; index < chainedContexts.length; index++) {
69                  if (chainedContexts[index].containsKey(key)) {
70                      return true;
71                  }
72              }
73          }
74  
75          // nope, i guess it's really not here
76          return false;
77      }
78  
79      public Object internalGet(String key) {
80          // first, let's check to see if have the requested value
81          if (super.internalContainsKey(key)) {
82              return super.internalGet(key);
83          }
84  
85          // still no luck?  let's look against the value stack
86          if (stack != null) {
87              Object object = stack.findValue(key);
88  
89              if (object != null) {
90                  return object;
91              }
92  
93              object = stack.getContext().get(key);
94              if (object != null) {
95                  return object;
96              }
97  
98          }
99  
100         // finally, if we're chained to other contexts, let's look in them
101         if (chainedContexts != null) {
102             for (int index = 0; index < chainedContexts.length; index++) {
103                 if (chainedContexts[index].containsKey(key)) {
104                     return chainedContexts[index].internalGet(key);
105                 }
106             }
107         }
108 
109         // nope, i guess it's really not here
110         return null;
111     }
112 }