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