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  
20  package org.apache.myfaces.view.facelets.tag;
21  
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.net.URL;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.Map;
28  import javax.el.ELException;
29  import javax.el.ValueExpression;
30  import javax.el.VariableMapper;
31  import javax.faces.FacesException;
32  import javax.faces.component.UIComponent;
33  import javax.faces.view.facelets.FaceletContext;
34  import javax.faces.view.facelets.FaceletException;
35  import javax.faces.view.facelets.TagAttribute;
36  import javax.faces.view.facelets.TagConfig;
37  import javax.faces.view.facelets.TagException;
38  import javax.faces.view.facelets.TagHandler;
39  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
40  import org.apache.myfaces.view.facelets.TemplateClient;
41  import org.apache.myfaces.view.facelets.el.VariableMapperWrapper;
42  import org.apache.myfaces.view.facelets.tag.ui.DefineHandler;
43  
44  /**
45   * A Tag that is specified in a FaceletFile. Takes all attributes specified and sets them on the FaceletContext before
46   * including the targeted Facelet file.
47   * 
48   * @author Jacob Hookom
49   * @version $Id: UserTagHandler.java,v 1.12 2008/07/13 19:01:35 rlubke Exp $
50   */
51  final class LegacyUserTagHandler extends TagHandler implements TemplateClient, ComponentContainerHandler
52  {
53  
54      protected final TagAttribute[] _vars;
55  
56      protected final URL _location;
57  
58      protected final Map<String, DefineHandler> _handlers;
59  
60      /**
61       * @param config
62       */
63      public LegacyUserTagHandler(TagConfig config, URL location)
64      {
65          super(config);
66          this._vars = this.tag.getAttributes().getAll();
67          this._location = location;
68          
69          Collection<DefineHandler> defines = TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class);
70          if (defines.isEmpty())
71          {
72              _handlers = null;
73          }
74          else
75          {
76              _handlers = new HashMap<String, DefineHandler>();
77              for (DefineHandler handler : defines)
78              {
79                  _handlers.put(handler.getName(), handler);
80              }
81          }
82      }
83  
84      /**
85       * Iterate over all TagAttributes and set them on the FaceletContext's VariableMapper, then include the target
86       * Facelet. Finally, replace the old VariableMapper.
87       * 
88       * @see TagAttribute#getValueExpression(FaceletContext, Class)
89       * @see VariableMapper
90       * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
91       */
92      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
93              ELException
94      {
95          VariableMapper orig = ctx.getVariableMapper();
96  
97          AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
98          // eval include
99          try
100         {
101             String[] names = null;
102             ValueExpression[] values = null;
103             if (this._vars.length > 0)
104             {
105                 names = new String[_vars.length];
106                 values = new ValueExpression[_vars.length];
107                 for (int i = 0; i < _vars.length; i++)
108                 {
109                     names[i] = _vars[i].getLocalName();
110                     values[i] = _vars[i].getValueExpression(ctx, Object.class);
111                 }
112             }
113             //actx.pushTemplateContext(new TemplateContextImpl());
114             actx.pushClient(this);
115             // setup a variable map
116             if (this._vars.length > 0)
117             {
118                 VariableMapper varMapper = new VariableMapperWrapper(orig);
119                 for (int i = 0; i < this._vars.length; i++)
120                 {
121                     varMapper.setVariable(names[i], values[i]);
122                 }
123                 ctx.setVariableMapper(varMapper);
124             }
125             actx.getTemplateContext().setAllowCacheELExpressions(false);
126 
127             ctx.includeFacelet(parent, this._location);
128         }
129         catch (FileNotFoundException e)
130         {
131             throw new TagException(this.tag, e.getMessage());
132         }
133         finally
134         {
135 
136             // make sure we undo our changes
137             actx.popClient(this);
138             //actx.popTemplateContext();
139             ctx.setVariableMapper(orig);
140         }
141     }
142 
143     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
144             FaceletException, ELException
145     {
146         if (name != null)
147         {
148             if (this._handlers == null)
149             {
150                 return false;
151             }
152             DefineHandler handler = (DefineHandler) this._handlers.get(name);
153             if (handler != null)
154             {
155                 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
156                 //TemplateContext itc = actx.popTemplateContext();
157                 try
158                 {
159                     handler.applyDefinition(ctx, parent);
160                 }
161                 finally
162                 {
163                     //actx.pushTemplateContext(itc);
164                 }
165                 return true;
166             }
167             else
168             {
169                 return false;
170             }
171         }
172         else
173         {
174             AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
175             //TemplateContext itc = actx.popTemplateContext();
176             try
177             {
178                 this.nextHandler.apply(ctx, parent);
179             }
180             finally
181             {
182                 //actx.pushTemplateContext(itc);
183             }
184             return true;
185         }
186     }
187 
188 }