package com.icentris.util;
import java.util.*;
import java.io.Serializable;
// Comment_next_line_to_compile_with_Java_1.3
import java.util.regex.Pattern;
// Comment_next_line_to_compile_with_Java_1.3
import java.util.regex.PatternSyntaxException;
// Uncomment_next_line_to_compile_with_Java_1.3
//import fake.java.util.regex.Matcher;
// Uncomment_next_line_to_compile_with_Java_1.3
//import fake.java.util.regex.Pattern;
// Uncomment_next_line_to_compile_with_Java_1.3
//import fake.java.util.regex.PatternSyntaxException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.icentris.util.CharSequencer;
public class StringValidator implements Serializable {
private static final long serialVersionUID = 1L;
private static final Log logger = LogFactory.getLog(StringValidator.class);
protected String formatHint;
protected String[] regexps;
protected String[] validationErrors;
public String getFormatHint() {
return formatHint;
}
public void setFormatHint(String formatHint) {
this.formatHint = formatHint;
}
public String getRegexp() {
if ( regexps != null && regexps.length > 0 ) {
return regexps[0];
} else {
return null;
}
}
public void setRegexp(String regexp)
throws PatternSyntaxException
{
// compile this to make sure it's a valid regexp
Pattern.compile(regexp);
if ( regexps == null || regexps.length < 1 ) {
regexps = new String[1];
}
regexps[0] = regexp;
}
public String[] getRegexps() {
return regexps;
}
public void setRegexps(String[] regexps)
throws PatternSyntaxException
{
if ( regexps != null ) {
for ( int i=0; i < regexps.length; i++ ) {
Pattern.compile(regexps[i]);
}
}
this.regexps = regexps;
}
public void addRegexp(String regexp)
throws PatternSyntaxException
{
Pattern.compile(regexp);
List<String> regexpList;
if ( regexps == null ) {
regexpList = new ArrayList<String>();
} else {
regexpList = new ArrayList<String>( Arrays.asList(regexps) );
}
regexpList.add(regexp);
regexps = (String[]) regexpList.toArray(new String[0]);
}
public String getValidationError() {
if ( validationErrors != null && validationErrors.length > 0 ) {
return validationErrors[0];
} else {
return null;
}
}
public void setValidationError(String validationError) {
if ( validationErrors == null || validationErrors.length < 1 ) {
validationErrors = new String[1];
}
validationErrors[0] = validationError;
}
public String[] getValidationErrors() {
return validationErrors;
}
public void setValidationErrors(String[] validationErrors) {
this.validationErrors = validationErrors;
}
public void addValidationError(String validationError) {
List<String> validationErrorList;
if ( validationErrors == null ) {
validationErrorList = new ArrayList<String>();
} else {
validationErrorList = new ArrayList<String>( Arrays.asList(validationErrors) );
}
validationErrorList.add(validationError);
validationErrors = (String[]) validationErrorList.toArray(new String[0]);
}
// if this returns null, everything went well
public String[] validate(String userInput) {
// if no regexps have been set, everything is valid!
if ( regexps == null || regexps.length == 0 ) return null;
// can't match a null, so try empty string
if ( userInput == null ) userInput = "";
ArrayList<String> errors = new ArrayList<String>();
try {
CharSequencer charSequence = new CharSequencer(userInput);
for ( int i=0; i < regexps.length; i++ ) {
boolean shouldMatch = true;
String myReg=regexps[i];
if ( regexps[i] != null && regexps[i].startsWith("!") ) {
shouldMatch = false;
myReg = regexps[i].substring(1); // everything except the first char
}
Pattern re = Pattern.compile(myReg);
// if we this match fails
if (re.matcher(charSequence).find() != shouldMatch) {
// Validation failed: we didn't match and wanted to or did match and didn't want to
String errorMessage = validationErrors[i];
if ( errorMessage == null || errorMessage.length() == 0 ) {
if ( getFormatHint() != null && getFormatHint().length() > 0 ) {
errorMessage = "Your input: \"" + userInput + " must be formatted like " + getFormatHint();
} else {
errorMessage = "Your input: \"" + userInput + " is not valid.";
}
}
errors.add(errorMessage);
}
}
if ( errors.size() == 0 ) {
return null;
} else {
return (String[]) errors.toArray(new String[0]);
}
} catch (PatternSyntaxException e) {
throw new IllegalStateException("PatternSyntaxException:[" + e.getMessage() + " should have been thrown when setRegexp was called, so this should never happen!");
}
}
public static boolean validateOnce(String errorMessage, String regularExpression, String value) throws PatternSyntaxException {
StringValidator validator = new StringValidator();
if ( errorMessage != null ) validator.addValidationError(errorMessage);
if ( regularExpression != null ) validator.addRegexp(regularExpression);
String[] errors = validator.validate(value);
return errors == null || errors.length == 0;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("StringValidator:\n");
sb.append(" formatHint= [" + getFormatHint() + "]\n");
if ( regexps == null ) {
sb.append(" regexps= [" + regexps + "]\n");
} else {
sb.append(" regexps= [" + Arrays.asList(regexps) + "]\n");
}
if ( validationErrors == null ) {
sb.append(" validationErrors=[" + validationErrors + "]\n");
} else {
sb.append(" validationErrors=[" + Arrays.asList(validationErrors) + "]\n");
}
return sb.toString();
}
}