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.commons.validator;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.text.ParseException;
24  
25  import javax.faces.view.facelets.FaceletContext;
26  import javax.faces.view.facelets.MetaRule;
27  import javax.faces.view.facelets.Metadata;
28  import javax.faces.view.facelets.MetadataTarget;
29  import javax.faces.view.facelets.TagAttribute;
30  import javax.faces.view.facelets.TagAttributeException;
31  
32  
33  
34  public class _DateRestrictionRule extends MetaRule
35  {
36      final static class DateRestrictionMetadata extends Metadata {
37  
38          private final String name;
39  
40          private final TagAttribute attr;
41  
42          private final Method method;
43          
44          private final Class type;
45          
46          private Object[] value;
47  
48          public DateRestrictionMetadata(String name, Method method, Class type,
49                  TagAttribute attr) {
50              this.name = name;
51              this.attr = attr;
52              this.method = method;
53              this.type = type;
54          }
55          
56          @Override
57          public void applyMetadata(FaceletContext ctx, Object instance)
58          {
59              if (this.attr.isLiteral())
60              {
61                  try
62                  {
63                      if (value == null) {
64                          value = new Object[] { 
65                                  org.apache.myfaces.commons.util.TagUtils.getStringArray(
66                                          ctx.getExpressionFactory().coerceToType(
67                                                  this.attr.getValue(), 
68                                                  String.class)
69                                  ) };
70                      }
71                      try {
72                          method.invoke(instance, this.value);
73                      } catch (InvocationTargetException e) {
74                          throw new TagAttributeException(this.attr, e.getCause());
75                      } catch (Exception e) {
76                          throw new TagAttributeException(this.attr, e);
77                      }
78                  }
79                  catch(ParseException e)
80                  {
81                      throw new TagAttributeException(this.attr, e);
82                  }
83              }
84              else
85              {
86                  ((ValidatorBase) instance).setValueExpression(this.name, this.attr
87                          .getValueExpression(ctx, this.type));
88              }
89          }
90          
91      }
92      
93      public final static _DateRestrictionRule Instance = new _DateRestrictionRule();
94      
95      public _DateRestrictionRule()
96      {
97          super();
98      }
99  
100     @Override
101     public Metadata applyRule(String name, TagAttribute attribute,
102             MetadataTarget meta)
103     {
104         if (meta.isTargetInstanceOf(DateRestrictionValidator.class)) {
105             
106             if ("invalidMonths".equals(name) || "invalidDaysOfWeek".equals(name))
107             {
108                 Method m = meta.getWriteMethod(name);
109                 Class type = meta.getPropertyType(name);
110                 if (type == null) {
111                     type = Object.class;
112                 }
113                 return new DateRestrictionMetadata(name, m, type, attribute);
114             }
115         }
116         return null;
117     }
118 
119 }
120