⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validatoraction.java

📁 java验证框架 java验证框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.validator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.util.ValidatorUtils;

/**
 * Contains the information to dynamically create and run a validation
 * method.  This is the class representation of a pluggable validator that can 
 * be defined in an xml file with the <validator> element.
 *
 * <strong>Note</strong>: The validation method is assumed to be thread safe.
 *
 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
 */
public class ValidatorAction implements Serializable {
    
    /**
     * Logger.
     */
    private transient Log log = LogFactory.getLog(ValidatorAction.class);

    /**
     * The name of the validation.
     */
    private String name = null;

    /**
     * The full class name of the class containing
     * the validation method associated with this action.
     */
    private String classname = null;
    
    /**
     * The Class object loaded from the classname.
     */
    private Class validationClass = null;

    /**
     * The full method name of the validation to be performed.  The method
     * must be thread safe.
     */
    private String method = null;
    
    /**
     * The Method object loaded from the method name.
     */
    private Method validationMethod = null;

    /**
     * <p>
     * The method signature of the validation method.  This should be a comma
     * delimited list of the full class names of each parameter in the correct 
     * order that the method takes.
     * </p>
     * <p>
     * Note: <code>java.lang.Object</code> is reserved for the
     * JavaBean that is being validated.  The <code>ValidatorAction</code>
     * and <code>Field</code> that are associated with a field's
     * validation will automatically be populated if they are
     * specified in the method signature.
     * </p>
     */
    private String methodParams =
            Validator.BEAN_PARAM
            + ","
            + Validator.VALIDATOR_ACTION_PARAM
            + ","
            + Validator.FIELD_PARAM;
            
    /**
     * The Class objects for each entry in methodParameterList.
     */        
    private Class[] parameterClasses = null;

    /**
     * The other <code>ValidatorAction</code>s that this one depends on.  If 
     * any errors occur in an action that this one depends on, this action will 
     * not be processsed.
     */
    private String depends = null;

    /**
     * The default error message associated with this action.
     */
    private String msg = null;

    /**
     * An optional field to contain the name to be used if JavaScript is 
     * generated.
     */
    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.
     */
    private String javascript = null;

    /**
     * If the java method matching the correct signature isn't static, the 
     * instance is stored in the action.  This assumes the method is thread 
     * safe.
     */
    private Object instance = null;

    /**
     * An internal List representation of the other <code>ValidatorAction</code>s
     * this one depends on (if any).  This List gets updated
     * whenever setDepends() gets called.  This is synchronized so a call to
     * setDepends() (which clears the List) won't interfere with a call to
     * isDependency().
     */
    private List dependencyList = Collections.synchronizedList(new ArrayList());

    /**
     * An internal List representation of all the validation method's 
     * parameters defined in the methodParams String.
     */
    private List methodParameterList = new ArrayList();

    /**
     * Gets the name of the validator action.
     * @return Validator Action name.
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the validator action.
     * @param name Validator Action name.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the class of the validator action.
     * @return Class name of the validator Action.
     */
    public String getClassname() {
        return classname;
    }

    /**
     * Sets the class of the validator action.
     * @param classname Class name of the validator Action.
     */
    public void setClassname(String classname) {
        this.classname = classname;
    }

    /**
     * Gets the name of method being called for the validator action.
     * @return The method name.
     */
    public String getMethod() {
        return method;
    }

    /**
     * Sets the name of method being called for the validator action.
     * @param method The method name.
     */
    public void setMethod(String method) {
        this.method = method;
    }

    /**
     * Gets the method parameters for the method.
     * @return Method's parameters.
     */
    public String getMethodParams() {
        return methodParams;
    }

    /**
     * Sets the method parameters for the method.
     * @param methodParams A comma separated list of parameters.
     */
    public void setMethodParams(String methodParams) {
        this.methodParams = methodParams;

        this.methodParameterList.clear();

        StringTokenizer st = new StringTokenizer(methodParams, ",");
        while (st.hasMoreTokens()) {
            String value = st.nextToken().trim();

            if (value != null && value.length() > 0) {
                this.methodParameterList.add(value);
            }
        }
    }

    /**
     * Gets the dependencies of the validator action as a comma separated list 
     * of validator names.
     * @return The validator action's dependencies.
     */
    public String getDepends() {
        return this.depends;
    }

    /**
     * Sets the dependencies of the validator action.
     * @param depends A comma separated list of validator names.
     */
    public void setDepends(String depends) {
        this.depends = depends;

        this.dependencyList.clear();

        StringTokenizer st = new StringTokenizer(depends, ",");
        while (st.hasMoreTokens()) {
            String depend = st.nextToken().trim();

            if (depend != null && depend.length() > 0) {
                this.dependencyList.add(depend);
            }
        }
    }

    /**
     * Gets the message associated with the validator action.
     * @return The message for the validator action.
     */
    public String getMsg() {
        return msg;
    }

    /**
     * Sets the message associated with the validator action.
     * @param msg The message for the validator action.
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * Gets the Javascript function name.  This is optional and can
     * be used instead of validator action name for the name of the
     * Javascript function/object.
     * @return The Javascript function name.
     */
    public String getJsFunctionName() {
        return jsFunctionName;
    }

    /**
     * Sets the Javascript function name.  This is optional and can
     * be used instead of validator action name for the name of the
     * Javascript function/object.
     * @param jsFunctionName The Javascript function name.
     */
    public void setJsFunctionName(String jsFunctionName) {
        this.jsFunctionName = jsFunctionName;
    }

    /**
     * Sets the fully qualified class path of the Javascript function.
     * <p>
     * This is optional and can be used <strong>instead</strong> of the setJavascript().
     * Attempting to call both <code>setJsFunction</code> and <code>setJavascript</code>
     * will result in an <code>IllegalStateException</code> being thrown. </p>
     * <p>
     * If <strong>neither</strong> setJsFunction or setJavascript is set then 
     * validator will attempt to load the default javascript definition.
     * </p>
     * <pre>
     * <b>Examples</b>
     *   If in the validator.xml :
     * #1:
     *      &lt;validator name="tire"
     *            jsFunction="com.yourcompany.project.tireFuncion"&gt;
     *     Validator will attempt to load com.yourcompany.project.validateTireFunction.js from
     *     its class path.
     * #2:
     *    &lt;validator name="tire"&gt;
     *      Validator will use the name attribute to try and load
     *         org.apache.commons.validator.javascript.validateTire.js
     *      which is the default javascript definition.
     * </pre>
     * @param jsFunction The Javascript function's fully qualified class path.
     */
    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.
     * @return The Javascript validation.
     */
    public String getJavascript() {
        return javascript;
    }

    /**
     * Sets the Javascript equivalent of the java class and method
     * associated with this action.
     * @param javascript The Javascript validation.
     */
    public void setJavascript(String javascript) {
        if (jsFunction != null) {
            throw new IllegalStateException("Cannot call setJavascript() after calling setJsFunction()");
        }

        this.javascript = javascript;
    }

    /**
     * Initialize based on set.
     */
    protected void init() {
        this.loadJavascriptFunction();
    }

    /**
     * Load the javascript function specified by the given path.  For this
     * implementation, the <code>jsFunction</code> 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 loadJavascriptFunction() {

        if (this.javascriptAlreadyLoaded()) {
            return;
        }

        if (getLog().isTraceEnabled()) {
            getLog().trace("  Loading function begun");
        }

        if (this.jsFunction == null) {
            this.jsFunction = this.generateJsFunction();
        }

        String javascriptFileName = this.formatJavascriptFileName();

        if (getLog().isTraceEnabled()) {
            getLog().trace("  Loading js function '" + javascriptFileName + "'");
        }

        this.javascript = this.readJavascriptFile(javascriptFileName);

        if (getLog().isTraceEnabled()) {
            getLog().trace("  Loading javascript function completed");
        }

    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -