Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
利用awk自动生成DTO

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

      大多用于ejb开发的工具都支持dto的自动生成,前提是必须已经存在对应的实体bean。在没有实体bean时,就得我们自己敲那些繁琐无聊毫无意义的代码。这种情况下,这个awk script也许可以帮点忙。 如果你不了解awk,可以去
     http://www-900.ibm.com/developerWorks/cn/linux/shell/awk/awk-1/index.shtml
     http://www-900.ibm.com/developerWorks/cn/linux/shell/awk/awk-2/index.shtml
     http://www-900.ibm.com/developerWorks/cn/linux/shell/awk/awk-3/index.shtml
看看.  如果用的是windows,要想运行这个script,需要去down一个名为awk.exe的解释程序。 

对文本要求的格式:
    第一行 是 dto 的名字,
    其余行 是"类型-变量名"对, 注意别加分号
    如下例:
    User_DTO
    int          id
    String       name
    Collection   friends
以下斜体部分是script正文:

BEGIN{
    count =0 ;
}
{
    if(NF==2)
    {
       arr[count,0] = $1;
       arr[count,1] = $2;
   
       #print arr[count,0], arr[count,1], count;
       count++;
    }

    if(NF==1)
      dto = $1;
}

END{
    printf("/* DTO created by awk*/\n\n");
    printf("
/*@todo Complete package & import here*/\n\n");
    printf("public class %s implements Serializable\n{\n", dto);

    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];
      
       printf("\tprivate %s %s;\n", arrx, x);
    }

    printf("\n\n");

    printf("\tpublic %s()\n\t{\n\t}\n\n", dto);

    printf("\tpublic %s(", dto);
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];      
       printf("%s %s", arrx, x);

       if(i!=count-1)
         printf(", ");
    }
    printf(")\n\t{\n");

    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];      
       printf("\t\tthis.%s = %s;\n", x, x);
    }

    printf("\t}\n\n");


    # setters & getters
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];
      
       xHead = toupper(substr(x,1,1));
       xBody= substr(x,2,length(x)-1);
       printf("\tpublic void set%s%s(%s %s)\n\t{\n", xHead, xBody, arrx, x);
       printf("\t\tthis.%s = %s;\n", x, x);
       printf("\t}\n\n");

       printf("\tpublic %s get%s%s()\n\t{\n", arrx, xHead, xBody, arrx, x);
       printf("\t\treturn %s;\n", x);
       printf("\t}\n\n");
    }
   
    #  equals
    printf("\n\n");
    printf("\tpublic boolean equals(Object obj)\n\t{\n");
    printf("\t\tif (obj != null)\n\t\t{\n");
    printf("\t\t\tif (this.getClass().equals(obj.getClass()))\n\t\t\t{\n");
    printf("\t\t\t\t%s that = (%s) obj;\n", dto, dto);
    printf("\t\t\t\treturn \n");
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];      
       xHead = toupper(substr(x,1,1));
       xBody= substr(x,2,length(x)-1);
       if(arrx=="byte" || arrx=="char" || arrx=="int" || arrx=="long" || arrx=="float" || arrx=="double" || arrx=="boolean" )
       {
          printf("\t\t\t\t\tthis.get%s%s() == that.get%s%s() ", xHead, xBody, xHead, xBody);
       }else
       {
          printf("\t\t\t\t\t(((this.get%s%s() == null) && (that.get%s%s() == null)) ||\n",  xHead, xBody, xHead, xBody);
          printf("\t\t\t\t\t\t(this.get%s%s() != null && this.get%s%s().equals(that.get%s%s())))",  xHead, xBody, xHead, xBody, xHead, xBody);
       }
       if(i!=count-1)
         printf(" && \n");
       else
         printf(";\n");
    }
    printf("\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n");


    #  hashCode()
    printf("\tpublic int hashCode()\n\t{\n");
    printf("\t\treturn (");
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       if(i!=0)
         printf(" + ");

       printf("\"\" + %s", x);
    }
    printf(").hashCode();");
    printf("\n\t}\n\n");

   
    # toString()
    printf("\tpublic String toString()\n\t{\n");
    printf("\t\treturn ");
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       if(i==0)
         printf("\"\" + ");
       else
         printf(" + \",\" + ");

       printf("%s", x);
    }
    printf(";");
    printf("\n\t}\n\n");  

    printf("}");
}

#########################################################
#   如果有bug,请告诉我,thanks! :)
#   Ed Yan, msn/email:
[email protected]
#   2004.03.30
#########################################################




相关文章

相关软件