View Javadoc

1   /*
2    * $Id: ValueStackShadowMap.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.jasperreports;
23  
24  import com.opensymphony.xwork2.util.ValueStack;
25  
26  import java.util.HashMap;
27  import java.util.Set;
28  
29  
30  /***
31   * Ported to Struts:
32   *
33   */
34  public class ValueStackShadowMap extends HashMap {
35  
36      private static final long serialVersionUID = -167109778490907240L;
37  
38      /***
39       * valueStack reference
40       */
41      ValueStack valueStack;
42  
43      /***
44       * entries reference
45       */
46      Set entries;
47  
48  
49      /***
50       * Constructs an instance of ValueStackShadowMap.
51       *
52       * @param valueStack - the underlying valuestack
53       */
54      public ValueStackShadowMap(ValueStack valueStack) {
55          this.valueStack = valueStack;
56      }
57  
58  
59      /***
60       * Implementation of containsKey(), overriding HashMap implementation.
61       *
62       * @param key - The key to check in HashMap and if not found to check on valueStack.
63       * @return <tt>true</tt>, if conatins key, <tt>false</tt> otherwise.
64       * @see java.util.HashMap#containsKey
65       */
66      public boolean containsKey(Object key) {
67          boolean hasKey = super.containsKey(key);
68  
69          if (!hasKey) {
70              if (valueStack.findValue((String) key) != null) {
71                  hasKey = true;
72              }
73          }
74  
75          return hasKey;
76      }
77  
78      /***
79       * Implementation of get(), overriding HashMap implementation.
80       *
81       * @param key - The key to get in HashMap and if not found there from the valueStack.
82       * @return value - The object from HashMap or if null, from the valueStack.
83       * @see java.util.HashMap#get
84       */
85      public Object get(Object key) {
86          Object value = super.get(key);
87  
88          if ((value == null) && key instanceof String) {
89              value = valueStack.findValue((String) key);
90          }
91  
92          return value;
93      }
94  }