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.tag.ui;
20  
21  import java.io.IOException;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  import javax.el.ELException;
29  import javax.faces.FacesException;
30  import javax.faces.component.UIComponent;
31  import javax.faces.view.facelets.FaceletContext;
32  import javax.faces.view.facelets.FaceletException;
33  import javax.faces.view.facelets.TagAttribute;
34  import javax.faces.view.facelets.TagConfig;
35  import javax.faces.view.facelets.TagHandler;
36  
37  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
38  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
39  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
40  import org.apache.myfaces.view.facelets.TemplateClient;
41  import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
42  
43  /**
44   * TODO: REFACTOR - This class could easily use a common parent with DecoratorHandler
45   * 
46   * @author Jacob Hookom
47   * @version $Id: CompositionHandler.java 1189343 2011-10-26 17:53:36Z struberg $
48   */
49  @JSFFaceletTag(name="ui:composition")
50  public final class CompositionHandler extends TagHandler implements TemplateClient
51  {
52  
53      //private static final Logger log = Logger.getLogger("facelets.tag.ui.composition");
54      private static final Logger log = Logger.getLogger(CompositionHandler.class.getName());
55  
56      public final static String NAME = "composition";
57  
58      /**
59       * The resolvable URI of the template to use. The content within the composition tag will 
60       * be used in populating the template specified.
61       */
62      @JSFFaceletAttribute(
63              name="template",
64              className="javax.el.ValueExpression",
65              deferredValueType="java.lang.String")
66      protected final TagAttribute _template;
67  
68      protected final Map<String, DefineHandler> _handlers;
69  
70      protected final ParamHandler[] _params;
71  
72      /**
73       * @param config
74       */
75      public CompositionHandler(TagConfig config)
76      {
77          super(config);
78          _template = getAttribute("template");
79          if (_template != null)
80          {
81              _handlers = new HashMap<String, DefineHandler>();
82              for (DefineHandler handler : TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class))
83              {
84                  _handlers.put(handler.getName(), handler);
85                  if (log.isLoggable(Level.FINE))
86                  {
87                      log.fine(tag + " found Define[" + handler.getName() + "]");
88                  }
89              }
90  
91              Collection<ParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, ParamHandler.class);
92              if (!params.isEmpty())
93              {
94                  int i = 0;
95                  _params = new ParamHandler[params.size()];
96                  for (ParamHandler handler : params)
97                  {
98                      _params[i++] = handler;
99                  }
100             }
101             else
102             {
103                 _params = null;
104             }
105         }
106         else
107         {
108             _params = null;
109             _handlers = null;
110         }
111     }
112 
113     /*
114      * (non-Javadoc)
115      * 
116      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext,
117      * javax.faces.component.UIComponent)
118      */
119     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
120             ELException
121     {
122         if (_template != null)
123         {
124             //VariableMapper orig = ctx.getVariableMapper();
125             //if (_params != null)
126             //{
127             //    VariableMapper vm = new VariableMapperWrapper(orig);
128             //    ctx.setVariableMapper(vm);
129             //    for (int i = 0; i < _params.length; i++)
130             //    {
131             //        _params[i].apply(ctx, parent);
132             //    }
133             //}
134             AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
135             actx.extendClient(this);
136             if (_params != null)
137             {
138                 //VariableMapper vm = new VariableMapperWrapper(orig);
139                 //ctx.setVariableMapper(vm);
140                 for (int i = 0; i < _params.length; i++)
141                 {
142                     _params[i].apply(ctx, parent);
143                 }
144             }
145 
146             try
147             {
148                 ctx.includeFacelet(parent, _template.getValue(ctx));
149             }
150             finally
151             {
152                 actx.popExtendedClient(this);
153                 //ctx.setVariableMapper(orig);
154             }
155         }
156         else
157         {
158             this.nextHandler.apply(ctx, parent);
159         }
160     }
161 
162     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
163             FaceletException, ELException
164     {
165         if (name != null)
166         {
167             if (_handlers == null)
168             {
169                 return false;
170             }
171             
172             DefineHandler handler = _handlers.get(name);
173             if (handler != null)
174             {
175                 handler.applyDefinition(ctx, parent);
176                 return true;
177             }
178             else
179             {
180                 return false;
181             }
182         }
183         else
184         {
185             this.nextHandler.apply(ctx, parent);
186             return true;
187         }
188     }
189 
190 }