View Javadoc

1   /*
2    * $Id: StrutsVelocityContext.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.views.velocity;
23  
24  import org.apache.velocity.VelocityContext;
25  
26  import com.opensymphony.xwork2.util.ValueStack;
27  
28  
29  /***
30   */
31  public class StrutsVelocityContext extends VelocityContext {
32  
33      private static final long serialVersionUID = 8497212428904436963L;
34      ValueStack stack;
35      VelocityContext[] chainedContexts;
36  
37  
38      public StrutsVelocityContext(ValueStack stack) {
39          this(null, stack);
40      }
41  
42      public StrutsVelocityContext(VelocityContext[] chainedContexts, ValueStack stack) {
43          this.chainedContexts = chainedContexts;
44          this.stack = stack;
45      }
46  
47  
48      public boolean internalContainsKey(Object key) {
49          boolean contains = super.internalContainsKey(key);
50  
51          // first let's check to see if we contain the requested key
52          if (contains) {
53              return true;
54          }
55  
56          // if not, let's search for the key in the ognl value stack
57          if (stack != null) {
58              Object o = stack.findValue(key.toString());
59  
60              if (o != null) {
61                  return true;
62              }
63  
64              o = stack.getContext().get(key.toString());
65              if (o != null) {
66                  return true;
67              }
68          }
69  
70          // if we still haven't found it, le's search through our chained contexts
71          if (chainedContexts != null) {
72              for (int index = 0; index < chainedContexts.length; index++) {
73                  if (chainedContexts[index].containsKey(key)) {
74                      return true;
75                  }
76              }
77          }
78  
79          // nope, i guess it's really not here
80          return false;
81      }
82  
83      public Object internalGet(String key) {
84          // first, let's check to see if have the requested value
85          if (super.internalContainsKey(key)) {
86              return super.internalGet(key);
87          }
88  
89          // still no luck?  let's look against the value stack
90          if (stack != null) {
91              Object object = stack.findValue(key);
92  
93              if (object != null) {
94                  return object;
95              }
96  
97              object = stack.getContext().get(key);
98              if (object != null) {
99                  return object;
100             }
101 
102         }
103 
104         // finally, if we're chained to other contexts, let's look in them
105         if (chainedContexts != null) {
106             for (int index = 0; index < chainedContexts.length; index++) {
107                 if (chainedContexts[index].containsKey(key)) {
108                     return chainedContexts[index].internalGet(key);
109                 }
110             }
111         }
112 
113         // nope, i guess it's really not here
114         return null;
115     }
116 }