以前写过一段C#,苦于编译才能用。这样的小工具最好是用脚本语言来编写,易于执行,也易于修改。
js 代码 convert.js
--------------------------------------------------
String.prototype.trim=function(){ return this.replace(/(^s*)|(s*$)/g, ""); } String.prototype.ltrim=function(){ return this.replace(/(^s*)/g,""); } String.prototype.rtrim=function(){ return this.replace(/(s*$)/g,""); } String.prototype.startWith=function(str){ var reg = new RegExp("^" + str); return reg.test(this); } var ConvertCode = { content: "", run: function(){ console.writeline(this.content); if( !this.content) return; var lines = this.content.split(' '); var ret = ""; for(var i=0; i<lines.length; i++) { var dataInfo = this.convertLine(lines[i]); if(!dataInfo) continue; ret = ret + "///<summary> "; ret = ret + "/// "; ret = ret + "///<summary> "; ret = ret + '[DBColumn("' + dataInfo.column + '")] '; ret = ret + "public " + dataInfo.dataType + " " + dataInfo.name + "{get;set;} "; } return ret; }, convertLine: function(line){ line = line.trim(); if(line.length == 0) return null; var tokens = line.split(/s+/); if(tokens.length <= 1) return null; return { column: tokens[0], name: this.toNormalName(tokens[0]), dataType: this.toDataType(tokens[1]) }; }, toNormalName: function(raw) { var tokens = raw.split('_'); var ret = ""; for(var i=0; i<tokens.length; i++) { ret = ret + tokens[i].substr(0,1).toUpperCase() + tokens[i].substr(1); } return ret; }, toDataType: function(d) { if(!d) return null; if(d.startWith("varchar") || d.startWith("char")) return "string"; if(d.startWith("int")) return "int"; if(d.startWith("number")) return "decimal"; if(d.startWith("date")) return "DateTime"; return null; } }
html 代码用于调用它
----------------------------------
<html> <head> <meta charset='gbk' /> <script type='text/javascript' src="convert.js"></script> <script language="javascript" type='text/javascript'> var doparse = function(){ var parser = ConvertCode; parser.content = document.getElementById('content').value; var result = parser.run(content); document.getElementById('divResult').value = result; console.writeline( document.getElementById('divResult').value); } </script> </head> <body> <div><input type="button" value="转换" onclick="javascript:doparse();" style="50px; height: 30px;"/></div> <div style="float:left; 500px;"> <textarea id='content' style="500px; height:600px;"></textarea> </div> <div style="float:left; 500px; margin-left:30px;" > <textarea id="divResult" style="500px; height:600px;"></textarea> </div> </body> </html>