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: MappedValueExpression.java 1187701 2011-10-22 12:21:54Z bommel $
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 public Object getKey()
45 {
46 return key;
47 }
48
49 public Object getValue()
50 {
51 return src.get(key);
52 }
53
54 public Object setValue(Object value)
55 {
56 return src.put(key, value);
57 }
58
59 }
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 /**
71 *
72 */
73 public MappedValueExpression(ValueExpression orig, Map.Entry entry)
74 {
75 this.orig = orig;
76 this.key = entry.getKey();
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see javax.el.ValueExpression#getValue(javax.el.ELContext)
83 */
84 public Object getValue(ELContext context)
85 {
86 Object base = this.orig.getValue(context);
87 if (base != null)
88 {
89 context.setPropertyResolved(true);
90 return new Entry((Map) base, key);
91
92 }
93 return null;
94 }
95
96 /*
97 * (non-Javadoc)
98 *
99 * @see javax.el.ValueExpression#setValue(javax.el.ELContext, java.lang.Object)
100 */
101 public void setValue(ELContext context, Object value)
102 {
103 Object base = this.orig.getValue(context);
104 if (base != null)
105 {
106 context.setPropertyResolved(false);
107 context.getELResolver().setValue(context, base, key, value);
108 }
109 }
110
111 /*
112 * (non-Javadoc)
113 *
114 * @see javax.el.ValueExpression#isReadOnly(javax.el.ELContext)
115 */
116 public boolean isReadOnly(ELContext context)
117 {
118 Object base = this.orig.getValue(context);
119 if (base != null)
120 {
121 context.setPropertyResolved(false);
122 return context.getELResolver().isReadOnly(context, base, key);
123 }
124 return true;
125 }
126
127 /*
128 * (non-Javadoc)
129 *
130 * @see javax.el.ValueExpression#getType(javax.el.ELContext)
131 */
132 public Class getType(ELContext context)
133 {
134 Object base = this.orig.getValue(context);
135 if (base != null)
136 {
137 context.setPropertyResolved(false);
138 return context.getELResolver().getType(context, base, key);
139 }
140 return null;
141 }
142
143 /*
144 * (non-Javadoc)
145 *
146 * @see javax.el.ValueExpression#getExpectedType()
147 */
148 public Class getExpectedType()
149 {
150 return Object.class;
151 }
152
153 /*
154 * (non-Javadoc)
155 *
156 * @see javax.el.Expression#getExpressionString()
157 */
158 public String getExpressionString()
159 {
160 return this.orig.getExpressionString();
161 }
162
163 /*
164 * (non-Javadoc)
165 *
166 * @see javax.el.Expression#equals(java.lang.Object)
167 */
168 public boolean equals(Object obj)
169 {
170 return this.orig.equals(obj);
171 }
172
173 /*
174 * (non-Javadoc)
175 *
176 * @see javax.el.Expression#hashCode()
177 */
178 public int hashCode()
179 {
180 // TODO Auto-generated method stub
181 return 0;
182 }
183
184 /*
185 * (non-Javadoc)eturn new Map.Entry<K, V>
186 *
187 * @see javax.el.Expression#isLiteralText()
188 */
189 public boolean isLiteralText()
190 {
191 // TODO Auto-generated method stub
192 return false;
193 }
194
195 }