manveen     02/05/04 12:46:57

  Modified:    webapps/admin/WEB-INF/classes/org/apache/webapp/admin/resources
                        EnvEntryForm.java
  Log:
  Added validation check that the value entered must match entry type chosen.
  
  Revision  Changes    Path
  1.2       +68 -11    
jakarta-tomcat-4.0/webapps/admin/WEB-INF/classes/org/apache/webapp/admin/resources/EnvEntryForm.java
  
  Index: EnvEntryForm.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/webapps/admin/WEB-INF/classes/org/apache/webapp/admin/resources/EnvEntryForm.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EnvEntryForm.java 4 May 2002 14:40:09 -0000       1.1
  +++ EnvEntryForm.java 4 May 2002 19:46:57 -0000       1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/webapps/admin/WEB-INF/classes/org/apache/webapp/admin/resources/EnvEntryForm.java,v
 1.1 2002/05/04 14:40:09 manveen Exp $
  - * $Revision: 1.1 $
  - * $Date: 2002/05/04 14:40:09 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/webapps/admin/WEB-INF/classes/org/apache/webapp/admin/resources/EnvEntryForm.java,v
 1.2 2002/05/04 19:46:57 manveen Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/05/04 19:46:57 $
    *
    * ====================================================================
    *
  @@ -71,12 +71,13 @@
   import org.apache.struts.action.ActionMapping;
   import org.apache.webapp.admin.LabelValueBean;
   
  +import java.lang.reflect.Constructor;
   
   /**
    * Form bean for the individual environment entry page.
    *
    * @author Manveen Kaur
  - * @version $Revision: 1.1 $ $Date: 2002/05/04 14:40:09 $
  + * @version $Revision: 1.2 $ $Date: 2002/05/04 19:46:57 $
    * @since 4.1
    */
   
  @@ -156,7 +157,10 @@
           typeVals.add(new LabelValueBean("java.lang.Integer", "java.lang.Integer")); 
   
           typeVals.add(new LabelValueBean("java.lang.Long", "java.lang.Long"));
           typeVals.add(new LabelValueBean("java.lang.Short", "java.lang.Short"));     
   
  -        typeVals.add(new LabelValueBean("java.lang.String", "java.lang.String"));
  +        typeVals.add(new LabelValueBean("java.lang.String", "java.lang.String"));   
        
  +        typeVals.add(new LabelValueBean("java.lang.hehe", "java.lang.hehe"));
  +        typeVals.add(new LabelValueBean("java.util.Vector", "java.util.Vector"));
  +        
       }
   
       /**
  @@ -195,7 +199,6 @@
   
       }
   
  -
       /**
        * Validate the properties that have been set from this HTTP request,
        * and return an <code>ActionErrors</code> object that encapsulates any
  @@ -206,10 +209,13 @@
        * @param mapping The mapping used to select this instance
        * @param request The servlet request we are processing
        */
  +    
  +    private ActionErrors errors = null;
  +    
       public ActionErrors validate(ActionMapping mapping,
       HttpServletRequest request) {
   
  -        ActionErrors errors = new ActionErrors();
  +        errors = new ActionErrors();
   
           String submit = request.getParameter("submit");
           if (submit != null) {
  @@ -243,12 +249,63 @@
                   errors.add("description",
                              new ActionError("users.error.quotes"));
               }
  -
  +            
  +            if (validateType(entryType, value)) {
  +                   errors.add("value",
  +                           new ActionError("resources.error.value.mismatch"));
  +            }
           }
  -
           return (errors);
  -
       }
   
  -
  +    /**
  +     * Entry type must match type of value.
  +     */
  +    private boolean validateType(String entryType, String value) {
  +        Class cls = null;
  +        boolean mismatch = false;
  +        try {
  +            cls = Class.forName(entryType);
  +            
  +            if (Character.class.isAssignableFrom(cls)) {
  +                // Special handling is needed because the UI returns
  +                // a string even if it is a character (single length string).
  +                if (value.length() != 1) {
  +                    mismatch = true;
  +                }
  +            } else if (Boolean.class.isAssignableFrom(cls)) {
  +                // Special handling is needed because Boolean
  +                // string constructor accepts anything other than
  +                // true to be false
  +                if (!("true".equalsIgnoreCase(value) ||
  +                "false".equalsIgnoreCase(value))) {
  +                    mismatch = true;
  +                }
  +            } else if (Number.class.isAssignableFrom(cls)) {
  +                // all numbers throw NumberFormatException if they are
  +                // constructed with an incorrect number string
  +                // We use the general string constructor to do this job
  +                try {
  +                    Class[] parameterTypes = {String.class};
  +                    Constructor ct = cls.getConstructor(parameterTypes);
  +                    Object arglist1[] = {value};
  +                    Object retobj = ct.newInstance(arglist1);
  +                } catch (Exception e) {
  +                    mismatch = true;
  +                }
  +            } else if (String.class.isAssignableFrom(cls)) {
  +                // all strings are allowed
  +            } else {
  +                // validation for other types not implemented yet
  +               errors.add("entryType",
  +                       new ActionError("resources.error.entryType.notimpl"));
  +            }
  +        } catch (ClassNotFoundException cnfe) {
  +            // entry type has an invalid entry
  +           errors.add("entryType",
  +                       new ActionError("resources.error.entryType.invalid"));
  +         }        
  +        return mismatch;
  +    }
  +    
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to