1 16 package org.apache.cocoon.forms.datatype.validationruleimpl; 17 18 import org.apache.cocoon.forms.FormsConstants; 19 import org.apache.cocoon.forms.util.I18nMessage; 20 import org.apache.cocoon.forms.validation.ValidationError; 21 import org.outerj.expression.ExpressionContext; 22 23 33 public class Mod10ValidationRule extends AbstractValidationRule { 34 public ValidationError validate(Object value, ExpressionContext expressionContext) { 35 String numberToCheck = (String )value; 36 int nulOffset = '0'; 37 int sum = 0; 38 for (int i = 1; i <= numberToCheck.length(); i++) { 39 int currentDigit = numberToCheck.charAt(numberToCheck.length() - i) - nulOffset; 40 if ((i % 2) == 0) { 41 currentDigit *= 2; 42 currentDigit = currentDigit > 9 ? currentDigit - 9 : currentDigit; 43 sum += currentDigit; 44 } else { 45 sum += currentDigit; 46 } 47 } 48 if(!((sum % 10) == 0)) 49 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.mod10", FormsConstants.I18N_CATALOGUE)); 50 else 51 return null; 52 } 53 54 public boolean supportsType(Class clazz, boolean arrayType) { 55 return String .class.isAssignableFrom(clazz) && !arrayType; 56 } 57 } 58 | Popular Tags |