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