package myReplace;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author jpsoft mengzhijun
*
*/
public class DealWithFilesBase {//便捷式一站处理xml,query.jsp,update.jsp文件的下划线问题,支持处理业务扩展,只需将要处理的文件放在桌面生成的ObjectFile文件夹里,然后在桌面生成的result文件夹里寻找修改后的文件即可
private String deskopPath="^c:\\\\\\\\Users\\\\(\\S){0,}\\\\Desktop$";//桌面路径匹配表达式
private String xmlFIleMatch=".xml$";//匹配.xml文件名
private String queryFileMatch="Query.jsp$";//匹配Query.jsp文件名
private String updateFileMatch="Update.jsp$";//匹配Update.jsp文件名
private String saveFileMatch=".txt$";//匹配.txt文件名
private String resultxmlPath="xml";
private String resultQueryPath="queryJsp";
private String resultUpdatePath="updateJsp";
private String objectFilePath="ObjectFile";
private String resultFilePath="result";
private String writePath=resultFilePath;//默认路径为result
private String cHARY=String.valueOf('"');
private String SOLIDSk=writePath;
/**
* 找到电脑桌面的位置
* @return
*/
public String getDeskopLocation()
{
File file=new File("C:");
@SuppressWarnings("static-access")
File []homePath=file.listRoots();
for(File h:homePath) {
if(h.isDirectory()){return findDeskop(file).getAbsolutePath();}
}
return null;
}
/**
* 搜索计算机桌面的位置
* @param file
* @return
*/
public File findDeskop(File file)
{
File tempFile=file;
if(tempFile.exists()&&tempFile.isDirectory())
{
if(tempFile.getName().equals("Desktop")&&isMatch(deskopPath, tempFile.getAbsolutePath())&&(!tempFile.getPath().equals("C:\\Users\\Default\\Desktop"))&&(!tempFile.getPath().equals("C:\\Users\\Default\\Desktop")))
{return tempFile;}
File []files=tempFile.listFiles();
if(files!=null){
for(File f:files)
{
if(f!=null){
tempFile=findDeskop(f);
if(tempFile.getName().equals("Desktop")&&isMatch(deskopPath, tempFile.getAbsolutePath())&&(!tempFile.getPath().equals("C:\\Users\\Default\\Desktop"))&&(!tempFile.getPath().equals("C:\\Users\\Default\\Desktop"))){break;};
}
}
}
}
return tempFile;
}
/**
* 判断是否匹配,处理文件名
* @param regex
* @param objectString
* @return
*/
public boolean isMatch(String regex,String objectString)
{Pattern pattern=Pattern.compile(regex);
if(regex.equals(deskopPath)){pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);}
Matcher matcher=pattern.matcher(objectString);
return matcher.find();
}
/**
* 处理匹配,并返回处理后的结果
* @param regex
* @param objectString
* @return
*/
public StringBuffer findMatchGroupe(String regex,String objectString,boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find())
{
if(isUpdate){if(isContainfor_(matcher.group())){matcher.appendReplacement(sb,"id="+cHARY+"for_"+subString(matcher.group().substring(8)));}
else{ matcher.appendReplacement(sb,subString(matcher.group()));}
}else{
matcher.appendReplacement(sb,subString(matcher.group()));
}
}
matcher.appendTail(sb);
return sb;
}
/**
* 处理for_,针对update.jsp里的id="for_**
*/
public boolean isContainfor_(String objectString)
{Pattern pattern=Pattern.compile("id=\\Sfor(\\w){0,}");
Matcher matcher=pattern.matcher(objectString);
return matcher.find();
}
/**
* 扫描目标文件夹的文件
*/
public void scanFile(String filePath)
{
int i=0;
File file=new File(filePath);
if(file.isDirectory())
{
File[] files=file.listFiles();
for(File f:files)
{
if(f!=null)
{
System.out.println("正在执行第"+(++i)+"个文件");
setWritePath();
if(isMatch(xmlFIleMatch,f.getName() )){this.writePath=this.writePath+"\\"+resultxmlPath+"\\"+f.getName() ;}
if(isMatch(queryFileMatch, f.getName())){this.writePath=this.writePath+"\\"+resultQueryPath+"\\"+f.getName();}
if(isMatch(updateFileMatch, f.getName())){this.writePath=this.writePath+"\\"+resultUpdatePath+"\\"+f.getName();}
produceResultFiles(readFile(f),this.writePath);
System.out.println(writePath);
}
}
}
}
/**
* 初始化writePath路径
*/
public void setWritePath()
{
this.writePath=this.SOLIDSk;
}
/**
* 文件读取
* @param f
*/
public StringBuffer readFile(File f)
{
StringBuffer tBuffer=new StringBuffer();
InputStream iStream=null;
InputStreamReader bstream=null;
BufferedReader bReader=null;
try {
iStream=new FileInputStream(f);
bstream =new InputStreamReader(iStream);
bReader=new BufferedReader(bstream);
if(isMatch(saveFileMatch, f.getName())){tBuffer=dealWithTXT(bReader);}
if(isMatch(xmlFIleMatch,f.getName() )){ tBuffer=dealWithXml(bReader);}
if(isMatch(queryFileMatch, f.getName())){tBuffer=dealWithQueryJsp(bReader);}
if(isMatch(updateFileMatch, f.getName())){tBuffer=dealWithUpdateJsp(bReader);}
} catch (FileNotFoundException e) {}
finally{
try {bReader.close();
bstream.close();
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
return tBuffer;
}
/**
* 处理带下划线的字符串
* @param temstring
* @return
*/
public String subString(String temstring)
{
String mytemp=null;
for(int i=0;i<temstring.length();i++)
{
if("_".equals(temstring.substring(i, i+1)))
{
mytemp= temstring.replace(temstring.substring(i,i+2), temstring.substring(i+1, i+2).toUpperCase());
mytemp= subString(mytemp);
break;
}
}
return mytemp!=null?mytemp:temstring;
}
/**
* 生成结果文件
* @param writePath
*/
public void produceResultFiles( StringBuffer textContent,String mywritePath)
{
File file=new File(mywritePath);
OutputStream foStream=null;
OutputStreamWriter oWriter=null;
BufferedWriter bWriter=null;
if(!file.exists()){try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}}
try {
foStream=new FileOutputStream(file);
oWriter=new OutputStreamWriter(foStream);
bWriter=new BufferedWriter(oWriter);
bWriter.write(textContent.toString());
bWriter.flush();
} catch (IOException e) {
System.out.println("文件写入失败");
e.printStackTrace();
}finally{
try {
bWriter.close();
oWriter.close();
foStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 处理txt格式文件
* @param bReader
* @return
*/
public StringBuffer dealWithTXT( BufferedReader bReader)
{String tempString="";
StringBuffer sBuffer=new StringBuffer();
try {
while((tempString=bReader.readLine())!=null)
{
sBuffer.append(tempString);
}
} catch (IOException e) {
e.printStackTrace();
}
return sBuffer;
}
/**
* 处理xml文件
* @param bReader
* @return
*/
public StringBuffer dealWithXml(BufferedReader bReader)
{
String tempString="";
StringBuffer sBuffer=new StringBuffer();
try {
while((tempString=bReader.readLine())!=null)
{
tempString=findMatchGroupe(":(\\w){0,}", tempString,false).toString();
sBuffer.append(tempString).append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sBuffer;
}
/**
* 处理queryJsp文件
* @param bReader
* @return
*/
public StringBuffer dealWithQueryJsp(BufferedReader bReader)
{
String tempString="";
StringBuffer sBuffer=new StringBuffer();
try {
while((tempString=bReader.readLine())!=null)
{
tempString=findMatchGroupe("(\\{(\\w){0,}\\S{0,}\\})|\\{(\\w){0,}\\S{0,}", tempString,false).toString();
sBuffer.append(tempString).append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sBuffer;
}
/**
* 处理updateJsp
* @param bReader
* @return
*/
public StringBuffer dealWithUpdateJsp(BufferedReader bReader)
{
String tempString="";
StringBuffer sBuffer=new StringBuffer();
try {
while((tempString=bReader.readLine())!=null)
{
tempString=findMatchGroupe("name=\\S(\\w){0,}|id=\\S(\\w){0,}", tempString,true).toString();
sBuffer.append(tempString).append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sBuffer;
}
/**
* 执行
*/
public void execute()
{ String deskopPath="";
File file=new File("com.jpsoft.savePath.txt");
if(!file.exists()){
try {
System.out.println("第一次运行,可能需要几分钟,请耐心等待.....");
file.createNewFile();
String path=getDeskopLocation();
produceResultFiles((new StringBuffer()).append(path),file.getName() );
deskopPath= readFile(file).toString();
} catch (IOException e) {
System.out.println("文件创建失败");
}}
deskopPath= readFile(file).toString();
File objectFileContents=new File(deskopPath+"\\"+objectFilePath);
if(!objectFileContents.exists()){objectFileContents.mkdir();System.out.println("目标文件路径创建成功"+" 路径:"+objectFileContents.getPath());}
File resultFileContents=new File(deskopPath+"\\"+resultFilePath);
if(!resultFileContents.exists()){resultFileContents.mkdir();System.out.println("结果文件夹路径创建成功"+" 路径:"+resultFileContents.getPath());}
File xmlFileContent=new File(deskopPath+"\\"+resultFilePath+"\\"+resultxmlPath);
if(!xmlFileContent.exists()){xmlFileContent.mkdir();System.out.println("xml文件夹路径创建成功"+" 路径:"+xmlFileContent.getPath());}
File queryFileContent=new File(deskopPath+"\\"+resultFilePath+"\\"+resultQueryPath);
if(!queryFileContent.exists()){queryFileContent.mkdir();System.out.println("queryjsp文件夹路径创建成功"+" 路径:"+queryFileContent.getPath());}
File updateFileContent=new File(deskopPath+"\\"+resultFilePath+"\\"+resultUpdatePath);
if(!updateFileContent.exists()){updateFileContent.mkdir();System.out.println("updateJsp文件夹路径创建成功"+" 路径:"+updateFileContent.getPath());}
deskopPath= readFile(file).toString();
this.writePath=deskopPath+"\\"+resultFilePath;
this.SOLIDSk=this.writePath;
scanFile(deskopPath+"/"+objectFilePath);
System.out.println("运行成功");
}
/**
* @param args
*/
public static void main(String[] args) {
DealWithFilesBase dFiles=new DealWithFilesBase();
dFiles.execute();
}
}
呵呵,可以处理页面的软元素的下划线问题