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.ValueExpression;
28  import javax.faces.FacesWrapper;
29  import javax.faces.context.FacesContext;
30  import javax.faces.view.Location;
31  
32  /**
33   * A ValueExpression that contains the original ValueExpression and
34   * the Location of the facelet file from which the ValueExpression was
35   * created. This is needed when the current composite component (cc) 
36   * has to be resolved by the ValueExpression, because #{cc} refers to the
37   * composite component which is implemented in the file the ValueExpression
38   * comes from and not the one currently on top of the composite component stack.
39   * 
40   * This ValueExpression implementation passes through all methods to the delegate
41   * ValueExpression, but saves the related composite component in a FacesContext attribute 
42   * before the invocation of the method on the delegate and removes it afterwards. 
43   * 
44   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
45   * @version $Revision: 1351627 $ $Date: 2012-06-19 04:50:21 -0500 (Tue, 19 Jun 2012) $
46   */
47  public class LocationValueExpression extends ValueExpression
48      implements FacesWrapper<ValueExpression>, Externalizable
49  {
50      
51      private static final long serialVersionUID = -5636849184764526288L;
52      
53      // location and delegate need to be available in LocationValueExpressionUEL
54      Location location;
55      ValueExpression delegate;
56      int ccLevel;
57      
58      public LocationValueExpression()
59      {
60          super();
61      }
62      
63      public LocationValueExpression(Location location, ValueExpression delegate)
64      {
65          this.location = location;
66          this.delegate = delegate;
67          this.ccLevel = 0;
68      }
69  
70      public LocationValueExpression(Location location, ValueExpression delegate, int ccLevel)
71      {
72          this.location = location;
73          this.delegate = delegate;
74          this.ccLevel = ccLevel;
75      }
76      
77      public Location getLocation()
78      {
79          return location;
80      }
81      
82      public int getCCLevel()
83      {
84          return ccLevel;
85      }
86      
87      public LocationValueExpression apply(int newCCLevel)
88      {
89          if(this.ccLevel == newCCLevel)
90          {
91              return this;
92          }
93          else
94          {
95              return new LocationValueExpression(this.location, this.delegate, newCCLevel);
96          }
97      }
98      
99      @Override
100     public Class<?> getExpectedType()
101     {
102         return delegate.getExpectedType();
103     }
104 
105     @Override
106     public Class<?> getType(ELContext context)
107     {
108         FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
109         CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location, ccLevel);
110         try
111         {
112             return delegate.getType(context);
113         }
114         finally
115         {
116             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
117         }
118     }
119 
120     @Override
121     public Object getValue(ELContext context)
122     {
123         FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
124         CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location, ccLevel);
125         try
126         {
127             return delegate.getValue(context);
128         }
129         finally
130         {
131             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
132         }
133     }
134 
135     @Override
136     public boolean isReadOnly(ELContext context)
137     {
138         FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
139         CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location, ccLevel);
140         try
141         {
142             return delegate.isReadOnly(context);
143         }
144         finally
145         {
146             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
147         }
148     }
149 
150     @Override
151     public void setValue(ELContext context, Object value)
152     {
153         FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
154         CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location, ccLevel);
155         try
156         {
157             delegate.setValue(context, value);
158         }
159         finally
160         {
161             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
162         }
163     }
164 
165     @Override
166     public boolean equals(Object obj)
167     {
168         return delegate.equals(obj);
169     }
170 
171     @Override
172     public String getExpressionString()
173     {
174         return delegate.getExpressionString();
175     }
176 
177     @Override
178     public int hashCode()
179     {
180         return delegate.hashCode();
181     }
182 
183     @Override
184     public boolean isLiteralText()
185     {
186         return delegate.isLiteralText();
187     }
188 
189     public ValueExpression getWrapped()
190     {
191         return delegate;
192     }
193     
194     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
195     {
196         this.delegate = (ValueExpression) in.readObject();
197         this.location = (Location) in.readObject();
198         this.ccLevel = in.readInt();
199     }
200 
201     public void writeExternal(ObjectOutput out) throws IOException
202     {
203         out.writeObject(this.delegate);
204         out.writeObject(this.location);
205         out.writeInt(this.ccLevel);
206     }
207 }