KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > genmodel > DefaultValidatorRuleFactory


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.compiler.genmodel;
19
20 import org.apache.beehive.netui.compiler.model.validation.ValidatorRule;
21 import org.apache.beehive.netui.compiler.model.validation.ValidatorRuleRange;
22 import org.apache.beehive.netui.compiler.model.validation.ValidatorConstants;
23 import org.apache.beehive.netui.compiler.CompilerUtils;
24 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 // Constants
32
import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
33 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
34 import org.apache.beehive.netui.compiler.typesystem.type.PrimitiveType;
35
36
37 public class DefaultValidatorRuleFactory
38         implements ValidatorRuleFactory, ValidatorConstants, JpfLanguageConstants
39 {
40     private static final Map JavaDoc VALIDATE_TYPE_RULES =
41             new HashMap JavaDoc();
42     
43     static
44     {
45         VALIDATE_TYPE_RULES.put( PrimitiveType.Kind.INT, RULENAME_INTEGER );
46         VALIDATE_TYPE_RULES.put( PrimitiveType.Kind.FLOAT, RULENAME_FLOAT );
47         VALIDATE_TYPE_RULES.put( PrimitiveType.Kind.LONG, RULENAME_LONG );
48         VALIDATE_TYPE_RULES.put( PrimitiveType.Kind.DOUBLE, RULENAME_DOUBLE );
49         VALIDATE_TYPE_RULES.put( PrimitiveType.Kind.BYTE, RULENAME_BYTE );
50         VALIDATE_TYPE_RULES.put( PrimitiveType.Kind.SHORT, RULENAME_SHORT );
51     }
52     
53     public ValidatorRule getFieldRule( String JavaDoc entityName, String JavaDoc propertyName, AnnotationInstance ruleAnnotation )
54     {
55         ValidatorRule rule = null;
56         String JavaDoc annName = CompilerUtils.getSimpleName( ruleAnnotation );
57         
58         if ( annName.equals( VALIDATE_REQUIRED_TAG_NAME ) ) rule = new ValidatorRule( RULENAME_REQUIRED );
59         else if ( annName.equals( VALIDATE_CREDIT_CARD_TAG_NAME ) ) rule = new ValidatorRule( RULENAME_CREDIT_CARD );
60         else if ( annName.equals( VALIDATE_EMAIL_TAG_NAME ) ) rule = new ValidatorRule( RULENAME_EMAIL );
61         else if ( annName.equals( VALIDATE_RANGE_TAG_NAME ) )
62         {
63             Double JavaDoc minFloat = CompilerUtils.getDouble( ruleAnnotation, MIN_FLOAT_ATTR, true );
64             
65             if ( minFloat != null )
66             {
67                 Double JavaDoc maxFloat = CompilerUtils.getDouble( ruleAnnotation, MAX_FLOAT_ATTR, true );
68                 assert maxFloat != null; // checker should catch this
69
rule = new ValidatorRuleRange( minFloat, maxFloat );
70             }
71             else
72             {
73                 Long JavaDoc minLong = CompilerUtils.getLong( ruleAnnotation, MIN_INT_ATTR, true );
74                 Long JavaDoc maxLong = CompilerUtils.getLong( ruleAnnotation, MAX_INT_ATTR, true );
75                 assert minLong != null; // checker should catch this
76
assert maxLong != null; // checker should catch this
77
rule = new ValidatorRuleRange( minLong, maxLong );
78             }
79         }
80         else if ( annName.equals( VALIDATE_MIN_LENGTH_TAG_NAME ) )
81         {
82             Integer JavaDoc nChars = CompilerUtils.getInteger( ruleAnnotation, CHARS_ATTR, true );
83             assert nChars != null;
84             rule = new ValidatorRule( RULENAME_MINLENGTH );
85             rule.setVar( VARNAME_MINLENGTH, nChars.toString() );
86         }
87         else if ( annName.equals( VALIDATE_MAX_LENGTH_TAG_NAME ) )
88         {
89             Integer JavaDoc nChars = CompilerUtils.getInteger( ruleAnnotation, CHARS_ATTR, true );
90             assert nChars != null;
91             rule = new ValidatorRule( RULENAME_MAXLENGTH );
92             rule.setVar( VARNAME_MAXLENGTH, nChars.toString() );
93         }
94         else if ( annName.equals( VALIDATE_MASK_TAG_NAME ) )
95         {
96             String JavaDoc regex = CompilerUtils.getString( ruleAnnotation, REGEX_ATTR, true );
97             assert regex != null;
98             rule = new ValidatorRule( RULENAME_MASK );
99             rule.setVar( VARNAME_MASK, regex );
100         }
101         else if ( annName.equals( VALIDATE_DATE_TAG_NAME ) )
102         {
103             boolean strict = CompilerUtils.getBoolean( ruleAnnotation, STRICT_ATTR, false ).booleanValue();
104             String JavaDoc pattern = CompilerUtils.getString( ruleAnnotation, PATTERN_ATTR, true );
105             assert pattern != null;
106             rule = new ValidatorRule( RULENAME_DATE );
107             rule.setVar( strict ? VARNAME_DATE_PATTERN_STRICT : VARNAME_DATE_PATTERN, pattern );
108         }
109         else if ( annName.equals( VALIDATE_TYPE_TAG_NAME ) )
110         {
111             AnnotationValue annotationValue = CompilerUtils.getAnnotationValue( ruleAnnotation, TYPE_ATTR, true );
112             assert annotationValue != null;
113             Object JavaDoc value = annotationValue.getValue();
114             assert value instanceof PrimitiveType : value.getClass().getName(); // TODO: checker enforces this
115
String JavaDoc typeName = ( String JavaDoc ) VALIDATE_TYPE_RULES.get( ( ( PrimitiveType ) value ).getKind() );
116             assert typeName != null : ( ( PrimitiveType ) value ).getKind().toString(); // TODO: checker enforces this
117
rule = new ValidatorRule( typeName );
118         }
119         else if ( annName.equals( VALIDATE_VALID_WHEN_TAG_NAME ) )
120         {
121             rule = new ValidatorRule( RULENAME_VALID_WHEN );
122             rule.setVar( VARNAME_VALID_WHEN, CompilerUtils.getString( ruleAnnotation, CONDITION_ATTR, true ) );
123         }
124         else if ( annName.equals( VALIDATE_CUSTOM_RULE_TAG_NAME ) )
125         {
126             String JavaDoc ruleName = CompilerUtils.getString( ruleAnnotation, RULE_ATTR, false );
127             rule = new ValidatorRule( ruleName );
128             List JavaDoc ruleVars = CompilerUtils.getAnnotationArray( ruleAnnotation, VARIABLES_ATTR, false );
129             
130             for ( Iterator JavaDoc ii = ruleVars.iterator(); ii.hasNext(); )
131             {
132                 AnnotationInstance ruleVar = ( AnnotationInstance ) ii.next();
133                 rule.setVar( CompilerUtils.getString( ruleVar, NAME_ATTR, false ),
134                              CompilerUtils.getString( ruleVar, VALUE_ATTR, false ) );
135             }
136         }
137  
138         return rule;
139     }
140 }
141
Popular Tags