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