KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > velocity > WebWorkVelocityContext


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.velocity;
6
7 import com.opensymphony.xwork.util.OgnlValueStack;
8 import org.apache.velocity.VelocityContext;
9
10
11 /**
12  * @author $Author: jcarreira $
13  * @version $Revision: 1.9 $
14  */

15 public class WebWorkVelocityContext extends VelocityContext {
16     //~ Instance fields ////////////////////////////////////////////////////////
17

18     OgnlValueStack stack;
19     VelocityContext[] chainedContexts;
20
21     //~ Constructors ///////////////////////////////////////////////////////////
22

23     public WebWorkVelocityContext(OgnlValueStack stack) {
24         this(null, stack);
25     }
26
27     public WebWorkVelocityContext(VelocityContext[] chainedContexts, OgnlValueStack stack) {
28         this.chainedContexts = chainedContexts;
29         this.stack = stack;
30     }
31
32     //~ Methods ////////////////////////////////////////////////////////////////
33

34     public boolean internalContainsKey(Object JavaDoc key) {
35         boolean contains = super.internalContainsKey(key);
36
37         // first let's check to see if we contain the requested key
38
if (contains) {
39             return true;
40         }
41
42         // if not, let's search for the key in the ognl value stack
43
if (stack != null) {
44             Object JavaDoc o = stack.findValue(key.toString());
45
46             if (o != null) {
47                 return true;
48             }
49         }
50
51         // if we still haven't found it, le's search through our chained contexts
52
if (chainedContexts != null) {
53             for (int index = 0; index < chainedContexts.length; index++) {
54                 if (chainedContexts[index].containsKey(key)) {
55                     return true;
56                 }
57             }
58         }
59
60         // nope, i guess it's really not here
61
return false;
62     }
63
64     public Object JavaDoc internalGet(String JavaDoc key) {
65         // first, let's check to see if have the requested value
66
if (super.internalContainsKey(key)) {
67             return super.internalGet(key);
68         }
69
70         // still no luck? let's look against the value stack
71
if (stack != null) {
72             Object JavaDoc object = stack.findValue(key);
73
74             if (object != null) {
75                 return object;
76             }
77         }
78
79         // finally, if we're chained to other contexts, let's look in them
80
if (chainedContexts != null) {
81             for (int index = 0; index < chainedContexts.length; index++) {
82                 if (chainedContexts[index].containsKey(key)) {
83                     return chainedContexts[index].internalGet(key);
84                 }
85             }
86         }
87
88         // nope, i guess it's really not here
89
return null;
90     }
91 }
92
Popular Tags