001: /*
002: * $Id: StrutsVelocityContext.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.velocity;
022:
023: import org.apache.velocity.VelocityContext;
024:
025: import com.opensymphony.xwork2.util.ValueStack;
026:
027: /**
028: */
029: public class StrutsVelocityContext extends VelocityContext {
030:
031: private static final long serialVersionUID = 8497212428904436963L;
032: ValueStack stack;
033: VelocityContext[] chainedContexts;
034:
035: public StrutsVelocityContext(ValueStack stack) {
036: this (null, stack);
037: }
038:
039: public StrutsVelocityContext(VelocityContext[] chainedContexts,
040: ValueStack stack) {
041: this .chainedContexts = chainedContexts;
042: this .stack = stack;
043: }
044:
045: public boolean internalContainsKey(Object key) {
046: boolean contains = super .internalContainsKey(key);
047:
048: // first let's check to see if we contain the requested key
049: if (contains) {
050: return true;
051: }
052:
053: // if not, let's search for the key in the ognl value stack
054: if (stack != null) {
055: Object o = stack.findValue(key.toString());
056:
057: if (o != null) {
058: return true;
059: }
060:
061: o = stack.getContext().get(key.toString());
062: if (o != null) {
063: return true;
064: }
065: }
066:
067: // if we still haven't found it, le's search through our chained contexts
068: if (chainedContexts != null) {
069: for (int index = 0; index < chainedContexts.length; index++) {
070: if (chainedContexts[index].containsKey(key)) {
071: return true;
072: }
073: }
074: }
075:
076: // nope, i guess it's really not here
077: return false;
078: }
079:
080: public Object internalGet(String key) {
081: // first, let's check to see if have the requested value
082: if (super .internalContainsKey(key)) {
083: return super .internalGet(key);
084: }
085:
086: // still no luck? let's look against the value stack
087: if (stack != null) {
088: Object object = stack.findValue(key);
089:
090: if (object != null) {
091: return object;
092: }
093:
094: object = stack.getContext().get(key);
095: if (object != null) {
096: return object;
097: }
098:
099: }
100:
101: // finally, if we're chained to other contexts, let's look in them
102: if (chainedContexts != null) {
103: for (int index = 0; index < chainedContexts.length; index++) {
104: if (chainedContexts[index].containsKey(key)) {
105: return chainedContexts[index].internalGet(key);
106: }
107: }
108: }
109:
110: // nope, i guess it's really not here
111: return null;
112: }
113: }
|