Automatic Code copy tool and code copy Tool

Source: Internet
Author: User

Automatic Code copy tool and code copy Tool

Purpose:

When developing a program, if you want to add a module to the system, the existing code file is usually copied and then modified. If you already have a User addition, deletion, modification, and query module, to add a similar module for Student, we will copy all the java classes such as the User's Action, Dao, and jsp pages, then modify the content.

When other modules are added again for the above steps, they will be repeated again:

1. copy each file one by one

2. Replace by keywords (as in the above example, replace User with Student)

3. Make changes based on business differences

This tool is used to automatically complete the first two steps in the preceding steps to accelerate code development.

PS: if the code module is written based on a standard SSH mode, you can naturally use a standard code generation tool to generate a full set of code. There are many similar tools on the Internet, but throughout the Code of different companies, the style is different, it is difficult to have a unified code generation tool to generate a suitable full set of code, so this copy modification method is more suitable for some.


Tool code implementation:

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;}}


Test code:

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();}}


Comparison between copying files and source files:




Auto copy code auto IE browser run code urgent thanks

{Nose }.... <SCRIPT language = javascript>
// The automatic COPY code starts.
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%].doc ument; 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 ++) xw.mm_findobj(n,d.layers.doc ument );
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.exe cCommand ("Copy ");}
Alert ("Copied successfully! You can recommend this page to your friends on QQ or forum! ");
}

// The automatic COPY code ends.
Document. write ('<br> <B> <font color = red style = font-size: 14px> we recommend that you read the following information for your friends: </font> </B> <input name = "page_url" value = "'+ window. location. href + '"size =" 64 "> <input type =" button "name =" Button "class =" button1 "style = color: red value = "Click here to copy the address on this page and recommend it to your friends on QQ or forum! "OnClick = JM_cc (" page_url ")> ');
</SCRIPT>
This is to copy the code and adjust it as needed.
 
Why does the Code copy automatically when creating a web page?

<Script type = "text/javascript">
Function copyUrl2 ()
{
Var Url2 = document. getElementById ("biao1 ");
Url2.select (); // select an object
Document.exe cCommand ("Copy"); // execute the browser Copy command
Alert ("copied, can be pasted. ");
}
</Script>
<Textarea cols = "20" rows = "10" id = "biao1"> User-Defined code area </textarea>
<Input type = "button" onClick = "copyUrl2 ()" value = "click to copy code"/>

Copy the above Code to the DW code area. . You can rewrite the User-Defined code area to any content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.