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.el;
20  
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  import java.io.ObjectOutput;
25  
26  import javax.el.ELContext;
27  import javax.el.ELException;
28  import javax.el.PropertyNotFoundException;
29  import javax.el.PropertyNotWritableException;
30  import javax.el.ValueExpression;
31  import javax.el.ValueReference;
32  import javax.faces.FacesWrapper;
33  import javax.faces.view.Location;
34  import javax.faces.view.facelets.TagAttribute;
35  
36  /**
37   * 
38   * 
39   * @author Jacob Hookom
40   * @version $Id$
41   */
42  public class ContextAwareTagValueExpression
43          extends ValueExpression
44          implements Externalizable, FacesWrapper<ValueExpression>, ContextAware
45  {
46  
47      private static final long serialVersionUID = 1L;
48  
49      private ValueExpression _wrapped; 
50      private Location _location;
51      private String _qName;
52  
53      public ContextAwareTagValueExpression()
54      {
55          super();
56      }
57  
58      public ContextAwareTagValueExpression(TagAttribute tagAttribute, ValueExpression valueExpression)
59      {
60          _location = tagAttribute.getLocation();
61          _qName = tagAttribute.getQName();
62          _wrapped = valueExpression;
63      }
64  
65      @Override
66      public Class<?> getExpectedType()
67      {
68          return _wrapped.getExpectedType();
69      }
70  
71      @Override
72      public Class<?> getType(ELContext context)
73      {
74          try
75          {
76              return _wrapped.getType(context);
77          }
78          catch (PropertyNotFoundException pnfe)
79          {
80              throw new ContextAwarePropertyNotFoundException(getLocation(), getLocalExpressionString(), getQName(),pnfe);
81          }
82          catch (ELException e)
83          {
84              throw new ContextAwareELException(getLocation(), getLocalExpressionString(), getQName(), e);
85          }
86      }
87  
88      @Override
89      public Object getValue(ELContext context)
90      {
91          try
92          {
93              return _wrapped.getValue(context);
94          }
95          catch (PropertyNotFoundException pnfe)
96          {
97              throw new ContextAwarePropertyNotFoundException(getLocation(), getLocalExpressionString(), getQName(),pnfe);
98          }
99          catch (ELException e)
100         {
101             throw new ContextAwareELException(getLocation(), getLocalExpressionString(), getQName(), e);
102         }
103         //Not necessary because NullPointerException by null context never occur and should not be wrapped
104         //catch (Exception e)
105         //{
106         //    throw new ContextAwareException(getLocation(), getLocalExpressionString(), getQName(), e);
107         //}
108     }
109     
110     private String getLocalExpressionString()
111     {
112         String expressionString = null;
113         try
114         {
115             expressionString = getExpressionString();
116         }
117         catch (Throwable t)
118         {
119             //swallo it because it is not important
120         }
121         return expressionString;
122     }
123 
124     @Override
125     public boolean isReadOnly(ELContext context)
126     {
127         try
128         {
129             return _wrapped.isReadOnly(context);
130         }
131         catch (PropertyNotFoundException pnfe)
132         {
133             throw new ContextAwarePropertyNotFoundException(getLocation(), getLocalExpressionString(), getQName(),pnfe);
134         }
135         catch (ELException e)
136         {
137             throw new ContextAwareELException(getLocation(), getLocalExpressionString(), getQName(), e);
138         }
139     }
140 
141     @Override
142     public void setValue(ELContext context, Object value)
143     {
144         try
145         {
146             _wrapped.setValue(context, value);
147         }
148         catch (PropertyNotFoundException pnfe)
149         {
150             throw new ContextAwarePropertyNotFoundException(getLocation(), getLocalExpressionString(), getQName(),pnfe);
151         }
152         catch (PropertyNotWritableException pnwe)
153         {
154             throw new ContextAwarePropertyNotWritableException(getLocation(), getLocalExpressionString(),
155                                                                getQName(), pnwe);
156         }
157         catch (ELException e)
158         {
159             throw new ContextAwareELException(getLocation(), getLocalExpressionString(), getQName(), e);
160         }
161     }
162     
163     @Override
164     public boolean equals(Object obj)
165     {
166         return _wrapped.equals(obj);
167     }
168 
169     @Override
170     public String getExpressionString()
171     {
172         return _wrapped.getExpressionString();
173     }
174 
175     @Override
176     public int hashCode()
177     {
178         return _wrapped.hashCode();
179     }
180 
181     @Override
182     public boolean isLiteralText()
183     {
184         return _wrapped.isLiteralText();
185     }
186 
187     @Override
188     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
189     {
190         _wrapped = (ValueExpression) in.readObject();
191         _location = (Location) in.readObject();
192         _qName = in.readUTF();
193     }
194 
195     @Override
196     public void writeExternal(ObjectOutput out) throws IOException
197     {
198         out.writeObject(_wrapped);
199         out.writeObject(_location);
200         out.writeUTF(_qName);
201     }
202 
203     @Override
204     public String toString()
205     {
206         return _location + ": " + _wrapped;
207     }
208 
209     @Override
210     public ValueExpression getWrapped()
211     {
212         return _wrapped;
213     }
214 
215     @Override
216     public Location getLocation()
217     {
218         return _location;
219     }
220     
221     @Override
222     public String getQName()
223     {
224         return _qName;
225     }
226     
227     @Override
228     public ValueReference getValueReference(ELContext context)
229     {
230         try
231         {
232             return getWrapped().getValueReference(context);
233         }
234         catch (PropertyNotFoundException pnfe)
235         {
236             throw new ContextAwarePropertyNotFoundException(getLocation(), getExpressionString(), getQName() ,  pnfe);
237         }
238         catch (ELException e)
239         {
240             throw new ContextAwareELException(getLocation(), getExpressionString(), getQName(),  e);
241         }
242     }
243 }