Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sun/facelets/tag/ui/DecorateHandler.java


1   /**
2    * Licensed under the Common Development and Distribution License,
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    * 
6    *   http://www.sun.com/cddl/
7    *   
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
11   * implied. See the License for the specific language governing
12   * permissions and limitations under the License.
13   */
14  
15  package com.sun.facelets.tag.ui;
16  
17  import java.io.IOException;
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  import javax.el.ELException;
27  import javax.el.VariableMapper;
28  import javax.faces.FacesException;
29  import javax.faces.component.UIComponent;
30  
31  import com.sun.facelets.FaceletContext;
32  import com.sun.facelets.FaceletException;
33  import com.sun.facelets.FaceletHandler;
34  import com.sun.facelets.TemplateClient;
35  import com.sun.facelets.el.VariableMapperWrapper;
36  import com.sun.facelets.tag.TagAttribute;
37  import com.sun.facelets.tag.TagConfig;
38  import com.sun.facelets.tag.TagHandler;
39  
40  /**
41   * @author Jacob Hookom
42   * @version $Id: DecorateHandler.java,v 1.11 2006/03/29 04:10:08 jhook Exp $
43   */
44  public final class DecorateHandler extends TagHandler implements TemplateClient {
45  
46      private final Logger log = Logger.getLogger("facelets.tag.ui.decorate");
47      
48      private final TagAttribute template;
49  
50      private final Map handlers;
51      
52      private final ParamHandler[] params;
53  
54      /**
55       * @param config
56       */
57      public DecorateHandler(TagConfig config) {
58          super(config);
59          this.template = this.getRequiredAttribute("template");
60          this.handlers = new HashMap();
61  
62          Iterator itr = this.findNextByType(DefineHandler.class);
63          DefineHandler d = null;
64          while (itr.hasNext()) {
65              d = (DefineHandler) itr.next();
66              this.handlers.put(d.getName(), d);
67              if (log.isLoggable(Level.FINE)) {
68                  log.fine(tag + " found Define[" + d.getName() + "]");
69              }
70          }
71          List paramC = new ArrayList();
72          itr = this.findNextByType(ParamHandler.class);
73          while (itr.hasNext()) {
74              paramC.add(itr.next());
75          }
76          if (paramC.size() > 0) {
77              this.params = new ParamHandler[paramC.size()];
78              for (int i = 0; i < this.params.length; i++) {
79                  this.params[i] = (ParamHandler) paramC.get(i);
80              }
81          } else {
82              this.params = null;
83          }
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
90       *      javax.faces.component.UIComponent)
91       */
92      public void apply(FaceletContext ctx, UIComponent parent)
93              throws IOException, FacesException, FaceletException, ELException {
94          VariableMapper orig = ctx.getVariableMapper();
95          if (this.params != null) {
96              VariableMapper vm = new VariableMapperWrapper(orig);
97              ctx.setVariableMapper(vm);
98              for (int i = 0; i < this.params.length; i++) {
99                  this.params[i].apply(ctx, parent);
100             }
101         }
102 
103         ctx.extendClient(this);
104         try {
105             ctx.includeFacelet(parent, this.template.getValue(ctx));
106         } finally {
107             ctx.setVariableMapper(orig);
108             ctx.popClient(this);
109         }
110     }
111 
112     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException, FaceletException, ELException {
113         if (name != null) {
114             FaceletHandler handler = (FaceletHandler) this.handlers.get(name);
115             if (handler != null) {
116                 handler.apply(ctx, parent);
117                 return true;
118             } else {
119                 return false;
120             }
121         } else {
122             this.nextHandler.apply(ctx, parent);
123             return false;
124         }
125     }
126 }