View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag.jstl.core;
20  
21  import java.io.Serializable;
22  import java.util.Map;
23  
24  import javax.el.ELContext;
25  import javax.el.ValueExpression;
26  
27  /**
28   * @author Jacob Hookom
29   * @version $Id$
30   */
31  public final class MappedValueExpression extends ValueExpression
32  {
33      private final static class Entry implements Map.Entry, Serializable
34      {
35          private final Map src;
36          private final Object key;
37  
38          public Entry(Map src, Object key)
39          {
40              this.src = src;
41              this.key = key;
42          }
43  
44          @Override
45          public Object getKey()
46          {
47              return key;
48          }
49  
50          @Override
51          public Object getValue()
52          {
53              return src.get(key);
54          }
55  
56          @Override
57          public Object setValue(Object value)
58          {
59              return src.put(key, value);
60          }
61  
62      }
63  
64      private static final long serialVersionUID = 1L;
65  
66      private final Object key;
67  
68      private final ValueExpression orig;
69  
70      public MappedValueExpression(ValueExpression orig, Map.Entry entry)
71      {
72          this.orig = orig;
73          this.key = entry.getKey();
74      }
75  
76      @Override
77      public Object getValue(ELContext context)
78      {
79          Object base = this.orig.getValue(context);
80          if (base != null)
81          {
82              context.setPropertyResolved(true);
83              return new Entry((Map) base, key);
84  
85          }
86          return null;
87      }
88  
89      @Override
90      public void setValue(ELContext context, Object value)
91      {
92          Object base = this.orig.getValue(context);
93          if (base != null)
94          {
95              context.setPropertyResolved(false);
96              context.getELResolver().setValue(context, base, key, value);
97          }
98      }
99  
100     @Override
101     public boolean isReadOnly(ELContext context)
102     {
103         Object base = this.orig.getValue(context);
104         if (base != null)
105         {
106             context.setPropertyResolved(false);
107             return context.getELResolver().isReadOnly(context, base, key);
108         }
109         return true;
110     }
111 
112     @Override
113     public Class getType(ELContext context)
114     {
115         Object base = this.orig.getValue(context);
116         if (base != null)
117         {
118             context.setPropertyResolved(false);
119             return context.getELResolver().getType(context, base, key);
120         }
121         return null;
122     }
123 
124     @Override
125     public Class getExpectedType()
126     {
127         return Object.class;
128     }
129 
130     @Override
131     public String getExpressionString()
132     {
133         return this.orig.getExpressionString();
134     }
135 
136     @Override
137     public boolean equals(Object obj)
138     {
139         return this.orig.equals(obj);
140     }
141 
142     @Override
143     public int hashCode()
144     {
145         return 0;
146     }
147 
148     @Override
149     public boolean isLiteralText()
150     {
151         return false;
152     }
153 
154 }