Java源码示例:org.apache.commons.validator.util.ValidatorUtils

示例1
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {

  Object result = null;
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  if ( GenericValidator.isBlankOrNull( value ) ) {
    return true;
  }

  result = GenericTypeValidator.formatInt( value );

  if ( result == null ) {
    ActionValidatorUtils.addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks,
      ActionValidatorUtils.getLevelOnFail( context, VALIDATOR_NAME ) );
    return false;
  }
  return true;

}
 
示例2
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {
  Object result = null;
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  if ( GenericValidator.isBlankOrNull( value ) ) {
    return Boolean.TRUE;
  }

  result = GenericTypeValidator.formatLong( value );

  if ( result == null ) {
    ActionValidatorUtils.addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks,
      ActionValidatorUtils.getLevelOnFail( context, VALIDATOR_NAME ) );
    return false;
  }
  return true;
}
 
示例3
/**
 * Replace the arg <code>Collection</code> key value with the key/value
 * pairs passed in.
 */
private void processArg(String key, String replaceValue) {
    for (int i = 0; i < this.args.length; i++) {

        Map<String, Arg> argMap = this.args[i];
        if (argMap == null) {
            continue;
        }

        Iterator<Arg> iter = argMap.values().iterator();
        while (iter.hasNext()) {
            Arg arg = iter.next();

            if (arg != null) {
                arg.setKey(
                        ValidatorUtils.replace(arg.getKey(), key, replaceValue));
            }
        }
    }
}
 
示例4
/**
 * Modifies the paramValue array with indexed fields.
 *
 * @param field
 * @param pos
 * @param paramValues
 */
private void handleIndexedField(Field field, int pos, Object[] paramValues)
    throws ValidatorException {

    int beanIndex = this.methodParameterList.indexOf(Validator.BEAN_PARAM);
    int fieldIndex = this.methodParameterList.indexOf(Validator.FIELD_PARAM);

    Object indexedList[] = field.getIndexedProperty(paramValues[beanIndex]);

    // Set current iteration object to the parameter array
    paramValues[beanIndex] = indexedList[pos];

    // Set field clone with the key modified to represent
    // the current field
    Field indexedField = (Field) field.clone();
    indexedField.setKey(
        ValidatorUtils.replace(
            indexedField.getKey(),
            Field.TOKEN_INDEXED,
            "[" + pos + "]"));

    paramValues[fieldIndex] = indexedField;
}
 
示例5
/**
 * Compares the two fields using the given comparator
 * 
 * @param bean
 * @param va
 * @param field
 * @param errors
 * @param request
 * @param comparator
 * @return
 */
private static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, Comparator comparator) {
    String greaterInputString = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String secondProperty = field.getVarValue("secondProperty");
    String lowerInputString = ValidatorUtils.getValueAsString(bean, secondProperty);

    if (!GenericValidator.isBlankOrNull(lowerInputString) && !GenericValidator.isBlankOrNull(greaterInputString)) {
        try {
            Double lowerInput = new Double(lowerInputString);
            Double greaterInput = new Double(greaterInputString);
            // if comparator result != VALUE then the condition is false
            if (comparator.compare(lowerInput, greaterInput) != VALUE) {
                errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            errors.add(field.getKey(), new ActionMessage(va.getMsg()));
            return false;
        }
    }
    return true;
}
 
示例6
public static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, ServletContext application) {

    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String sProperty2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (!value.equals(value2)) {
                errors.add(field.getKey(), new ActionMessage("errors.different.passwords"));
                return false;
            }
        } catch (Exception e) {
            errors.add(field.getKey(), new ActionMessage("errors.different.passwords"));
            return false;
        }
    }

    return true;
}
 
示例7
public boolean validate( CheckResultSourceInterface source, String propertyName,
  List<CheckResultInterface> remarks, ValidatorContext context ) {

  Object result = null;
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  if ( GenericValidator.isBlankOrNull( value ) ) {
    return true;
  }

  result = GenericTypeValidator.formatInt( value );

  if ( result == null ) {
    JobEntryValidatorUtils.addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks,
        JobEntryValidatorUtils.getLevelOnFail( context, VALIDATOR_NAME ) );
    return false;
  }
  return true;

}
 
示例8
public boolean validate( CheckResultSourceInterface source, String propertyName,
  List<CheckResultInterface> remarks, ValidatorContext context ) {
  Object result = null;
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  if ( GenericValidator.isBlankOrNull( value ) ) {
    return Boolean.TRUE;
  }

  result = GenericTypeValidator.formatLong( value );

  if ( result == null ) {
    JobEntryValidatorUtils.addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks,
        JobEntryValidatorUtils.getLevelOnFail( context, VALIDATOR_NAME ) );
    return false;
  }
  return true;
}
 
示例9
/**
 * Checks if the field is required.
 *
 * @return boolean If the field isn't <code>null</code> and
 * has a length greater than zero, <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 */
public static boolean validateRequired(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = !GenericValidator.isBlankOrNull(value);
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
示例10
public static boolean validateMaxLength(Object bean, Field field, ValidatorResults results, ValidatorAction action) {
	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
	if (value == null) {
		return true;
	}
	int max = Integer.parseInt(field.getVarValue("maxlength"));

	boolean valid = GenericValidator.maxLength(value, max);
	results.add(field, action.getName(), valid, value);
	return valid;
}
 
示例11
public static boolean validateIntRange(Object bean, Field field, ValidatorResults results, ValidatorAction action) {
	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
	if (value == null) {
		return true;
	}
	int intValue = Integer.parseInt(value);
	int min = Integer.parseInt(field.getVarValue("min"));
	int max = Integer.parseInt(field.getVarValue("max"));

	boolean valid = GenericValidator.isInRange(intValue, min, max);
	results.add(field, action.getName(), valid, value);
	return valid;
}
 
示例12
/**
 * Checks if field is positive assuming it is an integer
 * 
 * @param    value       The value validation is being performed on.
 * @param    field       Description of the field to be evaluated
 * @return   boolean     If the integer field is greater than zero, returns
 *                        true, otherwise returns false.
 */
public static boolean validatePositive(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = GenericTypeValidator.formatInt(value).intValue() > 0;
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
示例13
/**
   * Checks if the field value is in email format.
   *
   * @return boolean If the field isn't <code>null</code> and
   * has a length greater than zero, <code>true</code> is returned.  
   * Otherwise <code>false</code>.
   */
  public static boolean validateEmail(Object bean, Field field,
          ValidatorResults results, ValidatorAction action) {
  	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
if (value == null) {
	return true;
}

boolean valid = AgnitasEmailValidator.getInstance().isValid(value);

results.add(field, action.getName(), valid, value);
return valid;
  }
 
示例14
/**
 * Checks if the field value is in email format or if it is null
 *
 * @return if the field is null or has a valid email format - true is returned (false otherwise)
 */
public static boolean validateEmailOrNull(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    boolean valid = value == null || AgnitasEmailValidator.getInstance().isValid(value);
    results.add(field, action.getName(), valid, value);
    return valid;
}
 
示例15
/**
 * Checks if field is not negative assuming it is an integer
 * 
 * @param    value       The value validation is being performed on.
 * @param    field       Description of the field to be evaluated
 * @return   boolean     If the integer field is greater than or equals zero, returns
 *                        true, otherwise returns false.
 */
public static boolean validatePositiveOrZero(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = GenericTypeValidator.formatInt(value).intValue() >= 0;
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
示例16
public static boolean validateMailingType(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = MailingModel.mailingTypeMap.containsKey(value.toLowerCase());
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
示例17
public static boolean validateMailingFormat(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = MailingModel.formatMap.containsKey(value.toLowerCase());
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
示例18
public static boolean validateOnePixel(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = MailingModel.onePixelMap.containsKey(value.toLowerCase());
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
示例19
public static boolean validateTargetMode(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = MailingModel.targetModeMap.containsKey(value.toLowerCase());
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
示例20
public static boolean validateMaildropStatus(Object bean, Field field, ValidatorResults results, ValidatorAction action) {
	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

	boolean valid;
	try {
		MaildropStatus.fromName(value.toLowerCase());
		valid = true;
	} catch (Exception e) {
		valid = false;
	}
	results.add(field, action.getName(), valid, value);
	return valid;
}
 
示例21
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {

  String filename = ValidatorUtils.getValueAsString( source, propertyName );
  IVariables variables = getVariableSpace( source, propertyName, remarks, context );
  boolean failIfDoesNotExist = getFailIfDoesNotExist( source, propertyName, remarks, context );

  if ( null == variables ) {
    return false;
  }

  String realFileName = variables.environmentSubstitute( filename );
  FileObject fileObject = null;
  try {
    fileObject = HopVfs.getFileObject( realFileName );
    if ( fileObject == null || ( fileObject != null && !fileObject.exists() && failIfDoesNotExist ) ) {
      ActionValidatorUtils.addFailureRemark(
        source, propertyName, VALIDATOR_NAME, remarks, ActionValidatorUtils.getLevelOnFail(
          context, VALIDATOR_NAME ) );
      return false;
    }
    try {
      fileObject.close(); // Just being paranoid
    } catch ( IOException ignored ) {
      // Ignore close errors
    }
  } catch ( Exception e ) {
    ActionValidatorUtils.addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
  return true;
}
 
示例22
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {

  String filename = ValidatorUtils.getValueAsString( source, propertyName );
  IVariables variables = getVariableSpace( source, propertyName, remarks, context );
  boolean failIfExists = getFailIfExists( source, propertyName, remarks, context );

  if ( null == variables ) {
    return false;
  }

  String realFileName = variables.environmentSubstitute( filename );
  FileObject fileObject = null;
  try {
    fileObject = HopVfs.getFileObject( realFileName );

    if ( fileObject.exists() && failIfExists ) {
      ActionValidatorUtils.addFailureRemark(
        source, propertyName, VALIDATOR_NAME, remarks, ActionValidatorUtils.getLevelOnFail(
          context, VALIDATOR_NAME ) );
      return false;
    }
    try {
      fileObject.close(); // Just being paranoid
    } catch ( IOException ignored ) {
      // Ignore close errors
    }
  } catch ( Exception e ) {
    ActionValidatorUtils.addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
  return true;
}
 
示例23
/**
 * Fails if a field's value does not match the given mask.
 */
public static boolean validateMask( ICheckResultSource source, String propertyName, int levelOnFail,
                                    List<ICheckResult> remarks, String mask ) {
  final String VALIDATOR_NAME = "matches";
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  try {
    if ( null == mask ) {
      addGeneralRemark(
        source, propertyName, VALIDATOR_NAME, remarks, "errors.missingVar",
        ICheckResult.TYPE_RESULT_ERROR );
      return false;
    }

    if ( StringUtils.isNotBlank( value ) && !GenericValidator.matchRegexp( value, mask ) ) {
      addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks, levelOnFail );
      return false;
    } else {
      return true;
    }
  } catch ( Exception e ) {
    addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
}
 
示例24
public static void addOkRemark( ICheckResultSource source, String propertyName,
                                List<ICheckResult> remarks ) {
  final int SUBSTRING_LENGTH = 20;
  String value = ValidatorUtils.getValueAsString( source, propertyName );
  String substr = null;
  if ( value != null ) {
    substr = value.substring( 0, Math.min( SUBSTRING_LENGTH, value.length() ) );
    if ( value.length() > SUBSTRING_LENGTH ) {
      substr += "...";
    }
  }
  remarks.add( new CheckResult( ICheckResult.TYPE_RESULT_OK, ValidatorMessages.getString(
    "messages.passed", propertyName, substr ), source ) );
}
 
示例25
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {
  String value = ValidatorUtils.getValueAsString( source, propertyName );
  if ( GenericValidator.isBlankOrNull( value ) ) {
    ActionValidatorUtils.addFailureRemark(
      source, propertyName, VALIDATOR_NAME, remarks, ActionValidatorUtils.getLevelOnFail(
        context, VALIDATOR_NAME ) );
    return false;
  } else {
    return true;
  }
}
 
示例26
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  if ( !GenericValidator.isBlankOrNull( value ) && !GenericValidator.isEmail( value ) ) {
    ActionValidatorUtils.addFailureRemark(
      source, propertyName, VALIDATOR_NAME, remarks, ActionValidatorUtils.getLevelOnFail(
        context, VALIDATOR_NAME ) );
    return false;
  } else {
    return true;
  }
}
 
示例27
/**
 * Replace the vars value with the key/value pairs passed in.
 */
private void processVars(String key, String replaceValue) {
    Iterator<String> i = getVarMap().keySet().iterator();
    while (i.hasNext()) {
        String varKey = i.next();
        Var var = this.getVar(varKey);

        var.setValue(ValidatorUtils.replace(var.getValue(), key, replaceValue));
    }

}
 
示例28
/**
 * Replace the args key value with the key/value pairs passed in.
 */
private void processMessageComponents(String key, String replaceValue) {
    String varKey = TOKEN_START + TOKEN_VAR;
    // Process Messages
    if (key != null && !key.startsWith(varKey)) {
        for (Iterator<Msg> i = getMsgMap().values().iterator(); i.hasNext();) {
            Msg msg = i.next();
            msg.setKey(ValidatorUtils.replace(msg.getKey(), key, replaceValue));
        }
    }

    this.processArg(key, replaceValue);
}
 
示例29
/**
 * Creates and returns a copy of this object.
 * @return A copy of the Field.
 */
@Override
public Object clone() {
    Field field = null;
    try {
        field = (Field) super.clone();
    } catch(CloneNotSupportedException e) {
        throw new RuntimeException(e.toString());
    }

    @SuppressWarnings("unchecked") // empty array always OK; cannot check this at compile time
    final Map<String, Arg>[] tempMap = new Map[this.args.length];
    field.args = tempMap;
    for (int i = 0; i < this.args.length; i++) {
        if (this.args[i] == null) {
            continue;
        }

        Map<String, Arg> argMap = new HashMap<String, Arg>(this.args[i]);
        Iterator<Entry<String, Arg>> iter = argMap.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<String, Arg> entry = iter.next();
            String validatorName = entry.getKey();
            Arg arg = entry.getValue();
            argMap.put(validatorName, (Arg) arg.clone());
        }
        field.args[i] = argMap;
    }

    field.hVars = ValidatorUtils.copyFastHashMap(hVars);
    field.hMsgs = ValidatorUtils.copyFastHashMap(hMsgs);

    return field;
}
 
示例30
public static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, ServletContext application) {

    String valueString = ValidatorUtils.getValueAsString(bean, field.getProperty());

    String sProperty2 = ValidatorUtils.getValueAsString(bean, field.getVarValue("month"));
    String sProperty3 = ValidatorUtils.getValueAsString(bean, field.getVarValue("day"));

    if (((valueString == null) && (sProperty2 == null) && (sProperty3 == null))
            || ((valueString.length() == 0) && (sProperty2.length() == 0) && (sProperty3.length() == 0))) {
        // errors.add(field.getKey(),Resources.getActionError(request, va,
        // field));
        return true;
    }

    Integer year = null;
    Integer month = null;
    Integer day = null;

    try {
        year = new Integer(valueString);
        month = new Integer(sProperty2);
        day = new Integer(sProperty3);
    } catch (NumberFormatException e) {
        errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
        return false;
    }

    if (!GenericValidator.isBlankOrNull(valueString)) {
        if (!Data.validDate(day, month, year) || year == null || month == null || day == null || year.intValue() < 1
                || month.intValue() < 0 || day.intValue() < 1) {
            errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
        }

        return false;
    }

    return true;
}