使用HTML+Javascript制作代码生成工具



Run in new window

<!-- V1.30  by mostone.jiang 2008.04.15 mostone@hotmail.com-->
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN "
    "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
<html>
<head>
    
<meta   http-equiv = Content-Type   content = "text/html; charset=utf-8" >
    
<title> Convert RecordSet to Bean's Tool </title>
    
<style>
        body{margin:0px;padding:0px;background-color:ghostwhite;}
        div, input, label, option, select, optgroup{padding-left:5px;padding-right:5px;font-family: Lucida Sans Unicode;font-size: 9pt; font-weight: normal}
        textarea{width:100%;height:180px;border:none;font-family:Courier New;font-size: 9pt}
        label, legend{color:gray;font-weight:bold;padding:3px;margin:2px;}
        .submit_button{font-size:14px; font-weight:bold;background-color:SkyBlue;margin-top:5px;}
        .normal{background-color: gray;color:WhiteSmoke;border:none;}
        .selected{background-color:SkyBlue;color:black;font-weight:bold;padding:3px;border:none}
        #pageHeader{background-color:SkyBlue;clear:both;height:45px;border-right:solid 2px powderblue;border-bottom:solid 2px powderblue;}
        #titlePanel{border-left:6px solid royalblue;margin-top:5px;}
        .switch{background-color:cornflowerblue;border:1px solid royalblue;color:white;font-weight:bold;width:16px;height:16px;font-size:10px;text-align:center;overflow:hidden}
        form{margin:0px;padding:0px;}
    
</style>
    
<script   language = "javascript" >
     <!-- 
    
var   currentPanelIndex   =   0 ;
    
var   panels   =   null ;
    
function   switchPanel ( index ){
        
if   ( index   ==   currentPanelIndex ){
            
panels [ index ][ 1 ]. focus ();
            
return   false ;
        
}
        
with ( document.getElementById ( "fn"   +   currentPanelIndex )){
            
style.display   =   "none" ;
        
}
        
with ( document.getElementById ( "fn"   +   index )){
            
style.display   =   "" ;
        
}
        
currentPanelIndex   =   index ;
        
document.getElementById ( "titlePanel" ). innerHTML   =   panels [ index ][ 0 ];
        
panels [ index ][ 1 ]. focus ();
        
return   false ;
    
}
    
    
window.onload   =   function (){
        
document.forms [ 0 ]. input.value   =   document.getElementById ( "source" ). value ;
        
document.forms [ 0 ]. template.value   =   document.getElementById ( "demo" ). value ;  
        
if   (!!( window.attachEvent   &&   ! window.opera )){
            
// show Copy button when in IE
            
document.getElementById ( "copyButton" ). style.display   =   "" ;
        
}
        
        
document.forms [ 1 ]. input.value   =   document.getElementById ( "source1" ). value ;
        
panels   =   [[ "Source code maker" ,   document.forms [ 0 ]. input ],
            
[ "RegExp Test" ,   document.forms [ 1 ]. input ]];
    
}

    
-->
    
</script>
    
<script   language = "javascript" >
     <!-- 
    
var   sourceTool   =   {
        
validate :   function ( form ){
            
if   ( form.input.value == "" )   {
                
alert ( "Please input source." );
                
form.input.focus ();
                
return   false ;
            
}
            
if   ( form.template.value == "" )   {
                
alert ( "Please input template." );
                
form.template.focus ();
                
return   false ;
            
}
            
            
return   true ;
        
},

        
main :   function ( form ){
            
if   ( this.validate ( form )   ==   false ){
                
return   false ;
            
}
            
            
var   template ;
            
try {
                
templateText   =   "template = {"   +  
                    
form.template.value.replace ( /""/g , "///"" )   +  
                    
", content1: /"/", content2: /"/"};" ;
                
eval ( templateText );
            
} catch ( e ){
                
alert ( "template error!/n"   +   e );
                
return   false ;
            
}
            
            
var   tabWidth   =   parseInt ( form.tabWidth.value ,   10 );
            
            
var   src   =   form.input.value ;
            
var   reg   =   /^/s*(/S+)/s+(/w+).*?(?:--/s*(/S+)?)?/s*$/gm ;
            
var   fieldName ,   fieldType ,   fieldComment ;
            
var   javaName ,   javaMethodName ,   javaType ;
            
var   items   =   [];
            
var   index   =   0 ;
            
while   (( arr   =   reg.exec ( src ))   !=   null ){
                
fieldName   =   arr [ 1 ];
                
fieldType   =   arr [ 2 ];
                
fieldComment   =   ( typeof ( arr [ 3 ])   ==   "undefined"   ?   ""   :   arr [ 3 ]);
                
javaName   =   this.getJavaName ( fieldName.replace ( /(^")|("$)/g ,   "" ));
                
javaMethodName   =   this.getJavaMethodName ( javaName );
                
javaType   =   this.getJavaType ( fieldType );
                
                
items [ index ++]   =   { fieldName :   fieldName ,   fieldType :   fieldType ,   fieldComment :   fieldComment ,  
                    
javaName :   javaName ,   javaMethodName :   javaMethodName ,   javaType :   javaType };
            
}
            
            
for ( var   i = 0 ;   i < items.length ;   i ++){
                
if   ( i   ==   0 ){
                    
template.content1   +=   this.processTemplate ( items [ i ],   ( template.p1.first == null   ?   template.p1.content   :   template.p1.first ),   tabWidth );
                    
template.content2   +=   this.processTemplate ( items [ i ],   ( template.p2.first == null   ?   template.p2.content   :   template.p2.first ),   tabWidth );
                
} else   if   ( i   ==   ( items.length   -   1 )){
                    
template.content1   +=   this.processTemplate ( items [ i ],   ( template.p1.last == null   ?   template.p1.content   :   template.p1.last ),   tabWidth );
                    
template.content2   +=   this.processTemplate ( items [ i ],   ( template.p2.last == null   ?   template.p2.content   :   template.p2.last ),   tabWidth );
                
} else {
                    
template.content1   +=   this.processTemplate ( items [ i ],   template.p1.content ,   tabWidth );
                    
template.content2   +=   this.processTemplate ( items [ i ],   template.p2.content ,   tabWidth );
                
}
            
}
            
            
with ( template ){
                
form.output.value   =   content1   +   content2 ;
            
}
        
},
        
        
processTemplate :   function ( item ,   content ,   tabWidth ){
            
if   ( content   ==   null ){
                
return   "" ;
            
}
            
            
var   result   =   content ;
            
result   =   result.replace ( //$fieldName/g ,   item.fieldName );
            
result   =   result.replace ( //$fieldType/g ,   item.fieldType );
            
result   =   result.replace ( //$fieldComment/g ,   item.fieldComment );
            
result   =   result.replace ( //$javaName/g ,   item.javaName );
            
result   =   result.replace ( //$javaMethodName/g ,   item.javaMethodName );
            
result   =   result.replace ( //$javaType/g ,   item.javaType );
            
result   =   this.replacePaddingComment ( result ,   item ,   tabWidth );
            
return   result ;
        
},
        
        
replacePaddingComment :   function ( value ,   item ,   tabWidth ){
            
var   arr   =   //$/[fieldComment/((/d+)/)/((.+)/)/]/ . exec ( value );  
            
if   ( arr   ==   null ){
                
return   value ;
            
}
            
if   (! item.fieldComment ){
                
return   value.substring ( 0 ,   arr.index )   +   value.substr ( arr.index   +   arr [ 0 ]. length );
            
}
            
var   tabString   =   "" ;
            
for ( var   i = 0 ;   i < tabWidth ;   i ++){
                
tabString   +=   "1" ;
            
}
            
var   result   =   value.substring ( 0 ,   arr.index );
            
var   currentLength   =   result.replace ( /[^/u0000-/u00FF]/g ,   "11" ). replace ( //t/g ,   tabString ). length ;
            
var   paddingLength   =   parseInt ( arr [ 1 ],   10 );
            
if   ( currentLength   >   paddingLength ){
                
paddingLength   =   currentLength   +   1 ;
            
}
            
for ( var   i = currentLength ;   i < paddingLength ;   i ++){
                
result   +=   " " ;
            
}
            
result   +=   arr [ 2 ]   +   item.fieldComment   +   value.substr ( arr.index   +   arr [ 0 ]. length );
            
return   result ;
        
},
        
        
getJavaName :   function ( fieldName ){
            
fieldName   =   fieldName.replace ( /(^_*)|(_*$)/ ,   "" );
            
var   result   =   "" ;
            
var   pos   =   0 ;
            
var   tmp ;
            
do {
                
tmp   =   fieldName.indexOf ( "_" ,   pos );
                
if   ( pos   >   0 ){
                    
result   +=   fieldName.charAt ( pos ). toUpperCase ();
                    
pos ++;
                
}
                
if   ( tmp   ==   - 1 ){
                    
result   +=   fieldName.substr ( pos );
                
} else {
                    
result   +=   fieldName.substring ( pos ,   tmp );
                
}
                
pos   =   tmp   +   1 ;
            
} while ( tmp   !=   - 1 )
            
return   result ;
        
},
        
        
getJavaMethodName :   function ( javaName ){
            
if   ( javaName.length   ==   1 ){
                
return   javaName.toUpperCase ();
            
}
            
return   javaName.charAt ( 0 ). toUpperCase ()   +   javaName.substr ( 1 );
        
},
        
        
getJavaType :   function ( fieldType ){
            
switch ( fieldType ){
                
case   "serial" :
                
case   "integer" :
                    
return   "Integer" ;
                
case   "numeric" :
                    
return   "BigDecimal" ;
                
case   "boolean" :
                    
return   "Boolean" ;
                
case   "timestamp" :
                    
return   "Date" ;
                
default :
                    
return   "String" ;
            
}
        
},
        
        
setTemplate :   function ( templateId ){
            
if   ( templateId ){
                
document.forms [ 0 ]. template.value   =   document.getElementById ( templateId ). value ;
            
}
        
},
        
        
copyResult :   function ( value ){
            
if   ( value ){
                
clipboardData.setData ( "text" ,   value );
            
}
        
}
    
};
    
-->
    
</script>
    
<script   language = "javascript" >
     <!-- 
    
var   regexpTool   =   {
        
main :   function ( form ,   isReplace ){
            
if   ( this.validate ( form ,   isReplace )== false ){
                
return   false ;
            
}
            
            
var   outputString   =   "" ;
            
var   rowIndex   =   1 ;
            
var   arr   =   null ;
            
var   reg   =   new   RegExp ( form.regexpString.value ,   "mg" );             
            
var   lastIndex   =   0 ;
            
var   tmp   =   null ;
            
var   tmp1   =   null ;
            
while   (( arr   =   reg.exec ( form.input.value ))   !=   null ){
                
if   ( isReplace ){
                    
outputString   +=   form.input.value.substring ( lastIndex ,   arr.index );
                    
tmp   =   form.replaceString.value ;                  
                    
for ( var   i = 0 ;   i < arr.length ;   i ++){
                        
do {
                            
tmp1   =   tmp ;
                            
tmp   =   tmp.replace ( "$"   +   i ,   arr [ i ]);
                        
} while ( tmp != tmp1 );
                    
}
                    
outputString   +=   tmp ;
                    
lastIndex   =   arr.index   +   arr [ 0 ]. length ;
                
} else {
                    
lastIndex   =   arr.index   +   arr [ 0 ]. length ;
                    
outputString   +=   "No."   +   rowIndex   +  
                        
//"/n/tinput=" + arr.input +
                        
"/n/tindex="   +   arr.index   +
                        
"/n/tlastIndex="   +   lastIndex   +
                        
"/n" ;
                    
for ( var   i = 0 ;   i < arr.length ;   i ++){
                        
outputString   +=   "/t/t$"   +   i   +   "="   +   arr [ i ]   +   "/n" ;
                    
}
                
}
                
rowIndex ++;
            
}
            
if   ( isReplace ){
                
outputString   +=   form.input.value.substr ( lastIndex );
            
}
            
form.output.value   =   outputString ;
        
},
        
        
validate :   function ( form ,   isReplace ){
            
if   ( form.input.value == "" )   {
                
alert ( "Please input source." );
                
form.input.focus ();
                
return   false ;
            
}
            
if   ( form.regexpString.value == "" )   {
                
alert ( "Please input regexp string." );
                
form.regexpString.focus ();
                
return   false ;
            
}
            
if   ( isReplace   &&   form.replaceString.value == "" ){
                
alert ( "Please input replace string." );
                
form.replaceString.focus ();
                
return   false ;
            
}
            
            
return   true ;
        
}
    
}
    
-->
    
</script>
</head>

<body>
<div   id = "pageHeader" >
    
<div   id = "titlePanel"   style = "width:60%;float:left;padding-top:10px;font-size:18pt;font-weight:bold;color:dimgray;" > Source code Maker </div>
    
<div   style = "width:65px;float:right;padding-top:25px;" >
        
<input   type = "button"   value = "1"   onclick = "return switchPanel(0)"   class = "switch"   onmouseover = "this.style.backgroundColor='deepskyblue'"   onmouseout = "this.style.backgroundColor='cornflowerblue'" />
        
<input   type = "button"   value = "2"   onclick = "return switchPanel(1)"   class = "switch"   onmouseover = "this.style.backgroundColor='deepskyblue'"   onmouseout = "this.style.backgroundColor='cornflowerblue'" />
    
</div>
</div>
<div   id = "fn0" >
    
<form>
        
<fieldset>
            
<legend> Fields List </legend>
            
<textarea   name = "input"   onfocus = "this.select()" ></textarea>
        
</fieldset>
        
<fieldset>
            
<legend> Options </legend>
            
<label> Template </label>
            
<select   onchange = "sourceTool.setTemplate(this.value)" >
                
<option   value = "demo"   selected = "selected" > Demo </option>
                
<optgroup   LABEL = "--------------------" >
                    
<option   value = "insert" > Insert SQL </option>
                    
<option   value = "update" > Update SQL </option>
                    
<option   value = "insertSingle" > Insert SQL[Single row] </option>
                    
<option   value = "updateSingle" > Update SQL[Single row] </option>
                    
<option   value = "sqlCondition" > SQL Condition </option>
                
</optgroup>
                
<optgroup   LABEL = "--------------------" >
                    
<option   value = "set" > Set SQL Parameters </option>
                    
<option   value = "get" > Read SQL Result </option>
                
</optgroup>
                
<optgroup   LABEL = "--------------------" >
                    
<option   value = "json" > JSON </option>
                
</optgroup>
            
</select>
            
<label   for = "tabWidth"   style = "margin-left: 50px" > TAB width </label>
            
<input   type = "text"   name = "tabWidth"   id = "tabWidth"   value = "4"   size = "2"   maxlength = "1" />
        
</fieldset>
        
<fieldset>
            
<legend> Template </legend>
            
<textarea   name = "template" ></textarea>
        
</fieldset>
        
<fieldset>
            
<legend> Output source </legend>
            
<textarea   name = "output"   readonly = "readonly" ></textarea>
        
</fieldset>
        
<div   align = "right"   style = "float:right" >
            
<input   type = "button"   value = "Run Make"   class = "submit_button"   onclick = "sourceTool.main(this.form)" />
            
<input   type = "button"   value = "Copy to clipboard"   class = "submit_button"   onclick = "sourceTool.copyResult(this.form.output.value)"   id = "copyButton"   style = "display:none" />
        
</div>
    
</form>
</div>
<div   id = "fnValues0"   style = "display:none" >
<textarea   id = "source" >
  cmpno character varying(4) NOT NULL, -- 公司编号
  deptno character varying(20) NOT NULL DEFAULT 'etime-y'::character varying, -- 部门编号
  etno numeric(6) NOT NULL, -- 职务类别
  empno character varying(20) NOT NULL, -- 员工编号
  "year" character varying(4) NOT NULL, -- 年
  yearmonth character varying(6) NOT NULL, -- 年月
  date character varying(8) NOT NULL, -- 年月日
  plantime numeric(6), -- 计划时间
  planmoney numeric(8), -- 金额
  registtime timestamp without time zone NOT NULL DEFAULT now(), -- 登录时间
  registuser character varying(20) NOT NULL DEFAULT 'SYSTEM'::character varying, -- 登录者编号
  updatetime timestamp without time zone NOT NULL DEFAULT now(), -- 更新时间
  updateuser character varying(20) NOT NULL DEFAULT 'SYSTEM'::character varying, -- 更新者编号
</textarea>
<textarea   id = "demo" >
p1:{
    first: "This is a Demo: /nThis is first leader:/n/t/tfieldName: ""$fieldName"",  fieldType: ""$fieldType"",  fieldComment: ""$fieldComment""      $[fieldComment(108)(// -- )]/n",
    content: "/t/tfieldName: ""$fieldName"",  fieldType: ""$fieldType"",  fieldComment: ""$fieldComment""      $[fieldComment(108)(// -- )]/n",
    last: "/t/tfieldName: ""$fieldName"",  fieldType: ""$fieldType"",  fieldComment: ""$fieldComment""      $[fieldComment(108)(// -- )]/n"
},
p2:{
    first: "This is secend leader:/n/t/tjavaName: ""$javaName"",  javaMethodName: ""$javaMethodName"",  javaType: ""$javaType""       $[fieldComment(108)(// -- )]/n",
    content: "/t/tjavaName: ""$javaName"",  javaMethodName: ""$javaMethodName"",  javaType: ""$javaType""       $[fieldComment(108)(// -- )]/n",
    last: "/t/tjavaName: ""$javaName"",  javaMethodName: ""$javaMethodName"",  javaType: ""$javaType""       $[fieldComment(108)(// -- )]/n"
}
</textarea>
<textarea   id = "insert" >
p1:{
    first: "INSERT INTO XXXXX($fieldName, $[fieldComment(88)(-- )]/n",
    content: "/t/t$fieldName, $[fieldComment(88)(-- )]/n",
    last: "/t/t$fieldName) $[fieldComment(88)(-- )]/n"
},
p2:{
    first: "VALUES(?, $[fieldComment(88)(-- )]/n",
    content: "/t/t?, $[fieldComment(88)(-- )]/n",
    last: "/t/t?) $[fieldComment(88)(-- )]/n"
}
</textarea>
<textarea   id = "update" >
p1:{
    first: "UPDATE XXXXX SET $fieldName = ?, $[fieldComment(88)(-- )]/n",
    content: "/t/t$fieldName = ?, $[fieldComment(88)(-- )]/n",
    last: "/t/t$fieldName = ? $[fieldComment(88)(-- )]/nWHERE "
},
p2:{
    first: null,
    content: null,
    last: null
}
</textarea>
<textarea   id = "insertSingle" >
p1:{
    first: "INSERT INTO XXXXX($fieldName, ",
    content: "$fieldName, ",
    last: "$fieldName)"
},
p2:{
    first: " VALUES(?, ",
    content: "?, ",
    last: "?)"
}
</textarea>
<textarea   id = "updateSingle" >
p1:{
    first: "UPDATE XXXXX SET $fieldName = ?, ",
    content: "$fieldName = ?, ",
    last: "$fieldName = ? WHERE "
},
p2:{
    first: null,
    content: null,
    last: null
}
</textarea>
<textarea   id = "sqlCondition" >
p1:{
    first: "WHERE $fieldName = ? ",
    content: "AND $fieldName = ? ",
    last: null
},
p2:{
    first: null,
    content: null,
    last: null
}
</textarea>
<textarea   id = "set" >
p1:{
    first: null,
    content: "/t/tpStmt.set$javaType(parameterIndex++, model.get$javaMethodName()); $[fieldComment(88)(// )]/n",
    last: null
},
p2:{
    first: null,
    content: null,
    last: null
}
</textarea>
<textarea   id = "get" >
p1:{
    first: null,
    content: "/t/tmodel.set$javaMethodName(rs.get$javaType(columnIndex++)); $[fieldComment(88)(// )]/n",
    last: null
},
p2:{
    first: null,
    content: null,
    last: null
}
</textarea>
<textarea   id = "json" >
p1:{
    first: "/tvar json = transport.responseText.evalJSON();/n/twith(document.forms[0]){/n/t/tif (json.$javaName) $javaName.value = json.$javaName; $[fieldComment(88)(// )]/n",
    content: "/t/tif (json.$javaName) $javaName.value = json.$javaName; $[fieldComment(88)(// )]/n",
    last: "/t/tif (json.$javaName) $javaName.value = json.$javaName; $[fieldComment(88)(// )]/n/t}"
},
p2:{
    first: null,
    content: null,
    last: null
}
</textarea>
</div>
<div   id = "fn1"   style = "display:none" >
    
<form>
        
<fieldset>
            
<legend> Source String </legend>
            
<textarea   name = "input"   onfocus = "this.select()" ></textarea>
        
</fieldset>
        
<fieldset>
            
<legend> RegExp </legend>
            
<input   type = "text"   name = "regexpString"   onfocus = "this.select()"   style = "width:95%"   value = "([/w-]+)/b/s*:/s*([^;/n/r]*)" />
        
</fieldset>
        
<fieldset>
            
<legend> Replace String </legend>
            
<input   type = "text"   name = "replaceString"   onfocus = "this.select()"   style = "width:95%"   value = "property='$1' value='$2'" />
        
</fieldset>
        
<fieldset>
            
<legend> Output source </legend>
            
<textarea   name = "output"   readonly = "readonly" ></textarea>
        
</fieldset>
        
<div   align = "right"   style = "float:right" >
            
<input   type = "button"   value = "Excute"   class = "submit_button"   onclick = "regexpTool.main(this.form)" />
            
<input   type = "button"   value = "Replace"   class = "submit_button"   onclick = "regexpTool.main(this.form, true)" />
        
</div>
    
</form>
</div>
<div   id = "fnValues1"   style = "display:none" >
<textarea   id = "source1" >
.switch{
    background-color:cornflowerblue;
    border:1px solid royalblue;
    color:white;
    font-weight:bold;
    width:16px;
    height:16px;
    font-size:10px;
    text-align:center;
    overflow:hidden
}
</textarea>
</div>
<div   style = "margin:3px 0px 0px 0px;width:150px;float:left" >
Power by mostone.jiang
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值