package com.ssm.util.sms.excel.validation.base;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ssm.util.sms.excel.exception.ValidationException;
import com.ssm.util.sms.excel.validation.Validation;

public class RegexValidation implements Validation {
	private Pattern pattern;
	private String regex;
	private String errorTip;

	public RegexValidation(String regex, String errorTip) {
		this.regex = regex;
		this.errorTip = errorTip;
	}

	public boolean validate(int rowIndex, Integer[] index, String[] value,
			StringBuffer message) throws ValidationException {
		if (this.pattern == null)
			this.pattern = Pattern.compile(this.regex);
		boolean result = true;
		for (int i = 0; i < value.length; i++) {
			if (value[i].length() != 0) {
				Matcher matcher = this.pattern.matcher(value[i]);
				if (!matcher.matches()) {
					message.append("第" + (rowIndex + 1) + "行"
							+ (this.errorTip != null ? this.errorTip : ""));
					result = false;
				}
			}
		}
		return result;
	}
}
