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.composite;
20  
21  import javax.el.MethodExpression;
22  import javax.faces.view.facelets.ComponentConfig;
23  import javax.faces.view.facelets.ComponentHandler;
24  import javax.faces.view.facelets.FaceletContext;
25  import javax.faces.view.facelets.MetaRule;
26  import javax.faces.view.facelets.MetaRuleset;
27  import javax.faces.view.facelets.Metadata;
28  import javax.faces.view.facelets.MetadataTarget;
29  import javax.faces.view.facelets.TagAttribute;
30  
31  public class SimpleComponentTagHandler extends ComponentHandler
32  {
33  
34      public SimpleComponentTagHandler(ComponentConfig config)
35      {
36          super(config);
37          // TODO Auto-generated constructor stub
38      }
39  
40      @Override
41      protected MetaRuleset createMetaRuleset(Class type)
42      {
43          return super.createMetaRuleset(type).addRule(new CustomMethodRule());
44      }
45  
46      public final class CustomMethodRule extends MetaRule {
47  
48          @Override
49          public Metadata applyRule(String name, TagAttribute attribute,
50                  MetadataTarget meta)
51          {
52              if (meta.isTargetInstanceOf(SimpleComponent.class))
53              {
54                  if ("customMethod".equals(name))
55                  {
56                      return new SimpleMethodMapper(attribute);
57                  }
58              }
59              return null;
60          }
61          
62      }
63      
64      final static class SimpleMethodMapper extends Metadata
65      {
66          private final TagAttribute _attr;
67          
68          public final static Class<?>[] CUSTOM_METHOD_SIG = new Class[]{String.class};
69  
70          public SimpleMethodMapper(TagAttribute attr)
71          {
72              this._attr = attr;
73          }
74  
75          public void applyMetadata(FaceletContext ctx, Object instance)
76          {
77              MethodExpression expr = _attr.getMethodExpression(ctx, null, CUSTOM_METHOD_SIG);
78              ((SimpleComponent) instance).setCustomMethod(expr);
79          }
80      }
81  
82  }