自動代碼複製工具,代碼複製工具
工具用途:
平時開發程式的時候,如果要在系統裡新增一個模組,通常會複製已有的代碼檔案,然後再做一些修改。如果已有一個User的增刪改查模組,此時要新增一個對Student的類似模組,我們就會複製User的Action/Dao/jsp頁面等所有java類,然後對裡面的內容進行修改。
對於上面的步驟,當再次新增其他模組時,還會再次重複執行:
1.逐個複製各個檔案
2.按關鍵字進行替換(如上面範例中,需要將User替換為Student)
3.根據業務差異,進行修改
本工具用於自動完成上面步驟中前兩個步驟,達到加快代碼開發的目的。
PS:如果代碼模組是基於一種標準的SSH模式來編寫的,自然可以採用標準的代碼產生工具來產生全套代碼,網上也有很多類似的工具,但是縱觀各個不同公司的代碼,風格各異,很難有一個統一的代碼產生工具來產生合適的全套代碼,所以這種複製修改的方法反而更適用一些。
工具代碼實現:
package cn.jerryhouse.util.cc;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.util.LinkedList;import java.util.List;public class CodeCopier {private List<String> srcFileList = new LinkedList<String>();private String srcBaseDir;private String destBaseDir;private List<ReplacePair> replacePairList = new LinkedList<ReplacePair>();private final String NEW_LINE = System.getProperty("line.separator");public void process(){for(String filePath: srcFileList){String destFilePath = getDestFilePath(filePath);copyAndModifyFile(filePath,destFilePath);}}private String getDestFilePath(String srcFilePath){File srcFile = new File(srcFilePath);String srcFileName = srcFile.getName();String srcFileDir = srcFile.getParent();String destFileName = getDestFileName(srcFileName);String destFilePath = this.destBaseDir+srcFileDir.substring(srcBaseDir.length())+File.separator+destFileName;return destFilePath;}private String getDestFileName(String srcFilePath){return updateContentByPattern(srcFilePath);}private void copyAndModifyFile(String srcFilePath,String destFilePath){try {mkdirsForFilePath(destFilePath);BufferedReader br = new BufferedReader(new FileReader(srcFilePath));BufferedWriter bw = new BufferedWriter(new FileWriter(destFilePath));String line;while((line=br.readLine())!=null){String updatedLine = updateContentByPattern(line);bw.write(updatedLine+NEW_LINE);}br.close();bw.close();} catch (Exception e) {e.printStackTrace();}}private void mkdirsForFilePath(String filePath){File file = new File(filePath);file.getParentFile().mkdirs();}private String updateContentByPattern(String content){for(ReplacePair replacePair: replacePairList){content = content.replaceAll(replacePair.getToReplacePattern(), replacePair.getReplacement());}return content;}public void addReplacePair(String toReplacePattern,String replacement){replacePairList.add(new ReplacePair(toReplacePattern,replacement));}public void addSrcFile(String srcFilePath){srcFileList.add(srcFilePath);}public void setSrcBaseDir(String srcBaseDir) {this.srcBaseDir = srcBaseDir;}public void setDestBaseDir(String destBaseDir) {this.destBaseDir = destBaseDir;}}
package cn.jerryhouse.util.cc;public class ReplacePair {private String toReplacePattern;private String replacement;public ReplacePair(String toReplacePattern,String replacement){this.toReplacePattern = toReplacePattern;this.replacement = replacement;}public String getToReplacePattern() {return toReplacePattern;}public void setToReplacePattern(String toReplacePattern) {this.toReplacePattern = toReplacePattern;}public String getReplacement() {return replacement;}public void setReplacement(String replacement) {this.replacement = replacement;}}
測試代碼:
package cn.jerryhouse.util.cc.test;import org.junit.Test;import cn.jerryhouse.util.cc.CodeCopier;public class CodeCopierTest {@Testpublic void testProcess() {CodeCopier codeCopier = new CodeCopier();codeCopier.setSrcBaseDir("D:/tmp/sshExample2");codeCopier.setDestBaseDir("D:/tmp1/sshExample2");codeCopier.addSrcFile("D:/tmp/sshExample2/src/cn/jerry/ssh/mdv/action/UserAction.java");codeCopier.addSrcFile("D:/tmp/sshExample2/src/cn/jerry/ssh/mdv/dao/UserDaoImp.java");codeCopier.addReplacePair("User", "Student");codeCopier.addReplacePair("user", "student");codeCopier.process();}}
複製檔案和源檔案對比:
自動複製代碼 自動IE瀏覽器運行代碼 急 重謝
{摳鼻}。。。。 <SCRIPT language=javascript>
// 自動 COPY 代碼開始
function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function JM_cc(ob){
var obj=MM_findObj(ob); if (obj) {
obj.select();js=obj.createTextRange();js.execCommand("Copy");}
alert("複製成功!您可以將本頁推薦給您QQ或者論壇上的朋友閱讀!");
}
// 自動 COPY 代碼結束
document.write('<br><b><font color=red style=font-size:14px>推薦給您的朋友閱讀:</font></b><input name="page_url" value="'+window.location.href+'" size="64"> <input type="button" name="Button" class="button1" style=color:red value="點擊這裡複製本頁地址推薦給您QQ或者論壇上的朋友閱讀!" onClick=JM_cc("page_url")>');
</SCRIPT>
這就是複製代碼,根據需要自行調整。
網頁製作自動複製代碼問題?
<script type="text/javascript">
function copyUrl2()
{
var Url2=document.getElementById("biao1");
Url2.select(); //選擇對象
document.execCommand("Copy"); //執行瀏覽器複製命令
alert("已複製好,可貼粘。");
}
</script>
<textarea cols="20" rows="10" id="biao1">使用者定義的代碼地區</textarea>
<input type="button" onClick="copyUrl2()" value="點擊複製代碼" />
將以上代碼拷貝到DW的代碼區,就可以實現。其中。使用者定義的代碼地區這幾個字你可以改寫為任意內容。