StringValidator.java :  » J2EE » jfox » org » jfox » mvc » validate » Java Open Source

Java Open Source » J2EE » jfox 
jfox » org » jfox » mvc » validate » StringValidator.java
/*
 * JFox - The most lightweight Java EE Application Server!
 * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
 *
 * JFox is licenced and re-distributable under GNU LGPL.
 */
package org.jfox.mvc.validate;

import java.lang.annotation.Annotation;

/**
 * String validator
 *
 * @author <a href="mailto:jfox.young@gmail.com">Yang Yong</a>
 */
public class StringValidator implements Validator<String> {

    public String validate(String inputValue, Annotation validation) throws ValidateException {
        StringValidation stringValidation = (StringValidation)validation;
        if (inputValue == null || inputValue.trim().length() == 0) {
            if (!stringValidation.nullable()) {
                throw new ValidateException("input can not be null!", inputValue);
            }

        }
        else {
            if(inputValue.length() > stringValidation.maxLength() || inputValue.length() < stringValidation.minLength()) {
                throw new ValidateException("input length must between [" + stringValidation.minLength() + "," + stringValidation.maxLength() + "] !", inputValue);
            }
        }
        return inputValue;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.