formgenerator.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,156 行 · 第 1/3 页

JAVA
1,156
字号
package Jt.wizard.struts;

import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
import org.jdom.Element;
import Jt.*;
import Jt.util.JtJavaParser;
import Jt.util.BeanProperty;
import Jt.xml.JDOMAdapter;


/**
 * Jsp generator (Internal JtWizard Class).
 */

public class FormGenerator extends JtObject {
    public static final String JtCLASS_NAME = FormGenerator.class.getName(); 
    public static final String GENERATE_FORM = "GENERATE_FORM"; 
    public static final String GENERATE_ACTION_FORM = "GENERATE_ACTION_FORM"; 
    
    private static final long serialVersionUID = 1L;

    private String baseClassName;
    private String className;
    private String pack;  
    private String formName;
    private JDOMAdapter adapter;
    private boolean dynaActionForm;
    private JtFactory factory = new JtFactory ();

    StringBuffer oBuffer = new StringBuffer ();


    public FormGenerator() {
    }
    
    private void handleUIError (String error) {
        Collection col;

        if (error == null)
            return;
        col = (Collection) this.getObjErrors();

        if (col == null) {
            col = new LinkedList ();
            setObjErrors (col);
        }
        col.add(error);

    }

    // Attributes


    public void setClassName (String greetingMessage) {
        this.className = greetingMessage; 

    }

    public String getClassName () {
        return (className);
    }

    public String getBaseClassName() {
        return baseClassName;
    }

    public void setBaseClassName(String baseClassName) {
        this.baseClassName = baseClassName;
    }

    public boolean isDynaActionForm() {
        return dynaActionForm;
    }

    public void setDynaActionForm(boolean dynaActionForm) {
        this.dynaActionForm = dynaActionForm;
    }

    public String getFormName() {
        return formName;
    }

    public void setFormName(String name) {
        this.formName = name;
    }
/*
    public String getPack() {
        return pack;
    }

    public void setPack(String pack) {
        this.pack = pack;
    }
*/
    private void addElement (JDOMAdapter adapter, String parentPath, Element element) {
        JtMessage msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);

        if (adapter == null || element == null)
            return;

        msg.setMsgContent(element);
        msg.setMsgData(parentPath);
        adapter.processMessage(msg);
    }


    private boolean generateDynaFields (String className) {
        //JtFactory factory = new JtFactory ();
        Object obj;
        JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
        JtHashTable attributes;
        JtIterator iterator;
        String attrName;
        Element element;
        int index = 1;
        PropertyDescriptor prop;
        JtMessage msg1 = new JtMessage (JtHashTable.JtGET);
        String type;



        if (className == null)
            return (false);
        obj = factory.createObject(className);

        if (obj == null) {
            // Propagate exception
            if (factory.getObjException() != null)
                this.setObjException(factory.getObjException());
            return (false);      
        }

        msg.setMsgContent(obj);

        attributes = (JtHashTable) factory.processMessage(msg);

        if (attributes == null) {
            handleWarning ("FormGenerator.generateDynaFields: no class attributes for " +
                    className);
            return (true);
        }  

        iterator = (JtIterator) attributes.processMessage (new JtMessage (JtHashTable.JtGET_KEYS));

        if (iterator == null)
            return (false); //check

        msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        for (;;) {
            attrName = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));

            if (attrName == null)
                break;
            msg1.setMsgData (attrName);
            prop = (PropertyDescriptor) attributes.processMessage(msg1);
            
            
            type = prop.getPropertyType().getName();
            if (!(prop.getPropertyType().isPrimitive() || 
                    "java.lang.String".equals(type) || 
                    "java.util.Date".equals(type)))
                continue;

            //if (!(prop.getPropertyType().isPrimitive() || 
            //        String.class.isAssignableFrom (prop.getPropertyType())))
            //    continue;

            element = new Element ("form-property");
            element.setAttribute( "name", attrName);
            
                      
            if ("java.util.Date".equals(type))
                element.setAttribute ("type", "java.lang.String");              
            else    
                element.setAttribute ("type", prop.getPropertyType().getName());
            
            msg.setMsgContent(element);     

            //adapter.processMessage(msg);  
            addElement (adapter, "form-bean", element);

            index++;
        }  


        return (true);

    }  
    
    private boolean ngenerateDynaFields (InputStream inputStream) {
        //Object obj;
        JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
        JtHashTable attributes;
        JtIterator iterator;
        String attrName;
        Element element;
        int index = 1;
        BeanProperty prop;
        JtMessage msg1 = new JtMessage (JtHashTable.JtGET);
        String type;

        if (inputStream == null)
            return (false);
        
        attributes = retrieveAttributes (inputStream);        
        
        iterator = (JtIterator) attributes.processMessage (new JtMessage (JtHashTable.JtGET_KEYS));

        if (iterator == null)
            return (false); //check
/*
        if (className == null)
            return (false);
        obj = factory.createObject(className);

        if (obj == null) {
            // Propagate exception
            if (factory.getObjException() != null)
                this.setObjException(factory.getObjException());
            return (false);      
        }

        msg.setMsgContent(obj);

        attributes = (JtHashTable) factory.processMessage(msg);
*/
        
        if (attributes == null) {
            handleWarning ("FormGenerator.generateDynaFields: no class attributes for " +
                    className);
            return (true);
        }  

        iterator = (JtIterator) attributes.processMessage (new JtMessage (JtHashTable.JtGET_KEYS));

        if (iterator == null)
            return (false); //check

        msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        for (;;) {
            attrName = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));

            if (attrName == null)
                break;
            msg1.setMsgData (attrName);

            prop = (BeanProperty) attributes.processMessage(msg1);

            type = prop.getType();
            if (!(prop.isPrimitive() || 
                    "java.lang.String".equals(type) || "String".equals(type) ||
                    "java.util.Date".equals(type) || "Date".equals(type)))
                continue;

            //if (!(prop.getPropertyType().isPrimitive() || 
            //        String.class.isAssignableFrom (prop.getPropertyType())))
            //    continue;

            element = new Element ("form-property");
            element.setAttribute( "name", attrName);
            
                      
            if ("java.util.Date".equals(type) || "Date".equals(type) || "String".equals(type))
                element.setAttribute ("type", "java.lang.String");              
            else    
                element.setAttribute ("type", prop.getType ());
            
            msg.setMsgContent(element);     

            //adapter.processMessage(msg);  
            addElement (adapter, "form-bean", element);

            index++;
        }  


        return (true);

    }  

    /*
    private boolean generateInputFields (String className) {
        //JtFactory factory = new JtFactory ();
        Object obj;
        JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
        JtHashTable attributes;
        JtIterator iterator;
        String attrName;
        String attrName1;
        //Element element;
        String type;

        int index = 1;
        //Namespace nameSpace = Namespace.getNamespace("html", "http://www.xxl.com");
        PropertyDescriptor prop;
        JtMessage msg1 = new JtMessage (JtHashTable.JtGET);

        if (className == null)
            return (false);
        obj = factory.createObject(className);

        if (obj == null) {
            // Propagate exception
            if (factory.getObjException() != null)
                this.setObjException(factory.getObjException());
            return (false);      
        }

        msg.setMsgContent(obj);

        attributes = (JtHashTable) factory.processMessage(msg);

        if (attributes == null) {
            handleWarning ("FormGenerator.generateProperties: no class attributes for " +
                    className);
            return (true);
        }  

        iterator = (JtIterator) attributes.processMessage (new JtMessage (JtHashTable.JtGET_KEYS));

        if (iterator == null)
            return (false); //check

        msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        for (;;) {
            attrName = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));

            if (attrName == null)
                break;
            msg1.setMsgData (attrName);
            prop = (PropertyDescriptor) attributes.processMessage(msg1);

            type = prop.getPropertyType().getName();
            if (!(prop.getPropertyType().isPrimitive() || 
                    "java.lang.String".equals(type) ||
                    "java.util.Date".equals(type)))
                continue;
            
            //type = prop.getPropertyType().getName();
            
            if ("java.lang.String".equals(type) ||
                    "java.util.Date".equals(type))
                type = "String";

            oBuffer.append ("private " + type + " "+  attrName + ";\n");

            index++;
        }  

        oBuffer.append ("\n\n");

        iterator = (JtIterator) attributes.processMessage (new JtMessage (JtHashTable.JtGET_KEYS));

        if (iterator == null)
            return (false); //check

        for (;;) {
            attrName = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));

            if (attrName == null || attrName.equals (""))
                break;
            msg1.setMsgData (attrName);
            prop = (PropertyDescriptor) attributes.processMessage(msg1);

            //if (!(prop.getPropertyType().isPrimitive() || 
            //        String.class.isAssignableFrom (prop.getPropertyType())))
            
            type = prop.getPropertyType().getName();
            if (!(prop.getPropertyType().isPrimitive() || 
                    "java.lang.String".equals(type) ||
                    "java.util.Date".equals(type)))
                continue;

            //attrName1 = "" + attrName;
            if (attrName.length() == 1)
                attrName1 = Character.toUpperCase(attrName.charAt(0)) + "";
            else
                attrName1 = Character.toUpperCase(attrName.charAt(0)) + attrName.substring (1);

⌨️ 快捷键说明

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