在J2EE开发中,Hibernate生成的的DAO类通常要手动提取接口定义,并写到新文件中。本文将使用VBS开实现接口自动批量生成。
现实原理:使用VBS中的正则表达式,对DAO类中的内容整理,生成接口、实现接口。
模拟情景:Hibernate生成的的DAO类位于db包。
一、移动DAO类
选中所有DAO类,拖放到新新建的db.dao.impl包中,勾上“Update references to the moved element(s)”。
二、生成接口
打开记事本,新建文本如下,保存为“formInterface.vbs”。将所有DAO类,拖放到这个文件上,得到接口实现文件,放置到db.dao包中。
Set objArgs = WScript.Arguments '获取打开脚本文件时的参数
If objArgs.Count>0 Then
dim str
For I = 0 To objArgs.Count - 1
FileUrl = objArgs(I) '获取拖放的文件路径
str = readfile(FileUrl) '读取文件内容
Set regEx = New RegExp '建立正则表达式。
regEx.Pattern = "public class " '设置正则表达式。
regEx.IgnoreCase = True '设置是否区分大小写。
Str = regEx.Replace(Str, "public interface I") '替换;将类定义改为接口定义
regEx.Pattern = " extends HibernateDaoSupport "
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "") '去掉继承
regEx.Pattern = "db\.dao\.impl"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "db.dao") '改变包的位置
regEx.Pattern = "/*[^/]*\*/"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "") '去掉注释
regEx.Pattern = "//[^\n]*\n"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "") '去掉注释
regEx.Pattern = "import java\.util\.Set;"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "") '去掉Set类的引入
regEx.Pattern = "{[^{]*{"
regEx.IgnoreCase = True
res = regEx.test(Str) '检查是否有两层花括号
while res
regEx.Pattern = " *{[^{}]*}"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, ";") '去掉最内层花括号中,并换为一个“;”
regEx.Pattern = "{[^{]*{"
regEx.IgnoreCase = True
res = regEx.test(Str) '再次检查是否有两层花括号
wend
res = true
while res
regEx.Pattern = "(\([^\(\)]*)(\n)([^\(\)]*\))"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "$1$3") '去掉去掉括号中的回车符,防止函数的参数列表中的有换行,影响下面的整理
regEx.Pattern = "(\([^\(\)]*)(\n)([^\(\)]*\))"
regEx.IgnoreCase = True
res = regEx.test(Str) '再次检查
wend
res = true
while res
regEx.Pattern = "p\w* static[^\n]*;\n"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "") '去掉静态的方法或成员
regEx.Pattern = "p\w* static[^\n]*;\n"
regEx.IgnoreCase = True
res = regEx.test(Str) '再次检查
wend
res = true
while res
regEx.Pattern = "import org[^\n]*;\n"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "") '去掉org包的引入
regEx.Pattern = "import org[^\n]*;\n"
regEx.IgnoreCase = True
res = regEx.test(Str) '再次检查
wend
res = true
while res
regEx.Pattern = "\n\s*(\n)"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "$1") '去掉空行
regEx.Pattern = "\n\s*(\n)"
regEx.IgnoreCase = True
res = regEx.test(Str) '再次检查
wend
regEx.Pattern = "protect[^\n]*\n"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "") '去掉protect开头的行
regEx.Pattern = "\\(\w*DAO.java)"
regEx.IgnoreCase = True
FileUrl = regEx.replace(FileUrl, "\I$1") '取出文件名,前加“I”作为新文件名
Call WriteToFile(FileUrl, Str) '写到新文件
'Str = Str&vbCrLf&FileUrl
Next
'test = createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
'Call WriteToFile(test+"\filesPath.txt", Str)
Else
MsgBox "drag files", vbInformation, "温馨提示:"
End If
Function ReadFile(FileUrl)
Dim Str
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = "gb2312"
stm.Open
stm.loadfromfile FileUrl
Str = stm.readtext
stm.Close
Set stm = Nothing
ReadFile = Str
End Function
Function WriteToFile (FileUrl, Str)
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = "gb2312"
stm.Open
stm.WriteText Str
stm.SaveToFile FileUrl, 2
stm.flush
stm.Close
Set stm = Nothing
End Function
三、实现接口
打开记事本,新建文本如下,保存为“implements.vbs”。将所有DAO类,拖放到这个文件上。
Set objArgs = WScript.Arguments
If objArgs.Count>0 Then
dim str
For I = 0 To objArgs.Count - 1
FileUrl = objArgs(I)
str = readfile(FileUrl)
set regex = new regexp
regEx.Pattern = ";\n[^;]*public class (\w*DAO) extends HibernateDaoSupport "
regEx.IgnoreCase = True
Str = regEx.Replace(Str, ";"&vbCrLf&"import db.dao.I$1;"&vbCrLf&"public class $1 extends HibernateDaoSupport implements I$1") '在类定义前加一个接口的引入,后加一个实现接口
Call WriteToFile(FileUrl, Str)
'Str = Str&vbCrLf&FileUrl
Next
'test = createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
'Call WriteToFile(test+"\filesPath.txt", Str)
Else
MsgBox "drag files", vbInformation, "温馨提示:"
End If
Function ReadFile(FileUrl)
Dim Str
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = "gb2312"
stm.Open
stm.loadfromfile FileUrl
Str = stm.readtext
stm.Close
Set stm = Nothing
ReadFile = Str
End Function
Function WriteToFile (FileUrl, Str)
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = "gb2312"
stm.Open
stm.WriteText Str
stm.SaveToFile FileUrl, 2
stm.flush
stm.Close
Set stm = Nothing
End Function
最后回到MyEclipse刷新即可。