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

📄 baseform.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *
     * @param s The sting to check
     */
    protected boolean blank(String s) {
        return ConvertUtils.blank(s);
    }


    /**
     * Convenience method to check for a null, empty, or "0" String.
     *
     * @param s The sting to check
     */
    protected boolean blankValue(String s) {
        return ConvertUtils.blankValue(s);
    }


    /**
     * @deprecated Use blank instead.
     */
    protected boolean isBlank(String s) {
        return blank(s);
    }


    /**
     * Convenience method to test for a required field
     * and setup the error message.
     */
    protected void required(
        ActionErrors errors,
        String field,
        String name,
        String arg) {
        if ((null==field) || (0==field.length())) {
            errors.add(name,
                new ActionError(Tokens.ERRORS_REQUIRED,arg));
        }
    }


    /**
     * Convenience method to test for a required array
     * and setup the error message.
     */
    protected void required(
        ActionErrors errors,
        String[] field,
        String name,
        String arg) {
        if ((null==field) || (0==field.length)) {
            errors.add(name,
                new ActionError(Tokens.ERRORS_REQUIRED,arg));
        }
    }


    /**
     * Create an object of the specified class,
     * throwing a runtime exception if any error occurs.
     * If an exception is not thrown, then helper is guaranteed to exist.
     *
     * @param objectClass The name of the class
     * @throws IllegalArgumentException if object cannot be
     * instantiated
     */
    public Object createObject(
            String objectClass) {

            // Try the create
        Object object = null;
        try {
            object = Class.forName(objectClass).newInstance();
        }
        catch (Throwable t) {
           object = null;
               // assemble message: {class}: {exception}
           StringBuffer sb = new StringBuffer();
           sb.append(Log.CREATE_OBJECT_ERROR);
           sb.append(Log.CLASS);
           sb.append(objectClass);
           sb.append(Log.SPACE);
           sb.append(Log.ACTION_EXCEPTION);
           sb.append(t.toString());
                // throw runtime exception
           throw new IllegalArgumentException(sb.toString());
        }
        return object;

     } // createHelperObject()


    /**
     * Return map of properties for this bean.
     * Base method uses <code>PropertyUtils.describe</code>.
     * Override if some properties should not be transfered
     * this way, or a property name should be altered.
     * This will return the actual public properties.
     *
     * @exception Exception on any error.
     */
    public Map describe() throws Exception {

        try {
            return PropertyUtils.describe(this);
        } catch (Throwable t) {
            throw new ChainedException(t);
      }

    } // end describe


    /**
     * Set properties from given object.
     * Base method uses <code>BeanUtils.copyProperties</code> and
     * <code>PropertyUtils.describe</code>.
     *
     * @param o The object to use to populate this object.
     * @exception Exception on any error.
     */
    public void set(Object o) throws Exception {

        try {
            BeanUtils.copyProperties(this,o);
        } catch (Throwable t) {
            throw new ChainedException(t);
      }

    } // end set


    /**
     * Populate matching properties on given object.
     * Base method uses <code>BeanUtils.copyProperties</code> and
     * <code>describe()</code>.
     *
     * @param o The object to populate from this object.
     * @exception Exception on any error.
     */
    public void populate(Object o) throws Exception {

        try {
            BeanUtils.copyProperties(o,describe());
        } catch (Throwable t) {
            throw new ChainedException(t);
      }

    } // end populate


    /**
     * // :FIXME: Needs testing. Works OK without a profile bean =:o)
     * Merge a profile bean with this bean to provide a unified map
     * of the combined parameters. Will on add properties to the map
     * from the profile bean if matching property on this bean is
     * blank or null (which includes absent).
     * The result is a union of the properties, with the this
     * bean's non-blank properties having precedence over the
     * profile's properties. The profile is a base that this bean
     * can override on the fly -- If this bean does not supply a
     * property, then the profile property is used. But any 
     * property named on the userProfile is included (even if 
     * it has no match on this bean).
     * <p>
     * If profile is null, a map of this bean's properties is returned.
     * <p>
     * The profile can be any JavaBean.
     * <p>
     * This method is forwardly-compatible with BaseMapForm.
     * For an instance of BaseMapForm, getMap() is used; otherwise
     * describe() or PropertyUtils.describe() is used.
     *
     * :FIXME: Needs testing. Works OK without a profile bean =:o)
     * @param profile The profile bean, if any
     * @throws Exception if error transfering data to map
     */
    protected Map merge(Object profile) throws Exception {

        Map formMap = null;
        if (this instanceof BaseMapForm) {
            BaseMapForm form = (BaseMapForm) this;
            formMap = form.getMap();
        }
        else {
            formMap = describe();
        }

        if (profile!=null) {

            Map userMap = null;
            if (profile instanceof BaseMapForm) {
                BaseMapForm form = (BaseMapForm) profile;
                userMap = form.getMap();
            }
            else if (profile instanceof BaseForm) {
                BaseForm form = (BaseForm) profile;
                userMap = form.describe();
            }
            else {
                userMap = PropertyUtils.describe(this);
            }

            // Add user element to formMap if form element is null or blank
            // Starting with the formMap, for every element in the userMap, 
            // see if the formMap element is non-existant, null, or an empty String. 
            // If it is (our formMap doesn't override), add the userMap value 
            // to the formMap. 
            Iterator i = userMap.keySet().iterator();
            while (i.hasNext()) {
                String key = (String) i.next();
                String formValue = (String) formMap.get(key);
                if (isBlank(formValue)) {
                    formMap.put(key,userMap.get(key));
                }
            }
        }

        return formMap;

    } // end merge


 
    /**
     * If bean is set to mutable, calls <code>resetSessionLocale</code>
     * and <code>resetRemoteHost</code>.
     *
     * Subclasses resetting their own fields should observe the mutable
     * state (<code>if (isMutable()) ...</code>).
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public void reset(
            ActionMapping mapping,
            HttpServletRequest request) {

        if (isMutable()) {
            
            super.reset(mapping,request);

           // :TODO: Might be useful to have a collection of reset listeners 
            resetRemoteHost(request);
            resetSessionLocale(request);
        }

    } // end reset


    /**
     * Return an empty ActionErrors or the result of calling
     * the superclass validate. Will not return null.
     */
    public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {

        ActionErrors errors = super.validate(mapping,request);
        if (null==errors) errors = new ActionErrors();
        return errors;
    }
    

} // end BaseForm

⌨️ 快捷键说明

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