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