Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 80746 invoked from network); 18 May 2003 21:31:10 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 18 May 2003 21:31:10 -0000 Received: (qmail 22209 invoked by uid 97); 18 May 2003 21:33:22 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 22202 invoked from network); 18 May 2003 21:33:21 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 18 May 2003 21:33:21 -0000 Received: (qmail 80527 invoked by uid 500); 18 May 2003 21:31:08 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 80516 invoked by uid 500); 18 May 2003 21:31:08 -0000 Received: (qmail 80513 invoked from network); 18 May 2003 21:31:08 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 18 May 2003 21:31:07 -0000 Received: (qmail 69620 invoked by uid 1182); 18 May 2003 21:31:07 -0000 Date: 18 May 2003 21:31:07 -0000 Message-ID: <20030518213107.69618.qmail@icarus.apache.org> From: rleland@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator ValidatorAction.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N rleland 2003/05/18 14:31:06 Modified: validator/src/share/org/apache/commons/validator ValidatorAction.java Log: Add ability to read default javascript definitions resources from commons-validator jar file or any other class path given. Revision Changes Path 1.7 +159 -4 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.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- ValidatorAction.java 29 Apr 2003 02:47:53 -0000 1.6 +++ ValidatorAction.java 18 May 2003 21:31:06 -0000 1.7 @@ -62,6 +62,8 @@ package org.apache.commons.validator; import java.io.Serializable; +import java.io.InputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -69,6 +71,8 @@ import java.util.Map; import java.util.StringTokenizer; import org.apache.commons.collections.FastHashMap; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** *

Contains the information to dynamically instantiate and run a validation @@ -137,6 +141,12 @@ */ private String jsFunctionName = null; + /** + * An optional field to contain the class path to be + * used to retrieve the JavaScript function. + */ + private String jsFunction = null; + /** * An optional field to containing a JavaScript representation of the * java method assocated with this action. @@ -149,6 +159,12 @@ */ private Object instance = null; + + /** + * Logger + */ + private static Log log = LogFactory.getLog(ValidatorAction.class); + /** * A FastHashMap of the other * ValidatorActions this one depends on (if any). @@ -269,6 +285,38 @@ this.jsFunctionName = jsFunctionName; } + /** + * Sets the fully qualified class path of the Javascript function. + *

+ * This is optional and can be used instead of the setJavascript(). + * Attempting to call both setJsFunction and setJavascript + * will result in an IllegalStateException being thrown.

+ * If neither setJsFunction or setJavascript is set then validator will attempt + * to load the default javascript definition.

+ *
  +     * Examples
  +     *   If in the validator.xml :
  +     * #1:
  +     *      <validator name="tire"
  +     *            jsFunction="com.yourcompany.project.tireFuncion">
  +     *     Validator will attempt to load com.yourcompany.project.validateTireFunction.js from
  +     *     its class path.
  +     * #2:
  +     *    <validator name="tire">
  +     *      Validator will use the name attribute to try and load
  +     *         org.apache.commons.validator.javascript.validateTire.js
  +     *      which is the default javascript definition.
  +     * 
+ */ + public void setJsFunction(String jsFunction) { + if (javascript != null) { + throw new IllegalStateException("Cannot call setJsFunction() after calling setJavascript()"); + } + + this.jsFunction = jsFunction; + } + + /** * Gets the Javascript equivalent of the java class and method * associated with this action. @@ -282,6 +330,9 @@ * associated with this action. */ public void setJavascript(String javascript) { + if (jsFunction != null) { + throw new IllegalStateException("Cannot call setJavascript() after calling setJsFunction()"); + } this.javascript = javascript; } @@ -299,6 +350,110 @@ this.instance = instance; } + /** + * Initialize based on set. + */ + void init() { + loadFunction(); + } + + /** + * Load the javascript function specified by the given path. For this + * implementation, the jsFunction property should contain a fully + * qualified package and script name, separated by periods, to be loaded from + * the class loader that created this instance. + * + * @TODO if the path begins with a '/' the path will be intepreted as absolute, and remain unchanged. + * 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() { + + // Have we already loaded the javascript for this action? + if (javascript != null) { + return; + } + + if (log.isTraceEnabled()) { + log.trace(" Loading function begun"); + } + + // Have we already attempted to load the javascript for this action? + if (javascript != null) { + return; + } + + if (jsFunction == null) { + jsFunction = generateJsFunction(); + } + + 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); + } + InputStream is = null; + + // Load the specified javascript function + if (log.isTraceEnabled()) { + log.trace(" Loading js function '" + name + "'"); + } + + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) { + classLoader = this.getClass().getClassLoader(); + } + + is = classLoader.getResourceAsStream(name); + if (is == null) { + is = this.getClass().getResourceAsStream(name); + } + + 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); + } + } + } + + if (log.isTraceEnabled()) { + log.trace(" Loading function completed"); + } + + } + + /** + * Used to generate the javascript name when it is not specified. + * @return + */ + private String generateJsFunction() { + StringBuffer jsName = new StringBuffer("org.apache.commons.validator.javascript"); + jsName.append(".validate"); + jsName.append(name.substring(0,1).toUpperCase()); + jsName.append(name.substring(1,name.length())); + return jsName.toString(); + + } /** * Creates a FastHashMap for the isDependency method * based on depends. --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org