formgenerator.java

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

JAVA
1,156
字号


            
            if ("java.lang.String".equals(type) || "java.util.Date".equals(type))
                type = "String";
         
            oBuffer.append ("public " + type 
                    + " get"+attrName1 + "() {\n");
            oBuffer.append ("  return (" + attrName + ");\n");
            oBuffer.append ("}\n\n");   


            oBuffer.append ("public void " + "set"+attrName1 + "(" + 
                    type + " " + attrName + ") {\n");
            oBuffer.append ("  this." + attrName + "=" + attrName + ";\n");
            oBuffer.append ("}\n\n"); 

            index++;
        } 


        return (true);

    }
*/
    
    private JtHashTable retrieveAttributes (InputStream inputStream) {
        
        JtJavaParser parser = new JtJavaParser ();
        JtHashTable attributes;
        
        if (inputStream == null)
            return null;
                
        parser.setInputStream(inputStream);                
        
        attributes = (JtHashTable) factory.sendMessage(parser, new JtMessage (JtJavaParser.PARSE));
        

        if (parser.getObjException() != null) {
            this.setObjException(factory.getObjException());
            return null; // check
        }
        
        if (attributes  == null)
            return null;
        
        return (attributes);
        
    }

    private boolean ngenerateInputFields (InputStream inputStream) {

        Object obj;
        JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
        JtHashTable attributes;
        JtIterator iterator;
        String attrName;
        String attrName1;
        //Element element;
        String type;

        int index = 1;
        BeanProperty prop;
        JtMessage msg1 = new JtMessage (JtHashTable.JtGET);

        
        if (inputStream == null)
            return (false);
        
        attributes = retrieveAttributes (inputStream);        
        
        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;
            
            //type = prop.getPropertyType().getName();
            
            if ("java.lang.String".equals(type) ||
                    "java.util.Date".equals(type) ||
                    "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 = (BeanProperty) attributes.processMessage(msg1);

            //if (!(prop.getPropertyType().isPrimitive() || 
            //        String.class.isAssignableFrom (prop.getPropertyType())))
            
            type = prop.getType();
            if (!(prop.isPrimitive() || "String".equals(type) ||
                    "java.lang.String".equals(type) ||
                    "java.util.Date".equals(type) || "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);


            
            if ("java.lang.String".equals(type) || "java.util.Date".equals(type) ||
                    "String".equals(type) || "Date".equals(type))
                type = "String";
         
            oBuffer.append ("public " + type 
                    + " get"+attrName1 + "() {\n");
            oBuffer.append ("  return (" + attrName + ");\n");
            oBuffer.append ("}\n\n");   


            oBuffer.append ("public void " + "set"+attrName1 + "(" + 
                    type + " " + attrName + ") {\n");
            oBuffer.append ("  this." + attrName + "=" + attrName + ";\n");
            oBuffer.append ("}\n\n"); 

            index++;
        } 


        return (true);

    }
    
    
    private String calcJavaType (String type) {
        if (type == null)
            return (null);
        if (type.equals("String") ||
                type.equals("email") || type.equals ("creditCard"))
            return ("String");
        else if (type.equals("date"))
            return ("Date");
        else if (type.equals("integer"))
            return ("int");
        else if (!(type.equals ("float") || type.equals ("long") ||
                type.equals("short") || type.equals ("double") || 
                type.equals ("double") || type.equals("String") ||
                type.equals("boolean") || type.equals("byte")))
            return (type);
        return (null);
        
    }
    
    private boolean generateBeanAttributes (FormBeanForm form) {

        //JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
        //JtHashTable attributes;
        //JtIterator iterator;
        String attrName;
        String attrName1;
        //Element element;
        String type;
        DynaMapping[] attrs;
        int i;



        if (form == null)
            return (false);

        attrs = form.getMappings();

        if (attrs == null || attrs.length == 0)
            return (false);


        //msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        for (i = 0; i < attrs.length; i++) {
            attrName = attrs[i].getName();

            if (attrName == null)
                break;

            type = calcJavaType (attrs[i].getFieldType());

            if (type == null) {
                handleWarning ("generateBeanAttributes: invalid type:" +
                        attrs[i].getFieldType());
                continue;
            }
            
            oBuffer.append ("private " + type + " "+  attrName + ";\n");

            //index++;
        }  

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



        for (i = 0; i < attrs.length; i++) {

            attrName = attrs[i].getName();
            //if (!(prop.getPropertyType().isPrimitive() || 
            //        String.class.isAssignableFrom (prop.getPropertyType())))

            type = calcJavaType (attrs[i].getFieldType());
            if (type == null)
                continue;

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



            oBuffer.append ("public " + type 
                    + " get"+attrName1 + "() {\n");
            oBuffer.append ("  return (" + attrName + ");\n");
            oBuffer.append ("}\n\n");   


            oBuffer.append ("public void " + "set"+attrName1 + "(" + 
                    type + " " + attrName + ") {\n");
            oBuffer.append ("  this." + attrName + "=" + attrName + ";\n");
            oBuffer.append ("}\n\n"); 

            //index++;
        } 


        return (true);

    }
    
    private String retrieveName (String name) {
        int index;
        
        if (name == null)
            return (null);
        
        index = name.lastIndexOf(".");
        
        if (index < 0)
            return (name);
        
        if (index == name.length()-1)
            return (null);
        
        return (name.substring(index + 1));
                   
    }
    
    // Retrieve the package name
    
    private String retrievePackageName (String name) {
        int index;
        
        if (name == null)
            return (null);
        
        index = name.lastIndexOf(".");
        
        if (index < 0)
            return (null);       
        
        return (name.substring(0, index));
                   
    }
    
    
    
    private boolean checkExtension (String name) 
    {
        if (name == null)
            return false;
        
        return (name.endsWith(".java"));
       
    }

    private String generateForm (ActionForm1 form) {
        String name;
        InputStream inputStream = null;
        FormFile theFile;

        if (form == null)
            return null;
        
       // className = (String) form.get ("className");
        className = (String) form.getClassname();

        if (className == null || className.equals ("")) {
            handleUIError ("Please enter a class name.");
        }   
             
        theFile = form.getTheFile();
        
        //handleTrace ("generateForm:" + theFile.getFileName());
        
        if (theFile == null || !checkExtension (theFile.getFileName())) {
            handleUIError ("File must be a java class file (.java)");           
        }
        
        if (getObjErrors() != null) {
            return (null);
        }
        
        try {
            inputStream = theFile.getInputStream();
        } catch (Exception e) {
            handleException (e);
        }
        
        if (inputStream == null) {
            return null;
        }   
        
        /*
        baseClassName = (String) form.get ("baseClassName");

        if (baseClassName == null || baseClassName.equals ("")) {
            handleUIError ("Please enter a base class name.");
        } 
        */
        

        
 /*       
        if (className == null) {
            handleError ("The className attribute needs to be set.");
            return null;
        }


        if (baseClassName == null) {
            handleError ("The baseClassName attribute needs to be set.");
            return null;
        }
*/
        
        name =  retrieveName (className);
        
        if (name == null) {
            handleError ("Invalid class name");
            return null;           
        }
 /*       
        if (formName == null) {
            handleError ("The formName attribute needs to be set.");
            return null;
        }
*/
        pack = retrievePackageName (className);
        
        if (pack != null && !pack.equals(""))
            oBuffer.append("package " + pack +";\n");

⌨️ 快捷键说明

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