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 javax.faces.view.facelets.FaceletContext;
22  import javax.faces.view.facelets.MetaRule;
23  import javax.faces.view.facelets.Metadata;
24  import javax.faces.view.facelets.MetadataTarget;
25  import javax.faces.view.facelets.TagAttribute;
26  
27  final class _ValidatorRule extends MetaRule {
28  
29      final static class ValueExpressionMetadata extends Metadata {
30  
31          private final String name;
32  
33          private final TagAttribute attr;
34  
35          private final Class type;
36  
37          public ValueExpressionMetadata(String name, Class type,
38                  TagAttribute attr) {
39              this.name = name;
40              this.attr = attr;
41              this.type = type;
42          }
43  
44          public void applyMetadata(FaceletContext ctx, Object instance) {
45              ((ValidatorBase) instance).setValueExpression(this.name, this.attr
46                      .getValueExpression(ctx, this.type));
47          }
48  
49      }
50  
51      /*
52      final static class ValueBindingMetadata extends Metadata {
53  
54          private final String name;
55  
56          private final TagAttribute attr;
57  
58          private final Class type;
59  
60          public ValueBindingMetadata(String name, Class type, TagAttribute attr) {
61              this.name = name;
62              this.attr = attr;
63              this.type = type;
64          }
65  
66          public void applyMetadata(FaceletContext ctx, Object instance) {
67              ((ValidatorBase) instance).setValueBinding(this.name,
68                      new LegacyValueBinding(this.attr.getValueExpression(ctx,
69                              this.type)));
70          }
71  
72      }*/
73  
74      public final static _ValidatorRule Instance = new _ValidatorRule();
75  
76      public _ValidatorRule() {
77          super();
78      }
79  
80      public Metadata applyRule(String name, TagAttribute attribute,
81              MetadataTarget meta) {
82          if (meta.isTargetInstanceOf(ValidatorBase.class)) {
83  
84              // if component and dynamic, then must set expression
85              if (!attribute.isLiteral()) {
86                  Class type = meta.getPropertyType(name);
87                  if (type == null) {
88                      type = Object.class;
89                  }
90                  //if (FacesAPI.getComponentVersion(meta.getTargetClass()) >= 12) {
91                      return new ValueExpressionMetadata(name, type, attribute);
92                  //} else {
93                  //    return new ValueBindingMetadata(name, type, attribute);
94                  //}
95              }
96          }
97          return null;
98      }
99  }