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

📄 methodextagshandler.java

📁 appfuse1.9.4开发的一个简单的人事管理,简单的增删改查,修正了display tag乱码问题,解压缩后cmd进入该目录ant setup 部署到tomcat即可在http://localho
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.example.antbook.xdoclet;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;

import xdoclet.XDocletException;
import xdoclet.tagshandler.MethodTagsHandler;
import xdoclet.util.TypeConversionUtil;
import xjavadoc.Type;
import xjavadoc.XClass;
import xjavadoc.XMember;
import xjavadoc.XMethod;

/**
 * This class is an extension to xdoclet.tagshandler.MethodTagsHandler with
 * extra features designed for appgen.
 *
 * @author hzhang(mb4henry@yahoo.com.au)
 * @author Matt Raible
 * @author David Carter
 *
 */
public class MethodExTagsHandler extends MethodTagsHandler {

    XClass currentClass = getCurrentClass();
    private static String datePattern = "yyyy-MM-dd";
    private static String uiDatePattern = getDatePattern();
    private static final List numericTypes = new ArrayList();
    private String rootClassName = "";
    private String curprefix = "";

    static {
        numericTypes.add("java.lang.Integer");
        numericTypes.add("int");
        numericTypes.add("java.lang.Float");
        numericTypes.add("float");
        numericTypes.add("java.lang.Long");
        numericTypes.add("long");
        numericTypes.add("java.lang.Double");
        numericTypes.add("double");
        numericTypes.add("java.lang.Short");
        numericTypes.add("short");
        numericTypes.add("java.lang.Byte");
        numericTypes.add("byte");
    }

    /**
     * Iterates over all methods of current class and evaluates the body of the
     * tag for each method. <br>
     * The reason to override this method is to add nested form support when
     * iterating over all properties.
     *
     * @see xdoclet.tagshandler.MethodTagsHandler#forAllMethods(java.lang.String,
     *      java.util.Properties)
     *
     * @param template
     * @param attributes
     *
     * @throws XDocletException
     */
    public void forAllMethods(String template, Properties attributes) throws XDocletException {
        XClass currentClass = getCurrentClass();

        if (currentClass == null) {
            throw new XDocletException("currentClass == null!!!");
        }

        rootClassName = classNameLower();
        forAllMembersEx(currentClass, template, attributes, FOR_METHOD, "");
    }

    /**
     * Method to print out an ActionForm, setter and parameter value
     *
     * @return
     * @throws XDocletException
     */
    public String formSetterWithValue() throws XDocletException {
        XMethod method = super.getCurrentMethod();
        XMethod setter = method.getMutator();

        String value = "\"" + randomValueForDbUnit() + "\"";
        if (setter.getPropertyType().getType().isA("java.util.Date")) {
            value = "\"" + getDate(new Date(), uiDatePattern) + "\"";
        }

        rootClassName = classNameLower();
        return rootClassName + "Form." + curprefix + setter.getName() + "(" + value + ");";
    }

    /**
     * Method to print out a class, setter and a parameter value
     *
     * @return
     * @throws XDocletException
     */
    public String setterWithValue() throws XDocletException {
        XMethod method = super.getCurrentMethod();
        XMethod setter = method.getMutator();

        return classNameLower() + "." + curprefix + setter.getName() + "(" + randomValueForSetter() + ");";
    }

    /**
     * Method to print out a random value for use in setting WebTest parameters
     *
     * @return
     * @throws XDocletException
     */
    public String randomValueForWebTest() throws XDocletException {
        XMethod method = super.getCurrentMethod();
        XMethod setter = method.getMutator();

        String value = randomValueForDbUnit();
        if (setter.getPropertyType().getType().isA("java.util.Date")) {
            value = getDate(new Date(), uiDatePattern);
        }

        return value;
    }

    /**
     * A convenient way to get information of an id field. You can get the
     * property name, property type, setter name, getter name. When using this
     * method, you can pass in a parameter call "getType". And its value can be
     * one of
     * <ul>
     * <li>propertyName</li>
     * <li>propertyType</li>
     * <li>getterName</li>
     * <li>setterName</li>
     * </ul>
     * default will return property name.
     *
     * @param attributes
     * @return required information related to id field in the model class
     * @throws XDocletException
     */
    public String idField(Properties attributes) throws XDocletException {
        Collection members = null;

        members = currentClass.getMethods(true);

        for (Iterator j = members.iterator(); j.hasNext(); ) {
            XMember member = (XMember)j.next();
            setCurrentMethod((XMethod)member);
            XMethod getter = (XMethod)member;

            if (super.isGetterMethod(getter)) {
                Properties pro = new Properties();
                pro.setProperty("tagName", "hibernate.id");

                if (super.hasTag(pro, FOR_METHOD)) {
                    break;
                }

                setCurrentClass(member.getContainingClass());
            }
        }
        return methodInfo(attributes);
    }

    /**
     * Method methodInfo
     * @param attributes
     * @return
     * @throws XDocletException
     */
    public String methodInfo(Properties attributes) throws XDocletException {
        String type = attributes.getProperty("getType");
        XMethod getter = super.getCurrentMethod();
        String  result = "";

        if ("propertyName".equals(type)) {
            result = getter.getPropertyName();
        } else if ("getterName".equals(type)) {
            result = getter.getName();
        } else if ("setterName".equals(type)) {
            result = getter.getMutator().getName();
        } else if ("propertyType".equals(type)) {
            result = getter.getPropertyType().getType().getTransformedName();
        } else if ("columnName".equals(type)) {
            result = columnName(attributes);
        } else if ("javaType".equals(type)) {
            result = getter.getPropertyType().getType().getQualifiedName();
        } else if ("jdbcType".equals(type)) {
            result = jdbcType(attributes);
        } else {
            result = getter.getPropertyName();
        }

        return result;
    }

    /**
     * Method columnName
     * @param attributes
     * @return
     *
     * @throws XDocletException
     */
    public String columnName(Properties attributes) throws XDocletException {
        Properties prop = new Properties();

        prop.setProperty("tagName", "hibernate.property");
        prop.setProperty("paramName", "column");

        String column = methodTagValue(prop);

        if ((column == null) || (column.trim().length() < 1)) {
            prop.setProperty("tagName", "hibernate.id");
            column = methodTagValue(prop);
        }

        return column;
    }

    /**
     * Method jdbcType
     * @param props
     * @return
     * @throws XDocletException
     */
    public String jdbcType(Properties props) throws XDocletException {
        String jdbcType = "VARCHAR";

        if (super.getCurrentMethod() != null) {
            String javaType = super.getCurrentMethod().getPropertyType().getType().getQualifiedName().toLowerCase();

            if (javaType.indexOf("date") > 0) {
                jdbcType = "TIMESTAMP";
            } else if (javaType.indexOf("timestamp") > 0) {
                jdbcType = "TIMESTAMP";
            } else if ((javaType.indexOf("int") > 0) || (javaType.indexOf("long") > 0) || (javaType.indexOf("short") > 0)) {
                jdbcType = "INTEGER";
            } else if (javaType.indexOf("double") > 0) {
                jdbcType = "DOUBLE";
            } else if (javaType.indexOf("float") > 0) {
                jdbcType = "FLOAT";
            }
        }

        return jdbcType;
    }

    /**
     * @see xdoclet.tagshandler.MethodTagsHandler#forAllMembers
     * @param currentClass
     * @param template
     * @param attributes
     * @param forType
     * @param prefix
     * @throws XDocletException
     */
    protected void forAllMembersEx(XClass currentClass, String template, Properties attributes, int forType, String prefix)
    throws XDocletException {

⌨️ 快捷键说明

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