You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by rl...@apache.org on 2004/04/03 02:06:55 UTC

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

rleland     2004/04/02 16:06:55

  Modified:    src/share/org/apache/struts/validator ValidatorPlugIn.java
  Log:
  Modify Validator PlugIn to validate XML file. Failure no loner just logs failure and disables that skips that validation file.
  Errors now result in an exception being thrown to loudly flag an error.
  This also removes usage of deprecated Commons Validator classes.
  
  Revision  Changes    Path
  1.25      +63 -57    jakarta-struts/src/share/org/apache/struts/validator/ValidatorPlugIn.java
  
  Index: ValidatorPlugIn.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/ValidatorPlugIn.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- ValidatorPlugIn.java	14 Mar 2004 06:23:47 -0000	1.24
  +++ ValidatorPlugIn.java	3 Apr 2004 00:06:55 -0000	1.25
  @@ -20,7 +20,12 @@
   
   package org.apache.struts.validator;
   
  +import org.xml.sax.SAXException;
  +
   import java.util.StringTokenizer;
  +import java.util.List;
  +import java.util.ArrayList;
  +import java.util.Iterator;
   import java.io.BufferedInputStream;
   import java.io.InputStream;
   import java.io.IOException;
  @@ -30,7 +35,6 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   import org.apache.commons.validator.ValidatorResources;
  -import org.apache.commons.validator.ValidatorResourcesInitializer;
   import org.apache.struts.action.ActionServlet;
   import org.apache.struts.action.PlugIn;
   import org.apache.struts.config.ModuleConfig;
  @@ -68,14 +72,15 @@
        * Application scope key that <code>ValidatorResources</code> is stored under.
        */
       public final static String VALIDATOR_KEY =
  -        "org.apache.commons.validator.VALIDATOR_RESOURCES";
  +            "org.apache.commons.validator.VALIDATOR_RESOURCES";
   
       /**
        * Application scope key that <code>StopOnError</code> is stored under.
  +     *
        * @since Struts 1.2
        */
       public final static String STOP_ON_ERROR_KEY =
  -        "org.apache.struts.validator.STOP_ON_ERROR";
  +            "org.apache.struts.validator.STOP_ON_ERROR";
   
       /**
        * The set of Form instances that have been created and initialized,
  @@ -93,6 +98,7 @@
   
       /**
        * Gets a comma delimitted list of Validator resources.
  +     *
        * @return comma delimited list of Validator resource path names
        */
       public String getPathnames() {
  @@ -101,6 +107,7 @@
   
       /**
        * Sets a comma delimitted list of Validator resources.
  +     *
        * @param pathnames delimited list of Validator resource path names
        */
       public void setPathnames(String pathnames) {
  @@ -116,36 +123,37 @@
   
       /**
        * Gets the value for stopOnFirstError.
  +     *
        * @return A boolean indicating whether JavaScript validation should stop
  -     * when it finds the first error (Struts 1.1 behaviour) or continue
  -     * validation.
  +     *         when it finds the first error (Struts 1.1 behaviour) or continue
  +     *         validation.
        * @since Struts 1.2
        */
       public boolean isStopOnFirstError() {
  -    	return this.stopOnFirstError;
  +        return this.stopOnFirstError;
       }
   
       /**
        * Sets the value for stopOnFirstError.
  +     *
        * @param stopOnFirstError A boolean indicating whether JavaScript
  -     * validation should stop when it finds the first error
  -     * (Struts 1.1 behaviour) or continue validation.
  +     *                         validation should stop when it finds the first error
  +     *                         (Struts 1.1 behaviour) or continue validation.
        * @since Struts 1.2
        */
       public void setStopOnFirstError(boolean stopOnFirstError) {
  -    	this.stopOnFirstError = stopOnFirstError;
  +        this.stopOnFirstError = stopOnFirstError;
       }
   
       /**
        * Initialize and load our resources.
        *
        * @param servlet The ActionServlet for our application
  -     * @param config The ModuleConfig for our owning module
  -     *
  -     * @exception ServletException if we cannot configure ourselves correctly
  +     * @param config  The ModuleConfig for our owning module
  +     * @throws ServletException if we cannot configure ourselves correctly
        */
       public void init(ActionServlet servlet, ModuleConfig config)
  -        throws ServletException {
  +            throws ServletException {
   
           // Remember our associated configuration and servlet
           this.config = config;
  @@ -155,18 +163,15 @@
           try {
               this.initResources();
   
  -            servlet.getServletContext().setAttribute(
  -                VALIDATOR_KEY + config.getPrefix(),
  -                resources);
  -
  -            servlet.getServletContext().setAttribute(
  -                STOP_ON_ERROR_KEY + '.' + config.getPrefix(),
  -                new Boolean(this.stopOnFirstError));
  +            servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(),
  +                                                     resources);
  +
  +            servlet.getServletContext().setAttribute(STOP_ON_ERROR_KEY + '.' + config.getPrefix(),
  +                                                     new Boolean(this.stopOnFirstError));
   
           } catch (Exception e) {
               log.error(e.getMessage(), e);
  -            throw new UnavailableException(
  -                "Cannot load a validator resource from '" + pathnames + "'");
  +            throw new UnavailableException("Cannot load a validator resource from '" + pathnames + "'");
           }
   
       }
  @@ -190,51 +195,52 @@
       /**
        * Initialize the validator resources for this module.
        *
  -     * @exception IOException if an input/output error is encountered, not thrown by this implementation
  -     * @exception ServletException if we cannot initialize these resources, not thrown by this implementation
  +     * @throws IOException      if an input/output error is encountered
  +     * @throws ServletException if we cannot initialize these resources
        */
       protected void initResources() throws IOException, ServletException {
  -        this.resources = new ValidatorResources();
   
           if (pathnames == null || pathnames.length() <= 0) {
               return;
           }
  -
           StringTokenizer st = new StringTokenizer(pathnames, RESOURCE_DELIM);
   
  -        while (st.hasMoreTokens()) {
  -            String validatorRules = st.nextToken().trim();
  -
  -            if (log.isInfoEnabled()) {
  -                log.info(
  -                    "Loading validation rules file from '" + validatorRules + "'");
  -            }
  -
  -            InputStream input =
  -                servlet.getServletContext().getResourceAsStream(validatorRules);
  +        List streamList = new ArrayList();
  +        try {
  +            while (st.hasMoreTokens()) {
  +                String validatorRules = st.nextToken().trim();
  +                if (log.isInfoEnabled()) {
  +                    log.info("Loading validation rules file from '" + validatorRules + "'");
  +                }
   
  -            if (input != null) {
  -                BufferedInputStream bis = new BufferedInputStream(input);
  +                InputStream input = servlet.getServletContext().getResourceAsStream(validatorRules);
   
  -                try {
  -                    // pass in false so resources aren't processed
  -                    // until last file is loaded
  -                    ValidatorResourcesInitializer.initialize(resources, bis, false);
  -                } catch (IOException e) {
  -                    log.error(e.getMessage(), e);
  -                } finally {
  -                    bis.close();
  +                if (input != null) {
  +                    BufferedInputStream bis = new BufferedInputStream(input);
  +                    streamList.add(bis);
  +                } else {
  +                    throw new ServletException("Skipping validation rules file from '"
  +                              + validatorRules + "'.  No stream could be opened.");
                   }
  +            }
  +            int streamSize = streamList.size();
  +            InputStream[] streamArray = new InputStream[streamSize];
  +            for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) {
  +                InputStream is = (InputStream) streamList.get(streamIndex);
  +                streamArray[streamIndex] = is;
  +            }
   
  -            } else {
  -                log.error(
  -                    "Skipping validation rules file from '"
  -                        + validatorRules
  -                        + "'.  No stream could be opened.");
  +            this.resources = new ValidatorResources(streamArray);
  +        } catch (SAXException sex) {
  +            log.error("Skipping all validation",sex);
  +            throw new ServletException(sex);
  +        } finally {
  +            Iterator streamIterator = streamList.iterator();
  +            while (streamIterator.hasNext()) {
  +                InputStream is = (InputStream) streamIterator.next();
  +                is.close();
               }
           }
  -
  -        resources.process();
   
       }
   
  
  
  

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