在J2EE開(kāi)發(fā)中,Hibernate生成的的DAO類通常要手動(dòng)提取接口定義,并寫(xiě)到新文件中。本文將使用VBS開(kāi)實(shí)現(xiàn)接口自動(dòng)批量生成杏慰。
現(xiàn)實(shí)原理:使用VBS中的正則表達(dá)式,對(duì)DAO類中的內(nèi)容整理炼鞠,生成接口缘滥、實(shí)現(xiàn)接口。
模擬情景:Hibernate生成的的DAO類位于db包谒主。
一朝扼、移動(dòng)DAO類
選中所有DAO類,拖放到新新建的db.dao.impl包中霎肯,勾上“Update references to the moved element(s)”擎颖。
二、生成接口
打開(kāi)記事本观游,新建文本如下搂捧,保存為“formInterface.vbs”。將所有DAO類懂缕,拖放到這個(gè)文件上允跑,得到接口實(shí)現(xiàn)文件,放置到db.dao包中搪柑。
Set objArgs = WScript.Arguments '獲取打開(kāi)腳本文件時(shí)的參數(shù)
If objArgs.Count>0 Then
dim str
For I = 0 To objArgs.Count - 1
FileUrl = objArgs(I) '獲取拖放的文件路徑
str = readfile(FileUrl) '讀取文件內(nèi)容
Set regEx = New RegExp '建立正則表達(dá)式聋丝。
regEx.Pattern = "public class " '設(shè)置正則表達(dá)式。
regEx.IgnoreCase = True '設(shè)置是否區(qū)分大小寫(xiě)工碾。
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 = "http://[^\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) '檢查是否有兩層花括號(hào)
while res
regEx.Pattern = " *{[^{}]*}"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, ";") '去掉最內(nèi)層花括號(hào)中,并換為一個(gè)“;”
regEx.Pattern = "{[^{]*{"
regEx.IgnoreCase = True
res = regEx.test(Str) '再次檢查是否有兩層花括號(hào)
wend
res = true
while res
regEx.Pattern = "(\([^\(\)]*)(\n)([^\(\)]*\))"
regEx.IgnoreCase = True
Str = regEx.Replace(Str, "$1$3") '去掉去掉括號(hào)中的回車符倚喂,防止函數(shù)的參數(shù)列表中的有換行每篷,影響下面的整理
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, "") '去掉靜態(tài)的方法或成員
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開(kāi)頭的行
regEx.Pattern = "\\(\w*DAO.java)"
regEx.IgnoreCase = True
FileUrl = regEx.replace(FileUrl, "\I$1") '取出文件名,前加“I”作為新文件名
Call WriteToFile(FileUrl, Str) '寫(xiě)到新文件
'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
三端圈、實(shí)現(xiàn)接口
打開(kāi)記事本焦读,新建文本如下,保存為“implements.vbs”舱权。將所有DAO類矗晃,拖放到這個(gè)文件上。
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") '在類定義前加一個(gè)接口的引入宴倍,后加一個(gè)實(shí)現(xiàn)接口
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刷新即可张症。