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  import javax.el.ELContext;
26  import javax.el.ELException;
27  import javax.el.PropertyNotFoundException;
28  import javax.el.PropertyNotWritableException;
29  import javax.el.ValueExpression;
30  import javax.el.ValueReference;
31  import javax.faces.FacesWrapper;
32  import javax.faces.component.UIViewRoot;
33  import javax.faces.context.FacesContext;
34  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
35  import org.apache.myfaces.view.facelets.tag.jsf.FaceletState;
36  
37  /**
38   *
39   */
40  public class FaceletStateValueExpression extends ValueExpression 
41      implements Externalizable, FacesWrapper<ValueExpression>
42  {
43      private String uniqueId;
44      private String key;
45  
46      public FaceletStateValueExpression()
47      {
48      }
49      
50      public FaceletStateValueExpression(String uniqueId, String key)
51      {
52          this.uniqueId = uniqueId;
53          this.key = key;
54      }
55      
56      @Override
57      public ValueExpression getWrapped()
58      {
59          FacesContext facesContext = FacesContext.getCurrentInstance();
60          UIViewRoot root = facesContext.getViewRoot();
61          FaceletState map = (FaceletState) root.getAttributes().get(
62              ComponentSupport.FACELET_STATE_INSTANCE);
63          if (map == null)
64          {
65              map = (FaceletState)root.getTransientStateHelper().getTransient(
66                      ComponentSupport.FACELET_STATE_INSTANCE);
67          }
68          return map.getBinding(uniqueId, key);
69      }
70      
71      public ValueExpression getWrapped(ELContext context)
72      {
73          FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
74          if (facesContext == null)
75          {
76              facesContext = FacesContext.getCurrentInstance();
77          }
78          UIViewRoot root = facesContext.getViewRoot();
79          FaceletState map = (FaceletState) root.getAttributes().get(
80              ComponentSupport.FACELET_STATE_INSTANCE);
81          if (map == null)
82          {
83              map = (FaceletState)root.getTransientStateHelper().getTransient(
84                      ComponentSupport.FACELET_STATE_INSTANCE);
85          }
86          return map.getBinding(uniqueId, key);
87      }
88  
89  
90      @Override
91      public Class<?> getExpectedType()
92      {
93          return getWrapped().getExpectedType();
94      }
95  
96      @Override
97      public Class<?> getType(ELContext context) throws NullPointerException, PropertyNotFoundException, ELException
98      {
99          return getWrapped(context).getType(context);
100     }
101 
102     @Override
103     public boolean isReadOnly(ELContext context) throws NullPointerException, PropertyNotFoundException, ELException
104     {
105         return getWrapped(context).isReadOnly(context);
106     }
107 
108     @Override
109     public void setValue(ELContext context, Object value) 
110         throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException
111     {
112         getWrapped(context).setValue(context, value);
113     }
114 
115     @Override
116     public Object getValue(ELContext context) throws NullPointerException, PropertyNotFoundException, ELException
117     {
118         return getWrapped(context).getValue(context);
119     }
120 
121     @Override
122     public String getExpressionString()
123     {
124         return getWrapped().getExpressionString();
125     }
126 
127     @Override
128     public boolean isLiteralText()
129     {
130         return getWrapped().isLiteralText();
131     }
132 
133     @Override
134     public void writeExternal(ObjectOutput out) throws IOException
135     {
136         out.writeUTF(uniqueId);
137         out.writeUTF(key);
138     }
139 
140     @Override
141     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
142     {
143         uniqueId = (String) in.readUTF();
144         key = (String) in.readUTF();
145     }
146 
147     @Override
148     public int hashCode()
149     {
150         int hash = 7;
151         hash = 37 * hash + (this.uniqueId != null ? this.uniqueId.hashCode() : 0);
152         hash = 37 * hash + (this.key != null ? this.key.hashCode() : 0);
153         return hash;
154     }
155 
156     @Override
157     public boolean equals(Object obj)
158     {
159         if (obj == null)
160         {
161             return false;
162         }
163         if (getClass() != obj.getClass())
164         {
165             return false;
166         }
167         final FaceletStateValueExpression other = (FaceletStateValueExpression) obj;
168         if ((this.uniqueId == null) ? (other.uniqueId != null) : !this.uniqueId.equals(other.uniqueId))
169         {
170             return false;
171         }
172         if ((this.key == null) ? (other.key != null) : !this.key.equals(other.key))
173         {
174             return false;
175         }
176         return true;
177     }
178 
179     @Override
180     public ValueReference getValueReference(ELContext context)
181     {
182         return getWrapped(context).getValueReference(context);
183     }
184 }