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.util.HashMap;
22  import java.util.Map;
23  
24  import javax.el.ValueExpression;
25  import javax.el.VariableMapper;
26  
27  import org.apache.myfaces.view.facelets.PageContext;
28  import org.apache.myfaces.view.facelets.TemplateContext;
29  
30  /**
31   * Default instance of a VariableMapper backed by a Map
32   * 
33   * @see javax.el.VariableMapper
34   * @see javax.el.ValueExpression
35   * @see java.util.Map
36   * 
37   * @author Jacob Hookom
38   * @version $Id: DefaultVariableMapper.java 1187700 2011-10-22 12:19:37Z bommel $
39   */
40  public final class DefaultVariableMapper extends VariableMapperBase
41  {
42      private Map<String, ValueExpression> _vars;
43      
44      private PageContext _pageContext;
45      
46      private TemplateContext _templateContext;
47      
48      private VariableMapper _delegate;
49      
50      public boolean _trackResolveVariables;
51      
52      public boolean _variableResolved;
53  
54      public DefaultVariableMapper()
55      {
56          super();
57          _trackResolveVariables = false;
58          _variableResolved = false;
59      }
60  
61      public DefaultVariableMapper(VariableMapper delegate)
62      {
63          super();
64          this._delegate = delegate;
65      }
66      
67      /**
68       * @see javax.el.VariableMapper#resolveVariable(java.lang.String)
69       */
70      public ValueExpression resolveVariable(String name)
71      {
72          ValueExpression returnValue = null;
73          
74          if (_vars != null)
75          {
76              returnValue = _vars.get(name);
77          }
78  
79          //If the variable is not on the VariableMapper
80          if (returnValue == null)
81          {
82              //Check on page and template context
83              if (_pageContext != null && _pageContext.getAttributeCount() > 0)
84              {
85                  if (_pageContext.getAttributes().containsKey(name))
86                  {
87                      returnValue = _pageContext.getAttributes().get(name);
88                      if (_trackResolveVariables)
89                      {
90                          _variableResolved = true;
91                      }
92                      return returnValue;
93                  }
94              }
95              
96              if (_templateContext != null && !_templateContext.isParameterEmpty())
97              {
98                  if (_templateContext.getParameterMap().containsKey(name))
99                  {
100                     returnValue = _templateContext.getParameter(name);
101                     if (_trackResolveVariables)
102                     {
103                         _variableResolved = true;
104                     }
105                     return returnValue;
106                 }
107             }
108             
109             if (_delegate != null)
110             {
111                 returnValue = _delegate.resolveVariable(name);
112             }
113         }
114         else if (_trackResolveVariables)
115         {
116             // Is this code in a block that wants to cache 
117             // the resulting expression(s) and variable has been resolved?
118             _variableResolved = true;
119         }
120         
121         return returnValue;
122     }
123 
124     /**
125      * @see javax.el.VariableMapper#setVariable(java.lang.String, javax.el.ValueExpression)
126      */
127     public ValueExpression setVariable(String name, ValueExpression expression)
128     {
129         if (_vars == null)
130         {
131             _vars = new HashMap<String, ValueExpression>();
132         }
133         
134         return _vars.put(name, expression);
135     }
136     
137     /**
138      * Set the current page context this variable mapper should resolve against.
139      * 
140      * @param pageContext
141      */
142     public void setPageContext(PageContext pageContext)
143     {
144         this._pageContext = pageContext;
145     }
146     
147     /**
148      * Set the current template context this variable mapper should resolve against.
149      * 
150      * @param templateContext
151      */
152     public void setTemplateContext(TemplateContext templateContext)
153     {
154         this._templateContext = templateContext;
155     }
156 
157     @Override
158     public boolean isAnyFaceletsVariableResolved()
159     {
160         if (_trackResolveVariables)
161         {
162             return _variableResolved;
163         }
164         else
165         {
166             //Force expression creation
167             return true;
168         }
169     }
170 
171     @Override
172     public void beforeConstructELExpression()
173     {
174         _trackResolveVariables = true;
175         _variableResolved = false;
176     }
177 
178     @Override
179     public void afterConstructELExpression()
180     {
181         _trackResolveVariables = false;
182         _variableResolved = false;
183     }
184 }