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  
23  import javax.el.ELException;
24  import javax.faces.FacesException;
25  import javax.faces.component.UIComponent;
26  import javax.faces.view.facelets.FaceletContext;
27  import javax.faces.view.facelets.FaceletException;
28  import javax.faces.view.facelets.TagAttribute;
29  import javax.faces.view.facelets.TagAttributeException;
30  import javax.faces.view.facelets.TagConfig;
31  import javax.faces.view.facelets.TagHandler;
32  
33  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
34  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
35  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
36  import org.apache.myfaces.view.facelets.TemplateClient;
37  import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler;
38  
39  /**
40   * The insert tag is used within your templates to declare spots of replicable data.
41   * 
42   * @author Jacob Hookom
43   * @version $Id$
44   */
45  @JSFFaceletTag(name="ui:insert")
46  public final class InsertHandler extends TagHandler implements TemplateClient, ComponentContainerHandler
47  {
48  
49      /**
50       * The optional name attribute matches the associated <ui:define/> 
51       * tag in this template's client. If no name is specified, it's expected 
52       * that the whole template client will be inserted.
53       */
54      @JSFFaceletAttribute(
55              className="javax.el.ValueExpression",
56              deferredValueType="java.lang.String",
57              required=true)
58      private final String name;
59  
60      /**
61       * @param config
62       */
63      public InsertHandler(TagConfig config)
64      {
65          super(config);
66          TagAttribute attr = this.getAttribute("name");
67          if (attr != null)
68          {
69              if (!attr.isLiteral())
70              {
71                  throw new TagAttributeException(this.tag, attr, "Must be Literal");
72              }
73              this.name = attr.getValue();
74          }
75          else
76          {
77              this.name = null;
78          }
79      }
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
85       */
86      @Override
87      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
88              ELException
89      {
90          AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
91          actx.extendClient(this);
92          boolean found = false;
93          try
94          {
95              found = actx.includeDefinition(parent, this.name);
96          }
97          finally
98          {
99              actx.popExtendedClient(this);
100         }
101         if (!found)
102         {
103             this.nextHandler.apply(ctx, parent);
104         }
105     }
106 
107     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
108             FaceletException, ELException
109     {
110         if (this.name == name || this.name != null && this.name.equals(name))
111         {
112             this.nextHandler.apply(ctx, parent);
113             return true;
114         }
115         return false;
116     }
117 }