1 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 ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Iterator ; 30 31 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 VALIDATE_TYPE_RULES = 41 new HashMap (); 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 entityName, String propertyName, AnnotationInstance ruleAnnotation ) 54 { 55 ValidatorRule rule = null; 56 String 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 minFloat = CompilerUtils.getDouble( ruleAnnotation, MIN_FLOAT_ATTR, true ); 64 65 if ( minFloat != null ) 66 { 67 Double maxFloat = CompilerUtils.getDouble( ruleAnnotation, MAX_FLOAT_ATTR, true ); 68 assert maxFloat != null; rule = new ValidatorRuleRange( minFloat, maxFloat ); 70 } 71 else 72 { 73 Long minLong = CompilerUtils.getLong( ruleAnnotation, MIN_INT_ATTR, true ); 74 Long maxLong = CompilerUtils.getLong( ruleAnnotation, MAX_INT_ATTR, true ); 75 assert minLong != null; assert maxLong != null; rule = new ValidatorRuleRange( minLong, maxLong ); 78 } 79 } 80 else if ( annName.equals( VALIDATE_MIN_LENGTH_TAG_NAME ) ) 81 { 82 Integer 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 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 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 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 value = annotationValue.getValue(); 114 assert value instanceof PrimitiveType : value.getClass().getName(); String typeName = ( String ) VALIDATE_TYPE_RULES.get( ( ( PrimitiveType ) value ).getKind() ); 116 assert typeName != null : ( ( PrimitiveType ) value ).getKind().toString(); 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 ruleName = CompilerUtils.getString( ruleAnnotation, RULE_ATTR, false ); 127 rule = new ValidatorRule( ruleName ); 128 List ruleVars = CompilerUtils.getAnnotationArray( ruleAnnotation, VARIABLES_ATTR, false ); 129 130 for ( Iterator 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 |