commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator ValidatorAction.java
Date Thu, 22 May 2003 05:03:07 GMT
dgraham     2003/05/21 22:03:07

  Modified:    validator/src/share/org/apache/commons/validator
                        ValidatorAction.java
  Log:
  Refactored loadJavascriptFunction into smaller steps and changed default
  scope on init() method to protected.
  
  Revision  Changes    Path
  1.10      +82 -60    jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorAction.java
  
  Index: ValidatorAction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorAction.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ValidatorAction.java	22 May 2003 03:28:05 -0000	1.9
  +++ ValidatorAction.java	22 May 2003 05:03:03 -0000	1.10
  @@ -82,6 +82,7 @@
    * <strong>Note</strong>: The validation method is assumed to be thread safe.
    *
    * @author David Winterfeldt
  + * @author David Graham
    * @version $Revision$ $Date$
    */
   public class ValidatorAction implements Serializable {
  @@ -161,7 +162,7 @@
       /**
        * Logger.
        */
  -    private static Log log = LogFactory.getLog(ValidatorAction.class);
  +    private static final Log log = LogFactory.getLog(ValidatorAction.class);
   
   	/**
   	 * A <code>FastHashMap</code> of the other 
  @@ -330,6 +331,7 @@
           if (jsFunction != null) {
               throw new IllegalStateException("Cannot call setJavascript() after calling
setJsFunction()");
           }
  +        
   		this.javascript = javascript;
   	}
   
  @@ -350,8 +352,8 @@
       /**
        * Initialize based on set.
        */
  -    void init() {
  -        loadFunction();
  +    protected void init() {
  +        this.loadJavascriptFunction();
       }
   
       /**
  @@ -364,10 +366,9 @@
         * If this fails then it will attempt to treat the path as a file path.
         * It is assumed the script ends with a '.js'.
         */
  -     protected synchronized void loadFunction() {
  +     protected synchronized void loadJavascriptFunction() {
   
  -        // Have we already loaded the javascript for this action?
  -        if (javascript != null) {
  +        if (this.javascriptAlreadyLoaded()) {
               return;
           }
   
  @@ -375,68 +376,89 @@
               log.trace("  Loading function begun");
           }
   
  -         // Have we already attempted to load the javascript for this action?
  -         if (javascript != null) {
  -             return;
  +         if (this.jsFunction == null) {
  +             this.jsFunction = this.generateJsFunction();
            }
   
  -         if (jsFunction == null) {
  -             jsFunction = generateJsFunction();
  -         }
  +         String javascriptFileName = this.formatJavascriptFileName();
   
  -         String name;
  -         // Set up to load the javascript function, if we can
  -         if (jsFunction.charAt(0) != '/') {
  -            name = jsFunction.replace('.', '/');
  -            name += ".js";
  -         } else {
  -             name = jsFunction.substring(1);
  +         if (log.isTraceEnabled()) {
  +             log.trace("  Loading js function '" + javascriptFileName + "'");
            }
  -         InputStream is = null;
   
  -         // Load the specified javascript function
  +        this.javascript = this.readJavascriptFile(javascriptFileName);
  +
            if (log.isTraceEnabled()) {
  -             log.trace("  Loading js function '" + name + "'");
  +             log.trace("  Loading javascript function completed");
            }
   
  -         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  -         if (classLoader == null) {
  -             classLoader = this.getClass().getClassLoader();
  -         }
  +    }
   
  -         is = classLoader.getResourceAsStream(name);
  -         if (is == null) {
  -             is = this.getClass().getResourceAsStream(name);
  -         }
  +    /**
  +     * Read a javascript function from a file.
  +     * @param javascriptFileName The file containing the javascript.
  +     * @return The javascript function or null if it could not be loaded.
  +     */
  +    private String readJavascriptFile(String javascriptFileName) {
  +        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  +        if (classLoader == null) {
  +            classLoader = this.getClass().getClassLoader();
  +        }
   
  -         if (is != null) {
  -             try {
  -                 int bufferSize = is.available();
  -                 StringBuffer function = new StringBuffer();
  -                 while (bufferSize > 0) {
  -                    byte[] buffer = new byte[bufferSize];
  -                    is.read(buffer,0,bufferSize);
  -                    String functionPart = new String(buffer);
  -                    function.append(functionPart);
  -                    bufferSize = is.available();
  -                 }
  -                 javascript = function.toString();
  -             } catch (IOException e) {
  -                 log.error("loadFunction()", e);
  -
  -             } finally {
  -                 try {
  -                     is.close();
  -                 } catch (IOException e) {
  -                     log.error("loadFunction()", e);
  -                 }
  -             }
  -         }
  +        InputStream is = classLoader.getResourceAsStream(javascriptFileName);
  +        if (is == null) {
  +            is = this.getClass().getResourceAsStream(javascriptFileName);
  +        }
   
  -         if (log.isTraceEnabled()) {
  -             log.trace("  Loading function completed");
  -         }
  +        if (is == null) {
  +            return null;
  +        }
  +        
  +        StringBuffer function = new StringBuffer();
  +        try {
  +            int bufferSize = is.available();
  +
  +            while (bufferSize > 0) {
  +                byte[] buffer = new byte[bufferSize];
  +                is.read(buffer, 0, bufferSize);
  +                String functionPart = new String(buffer);
  +                function.append(functionPart);
  +                bufferSize = is.available();
  +            }
  +
  +        } catch (IOException e) {
  +            log.error("readJavascriptFile()", e);
  +
  +        } finally {
  +            try {
  +                is.close();
  +            } catch (IOException e) {
  +                log.error("readJavascriptFile()", e);
  +            }
  +        }
  +
  +        return function.toString().equals("") ? null : function.toString();
  +    }
   
  +    /**
  +     * @return A filename suitable for passing to a ClassLoader.getResourceAsStream()
  +     * method.
  +     */
  +    private String formatJavascriptFileName() {
  +        String name = this.jsFunction.substring(1);
  +
  +         if (!this.jsFunction.startsWith("/")) {
  +            name = jsFunction.replace('.', '/') + ".js";
  +         } 
  +         
  +        return name;
  +    }
  +    
  +    /**
  +     * @return true if the javascript for this action has already been loaded.
  +     */
  +    private boolean javascriptAlreadyLoaded() {
  +        return (this.javascript != null);
       }
   
       /**
  
  
  

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


Mime
View raw message