You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by dg...@apache.org on 2003/07/04 22:34:53 UTC

cvs commit: jakarta-struts/src/share/org/apache/struts/validator/validwhen ValidWhen.java

dgraham     2003/07/04 13:34:53

  Modified:    src/share/org/apache/struts/validator/validwhen
                        ValidWhen.java
  Log:
  Formatting and javadoc cleanup.
  
  Revision  Changes    Path
  1.5       +84 -66    jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhen.java
  
  Index: ValidWhen.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhen.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ValidWhen.java	1 Jul 2003 17:49:25 -0000	1.4
  +++ ValidWhen.java	4 Jul 2003 20:34:53 -0000	1.5
  @@ -1,7 +1,13 @@
   /*
  + * $Header$
  + * $Revision$
  + * $Date$
  + *
  + * ====================================================================
  + *
    *  The Apache Software License, Version 1.1
    *
  - *  Copyright (c) 1999 The Apache Software Foundation.  All rights
  + *  Copyright (c) 2003 The Apache Software Foundation.  All rights
    *  reserved.
    *
    *  Redistribution and use in source and binary forms, with or without
  @@ -54,36 +60,36 @@
   
   package org.apache.struts.validator.validwhen;
   
  -import org.apache.commons.validator.*;
  -import org.apache.struts.action.ActionErrors;
  -import org.apache.struts.action.ActionError;
  +import java.io.StringReader;
  +
   import javax.servlet.http.HttpServletRequest;
  +
  +import org.apache.commons.validator.Field;
  +import org.apache.commons.validator.Validator;
  +import org.apache.commons.validator.ValidatorAction;
  +import org.apache.commons.validator.ValidatorUtil;
  +import org.apache.struts.action.ActionErrors;
   import org.apache.struts.validator.Resources;
  -import java.io.StringReader;
   
   /**
  - *  <p>
  - *
  - *  This class contains the validwhen validation that is used in the validator-rules.xml
  - *  file.</p>
  - *
  + * This class contains the validwhen validation that is used in the 
  + * validator-rules.xml file.
    *
  - *@author     James Turner
  - *@since      Struts 1.1
  + * @author James Turner
  + * @since Struts 1.2
    */
  -
   public class ValidWhen {
  -  private static Class stringClass = new String().getClass();
  +    private static Class stringClass = new String().getClass();
   
  -  private static boolean isString(Object o) {
  -    if (o == null) return true;
  -    return (stringClass.isInstance(o));
  -  }
  +    private static boolean isString(Object o) {
  +        if (o == null)
  +            return true;
  +        return (stringClass.isInstance(o));
  +    }
   
       /**
  -     *  <p>
  -     *
  -     *  Checks if the field matches the boolean expression specified in <code>test</code> parameter</p>
  +     * Checks if the field matches the boolean expression specified in 
  +     * <code>test</code> parameter.
        *
        *@param  bean     The bean validation is being performed on.
        *@param  va       The <code>ValidatorAction</code> that is currently being performed.
  @@ -94,52 +100,64 @@
        *@param  request  Current request object.
        *@return          True if meets stated requirements, False otherwise
        */
  +    public static boolean validateValidWhen(
  +        Object bean,
  +        ValidatorAction va,
  +        Field field,
  +        ActionErrors errors,
  +        Validator validator,
  +        HttpServletRequest request) {
  +            
  +        Object form = validator.getResource(Validator.BEAN_KEY);
  +        String value = null;
  +        boolean valid = false;
  +        int index = -1;
  +        
  +        if (field.isIndexed()) {
  +            String key = field.getKey();
  +
  +            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
  +                index =
  +                    Integer.parseInt(
  +                        key.substring(key.indexOf("[") + 1, key.indexOf("]")));
  +            }
  +        }
  +        
  +        if (isString(bean)) {
  +            value = (String) bean;
  +        } else {
  +            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +        }
  +        
  +        String test = field.getVarValue("test");
  +        if (test == null) {
  +            return false;
  +        }
  +        
  +        ValidWhenLexer lexer = new ValidWhenLexer(new StringReader(test));
  +
  +        ValidWhenParser parser = new ValidWhenParser(lexer);
   
  -  public static boolean validateValidWhen(Object bean,
  -      ValidatorAction va,
  -      Field field,
  -      ActionErrors errors,
  -      Validator validator,
  -      HttpServletRequest request) {
  -    Object form = validator.getResource(Validator.BEAN_KEY);
  -    String value = null;
  -    boolean valid = false;
  -    int index = -1;
  -     if (field.isIndexed()) {
  -        String key = field.getKey();
  -
  -        if ((key.indexOf("[") > -1) &&
  -            (key.indexOf("]") > -1)) {
  -          index = Integer.parseInt(key.substring(key.indexOf("[")+1, key.indexOf("]")));
  +        parser.setForm(form);
  +        parser.setIndex(index);
  +        parser.setValue(value);
  +
  +        try {
  +            parser.expression();
  +            valid = parser.getResult();
  +            
  +        } catch (Exception ex) {
  +            ex.printStackTrace();
  +            errors.add(field.getKey(), Resources.getActionError(request, va, field));
  +            return false;
           }
  -      }
  -    if (isString(bean)) {
  -      value = (String) bean;
  -    } else {
  -      value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -    }
  -    String test = field.getVarValue("test");
  -    if (test == null) return false;
  -	ValidWhenLexer l = new ValidWhenLexer(new StringReader(test));
  -
  -       	ValidWhenParser p = new ValidWhenParser(l);
  -
  -	p.setForm(form);
  -	p.setIndex(index);
  -	p.setValue(value);
  -
  -	try {
  -	    p.expression();
  -	    valid = p.getResult();
  -    } catch  (Exception ex) {
  -	ex.printStackTrace();
  -        errors.add(field.getKey(), Resources.getActionError(request, va, field));
  -	return false;
  -    }
  -    if (!valid) {
  -        errors.add(field.getKey(), Resources.getActionError(request, va, field));
  -        return false;
  +        
  +        if (!valid) {
  +            errors.add(field.getKey(), Resources.getActionError(request, va, field));
  +            return false;
  +        }
  +        
  +        return true;
       }
  -    return true;
  -  }
  +    
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org