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