001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.compiler.genmodel;
020:
021: import org.apache.beehive.netui.compiler.model.validation.ValidatorRule;
022: import org.apache.beehive.netui.compiler.model.validation.ValidatorRuleRange;
023: import org.apache.beehive.netui.compiler.model.validation.ValidatorConstants;
024: import org.apache.beehive.netui.compiler.CompilerUtils;
025: import org.apache.beehive.netui.compiler.JpfLanguageConstants;
026:
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.Iterator;
031:
032: // Constants
033: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
034: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
035: import org.apache.beehive.netui.compiler.typesystem.type.PrimitiveType;
036:
037: public class DefaultValidatorRuleFactory implements
038: ValidatorRuleFactory, ValidatorConstants, JpfLanguageConstants {
039: private static final Map VALIDATE_TYPE_RULES = new HashMap();
040:
041: static {
042: VALIDATE_TYPE_RULES.put(PrimitiveType.Kind.INT,
043: RULENAME_INTEGER);
044: VALIDATE_TYPE_RULES.put(PrimitiveType.Kind.FLOAT,
045: RULENAME_FLOAT);
046: VALIDATE_TYPE_RULES.put(PrimitiveType.Kind.LONG, RULENAME_LONG);
047: VALIDATE_TYPE_RULES.put(PrimitiveType.Kind.DOUBLE,
048: RULENAME_DOUBLE);
049: VALIDATE_TYPE_RULES.put(PrimitiveType.Kind.BYTE, RULENAME_BYTE);
050: VALIDATE_TYPE_RULES.put(PrimitiveType.Kind.SHORT,
051: RULENAME_SHORT);
052: }
053:
054: public ValidatorRule getFieldRule(String entityName,
055: String propertyName, AnnotationInstance ruleAnnotation) {
056: ValidatorRule rule = null;
057: String annName = CompilerUtils.getSimpleName(ruleAnnotation);
058:
059: if (annName.equals(VALIDATE_REQUIRED_TAG_NAME))
060: rule = new ValidatorRule(RULENAME_REQUIRED);
061: else if (annName.equals(VALIDATE_CREDIT_CARD_TAG_NAME))
062: rule = new ValidatorRule(RULENAME_CREDIT_CARD);
063: else if (annName.equals(VALIDATE_EMAIL_TAG_NAME))
064: rule = new ValidatorRule(RULENAME_EMAIL);
065: else if (annName.equals(VALIDATE_RANGE_TAG_NAME)) {
066: Double minFloat = CompilerUtils.getDouble(ruleAnnotation,
067: MIN_FLOAT_ATTR, true);
068:
069: if (minFloat != null) {
070: Double maxFloat = CompilerUtils.getDouble(
071: ruleAnnotation, MAX_FLOAT_ATTR, true);
072: assert maxFloat != null; // checker should catch this
073: rule = new ValidatorRuleRange(minFloat, maxFloat);
074: } else {
075: Long minLong = CompilerUtils.getLong(ruleAnnotation,
076: MIN_INT_ATTR, true);
077: Long maxLong = CompilerUtils.getLong(ruleAnnotation,
078: MAX_INT_ATTR, true);
079: assert minLong != null; // checker should catch this
080: assert maxLong != null; // checker should catch this
081: rule = new ValidatorRuleRange(minLong, maxLong);
082: }
083: } else if (annName.equals(VALIDATE_MIN_LENGTH_TAG_NAME)) {
084: Integer nChars = CompilerUtils.getInteger(ruleAnnotation,
085: CHARS_ATTR, true);
086: assert nChars != null;
087: rule = new ValidatorRule(RULENAME_MINLENGTH);
088: rule.setVar(VARNAME_MINLENGTH, nChars.toString());
089: } else if (annName.equals(VALIDATE_MAX_LENGTH_TAG_NAME)) {
090: Integer nChars = CompilerUtils.getInteger(ruleAnnotation,
091: CHARS_ATTR, true);
092: assert nChars != null;
093: rule = new ValidatorRule(RULENAME_MAXLENGTH);
094: rule.setVar(VARNAME_MAXLENGTH, nChars.toString());
095: } else if (annName.equals(VALIDATE_MASK_TAG_NAME)) {
096: String regex = CompilerUtils.getString(ruleAnnotation,
097: REGEX_ATTR, true);
098: assert regex != null;
099: rule = new ValidatorRule(RULENAME_MASK);
100: rule.setVar(VARNAME_MASK, regex);
101: } else if (annName.equals(VALIDATE_DATE_TAG_NAME)) {
102: boolean strict = CompilerUtils.getBoolean(ruleAnnotation,
103: STRICT_ATTR, false).booleanValue();
104: String pattern = CompilerUtils.getString(ruleAnnotation,
105: PATTERN_ATTR, true);
106: assert pattern != null;
107: rule = new ValidatorRule(RULENAME_DATE);
108: rule.setVar(strict ? VARNAME_DATE_PATTERN_STRICT
109: : VARNAME_DATE_PATTERN, pattern);
110: } else if (annName.equals(VALIDATE_TYPE_TAG_NAME)) {
111: AnnotationValue annotationValue = CompilerUtils
112: .getAnnotationValue(ruleAnnotation, TYPE_ATTR, true);
113: assert annotationValue != null;
114: Object value = annotationValue.getValue();
115: assert value instanceof PrimitiveType : value.getClass()
116: .getName(); // TODO: checker enforces this
117: String typeName = (String) VALIDATE_TYPE_RULES
118: .get(((PrimitiveType) value).getKind());
119: assert typeName != null : ((PrimitiveType) value).getKind()
120: .toString(); // TODO: checker enforces this
121: rule = new ValidatorRule(typeName);
122: } else if (annName.equals(VALIDATE_VALID_WHEN_TAG_NAME)) {
123: rule = new ValidatorRule(RULENAME_VALID_WHEN);
124: rule.setVar(VARNAME_VALID_WHEN, CompilerUtils.getString(
125: ruleAnnotation, CONDITION_ATTR, true));
126: } else if (annName.equals(VALIDATE_URL_TAG_NAME)) {
127: Boolean allowAllSchemes = CompilerUtils.getBoolean(
128: ruleAnnotation, ALLOW_ALL_SCHEMES_ATTR, true);
129: Boolean allowTwoSlashes = CompilerUtils.getBoolean(
130: ruleAnnotation, ALLOW_TWO_SLASHES_ATTR, true);
131: Boolean disallowFragments = CompilerUtils.getBoolean(
132: ruleAnnotation, DISALLOW_FRAGMENTS, true);
133: List schemes = CompilerUtils.getStringArray(ruleAnnotation,
134: SCHEMES_ATTR, true);
135:
136: rule = new ValidatorRule(RULENAME_URL);
137: if (allowAllSchemes != null) {
138: rule.setVar(VARNAME_ALLOW_ALL_SCHEMES, allowAllSchemes
139: .toString());
140: }
141: if (allowTwoSlashes != null) {
142: rule.setVar(VARNAME_ALLOW_TWO_SLASHES, allowTwoSlashes
143: .toString());
144: }
145: if (disallowFragments != null) {
146: rule.setVar(VARNAME_DISALLOW_FRAGMENTS,
147: disallowFragments.toString());
148: }
149: if (schemes != null) {
150: Iterator it = schemes.iterator();
151: StringBuffer schemesStr = new StringBuffer((String) it
152: .next());
153:
154: while (it.hasNext()) {
155: schemesStr.append(',').append(
156: ((String) it.next()).trim());
157: }
158: rule.setVar(VARNAME_SCHEMES, schemesStr.toString());
159: }
160: } else if (annName.equals(VALIDATE_CUSTOM_RULE_TAG_NAME)) {
161: String ruleName = CompilerUtils.getString(ruleAnnotation,
162: RULE_ATTR, false);
163: rule = new ValidatorRule(ruleName);
164: List ruleVars = CompilerUtils.getAnnotationArray(
165: ruleAnnotation, VARIABLES_ATTR, false);
166:
167: for (Iterator ii = ruleVars.iterator(); ii.hasNext();) {
168: AnnotationInstance ruleVar = (AnnotationInstance) ii
169: .next();
170: rule.setVar(CompilerUtils.getString(ruleVar, NAME_ATTR,
171: false), CompilerUtils.getString(ruleVar,
172: VALUE_ATTR, false));
173: }
174: }
175:
176: return rule;
177: }
178: }
|