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.custom.date;
20  
21  import javax.faces.component.UIComponent;
22  
23  import org.apache.myfaces.custom.calendar.DateBusinessConverter;
24  import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
25  
26  import com.sun.facelets.FaceletContext;
27  import com.sun.facelets.el.LegacyValueBinding;
28  import com.sun.facelets.tag.MetaRule;
29  import com.sun.facelets.tag.MetaRuleset;
30  import com.sun.facelets.tag.Metadata;
31  import com.sun.facelets.tag.MetadataTarget;
32  import com.sun.facelets.tag.TagAttribute;
33  import com.sun.facelets.tag.jsf.ComponentConfig;
34  import com.sun.facelets.tag.jsf.html.HtmlComponentHandler;
35  
36  /**
37   * 
38   * @since 1.1.10
39   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
40   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (miƩ, 03 sep 2008) $
41   */
42  public class HtmlInputDateTagHandler extends HtmlComponentHandler
43  {
44  
45      public HtmlInputDateTagHandler(ComponentConfig config)
46      {
47          super(config);
48      }
49  
50      protected MetaRuleset createMetaRuleset(Class type)
51      {
52          MetaRuleset m = super.createMetaRuleset(type);
53  
54          m.addRule(DateBusinessConverterRule.INSTANCE);
55  
56          return m;
57      }
58  
59      public static class DateBusinessConverterRule extends MetaRule
60      {
61          public final static DateBusinessConverterRule INSTANCE = new DateBusinessConverterRule();
62  
63          final static class LiteralConverterMetadata extends Metadata
64          {
65  
66              private final Class dateBusinessConverterClass;
67  
68              public LiteralConverterMetadata(String dateBusinessConverterClass)
69              {
70                  try
71                  {
72                      this.dateBusinessConverterClass = ClassUtils
73                              .classForName(dateBusinessConverterClass);
74                  }
75                  catch(ClassNotFoundException e)
76                  {
77                      throw new IllegalArgumentException("Class referenced in calendarConverter not found: "+dateBusinessConverterClass);
78                  }
79                  catch(ClassCastException e)
80                  {
81                      throw new IllegalArgumentException("Class referenced in calendarConverter is not instance of org.apache.myfaces.custom.calendar.CalendarConverter: "+dateBusinessConverterClass);
82                  }
83              }
84  
85              public void applyMetadata(FaceletContext ctx, Object instance)
86              {
87                  ((AbstractHtmlInputDate) instance)
88                          .setDateBusinessConverter((DateBusinessConverter) ClassUtils
89                                  .newInstance(dateBusinessConverterClass));
90              }
91          }
92  
93          final static class DynamicConverterMetadata extends Metadata
94          {
95              private final TagAttribute attr;
96  
97              public DynamicConverterMetadata(TagAttribute attr)
98              {
99                  this.attr = attr;
100             }
101 
102             public void applyMetadata(FaceletContext ctx, Object instance)
103             {
104                 ((UIComponent) instance).setValueBinding("dateBusinessConverter",
105                         new LegacyValueBinding(attr.getValueExpression(ctx,
106                                 DateBusinessConverter.class)));
107             }
108         }
109 
110         public Metadata applyRule(String name, TagAttribute attribute,
111                 MetadataTarget meta)
112         {
113             if (meta.isTargetInstanceOf(AbstractHtmlInputDate.class))
114             {
115                 if ("dateBusinessConverter".equals(name)) {
116                     if (attribute.isLiteral()) {
117                         return new LiteralConverterMetadata(attribute.getValue());
118                     } else {
119                         return new DynamicConverterMetadata(attribute);
120                     }
121                 }
122             }
123             return null;
124         }
125     }
126 }