package com.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class ReplaceKeyWordUtil { private static String i18N_ZH_CN = "i18n.rmsMessage_zh_CN";
private static String i18N_ZH_TW = "i18n.rmsMessage_zh_TW";
private static String rootPath = "E:\\ideaSpace\\i18n_for_HK\\rms-web\\irms.web.itms\\src\\main\\java\\com\\boco\\authority\\action";
private static String fileExt = ".java"; /**
* 解析文件内容
* @param file
* @return 一个文件 xxx.java
* @throws Exception
*/
public static String parserSourceFile(File file) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(file));
char[] buffer = new char[(int) file.length()];
br.read(buffer, 0, (int) file.length());
return new String(buffer, 0, buffer.length);
} /**
* 根据模型正则匹配结果
* @param rex [\\u4e00-\\u9fa5]
* @return List
*/
public static String pattMatchAndReplace(String text, String rex,String replaceTarg,File file) throws FileNotFoundException {
Pattern pat = Pattern.compile(rex);
Matcher mat = pat.matcher(text);
if (mat.find()) {
PrintWriter pw = new PrintWriter(file);
pw.print(mat.replaceAll(replaceTarg));
pw.close();
}
return null;
} /**
* 根据目标目录获取文件内容
*
* @param rootPath
* @param result
* @param fileExt
* @return List 文件结果集
*/
public static List<File> getFiles(String rootPath, List<File> result, final String fileExt) {
File f = new File(rootPath);
File[] list = f.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
boolean ret = f.isDirectory() || (f.isFile() && f.getName().endsWith(fileExt));
return ret;
}
});
for (File fn : list) {
if (fn.isDirectory()) {
ReplaceKeyWordUtil.getFiles(fn.getAbsolutePath(), result, fileExt);
} else {
result.add(fn);
}
}
return result;
} /**
* 读取指定properties文件,组装成Map返回
*
* @param proName "i18n.rmsMessage_zh_TW" ,"rmsMessage_zh_CN",
* 这里的配置文件在maven项目工程的resource下或者在其下的某个文件夹里(这里是i18n),
* 传入的参数为文件名去掉.properties后缀。
* @return Map
*/
public static Map<String, String> getReadProperties(String proName) {
Map<String, String> properties = new LinkedHashMap<>();
ResourceBundle rbProResults = ResourceBundle.getBundle(proName);
Enumeration<String> keys = rbProResults.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
properties.put(key, rbProResults.getString(key));
}
return properties;
} //测试替换
public static void main(String[] args) throws Exception {
Map<String, String> properties = ReplaceKeyWordUtil.getReadProperties(i18N_ZH_CN);
handleReplace(properties,fileExt);
System.out.println("ok!");
} /**
* 执行替换
* @param properties 需要替换的关键字map集合
* @param properties 文件后缀 .java
* @param fileExt
* @throws Exception
*/
public static void handleReplace(Map properties,String fileExt) throws Exception {
//搜索目标文件
List<File> result = new LinkedList<File>();
result = ReplaceKeyWordUtil.getFiles(rootPath, result,fileExt);
System.out.println("file count :"+result.size());
//以配置文件的value为关键字,搜索匹配目标文件内容,使用MessageI18nHelper.getMessage(String key)替换该内容。
Iterator iterator = properties.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry it = (Map.Entry) iterator.next();
String key = (String) it.getKey();
String value = "\"" +(String)it.getValue()+"\"";
String replace = "MessageI18nHelper.getMessage(\"" + key + "\")";
//System.out.println(it.getKey() + "," +it.getValue());
//查询替换关键字
for (File file : result) {
System.out.println("fileName :"+file.getName());
String sourceFile = ReplaceKeyWordUtil.parserSourceFile(file);
ReplaceKeyWordUtil.pattMatchAndReplace(sourceFile, value, replace,file);
}
}
} /* *//**
* 根据properties结果map获取value集合
*
* @param propertis
* @return
*//*
public static Set<String> getPropertiesValues(Map<String, String> propertis) {
Set<String> valuesSet = new LinkedHashSet<>();
if (null != propertis) {
Iterator<String> iterator = propertis.keySet().iterator();
while (iterator.hasNext())
valuesSet.add(propertis.get(iterator.next()));
}
return valuesSet;
} *//**
* 根据properties结果map获取key集合
*
* @param propertis
* @return
*//*
public static Set<String> getPropertiesKeys(Map<String, String> propertis) {
Set<String> keysSet = new LinkedHashSet<>();
if (null != propertis) {
Iterator<String> iterator = propertis.keySet().iterator();
while (iterator.hasNext())
keysSet.add(iterator.next());
}
return keysSet;
}*/
}

替换Java WEB工程文件的指定字符串的更多相关文章

  1. 如何在java web工程下建立存储property文件的文件夹,让Java程序直接读取

    如何在java web工程下建立存储property文件的文件夹,让Java程序直接读取: 步骤如下:

  2. Eclipse创建java web工程

    Eclipse创建java web工程 eclipse版本:eclipse-jee-4.5-win32-x64 tomcat版本:apache-tomcat-7.0.63-windows-x64 jd ...

  3. Tomcat设置默认启动项目及Java Web工程设置默认启动页面

    Tomcat设置默认启动项目 Tomcat设置默认启动项目,顾名思义,就是让可以在浏览器的地址栏中输入ip:8080,就能访问到我们的项目.具体操作如下: 1.打开tomcat的安装根目录,找到Tom ...

  4. java web工程发布以及解决tomcat闪退

    1.tomcat闪退 a.环境变量错误 startup.bat最后假如PAUSE进入调试状态,双击startup.bat,可以看到错误,根据错误提示设置相应的环境变量,JAVA_HOME等. b.ec ...

  5. 于Heroku平台部署maven webapp(java web)工程

    眼下,需要Heroku上述部署java web工程,该项目必须使用maven管理 一:新maven webapp工程 编者pom.xml档,增加下面的配置为例, <project xmlns=& ...

  6. eclipse--java工程转web工程 以及 java或java web工程转maven工程

    1.  打开工程文件夹,编辑工程的.project文件. 在<natures></natures>中加入 <nature>org.eclipse.wst.commo ...

  7. 使用Java web工程建立Maven Web Module工程

    1. 前言 之前有一篇关于搭建S2SH的文章中提到建立Maven Web Module工程,有人反馈说这个方面不会.那还是唠叨一下,写篇文章说明一下吧. 建立Maven Web Module的方式有多 ...

  8. 使用 maven 创建 java web 工程

    本文主要讲述使用 maven 命令行的形式来创建  java web 工程 开发环境 jdk 1.7 maven 3.5.0 spring 3.2 tomcat 7 eclipse Mars Rele ...

  9. java web 工程创建及servlet简单使用

    1.java web工程创建 (1)File--->new--->project (2)选择java enterprise,按照下图操作 (3)点击next后,会进入如下界面,修改工程名后 ...

随机推荐

  1. [开发笔记]-unix时间戳、GMT时间与datetime类型时间之前的转换

    前段时间项目中涉及到了MySql和MsSql数据类型之间的转换,最近又在研究新浪微博的API,涉及到了带有时区的GMT时间类型的转换,所以,特记录于此,以备日后查询. 一:UNIX时间戳与dateti ...

  2. HDU 1159.Common Subsequence【动态规划DP】

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  3. 数学--数论--随机算法--Pollard Rho 大数分解算法 (带输出版本)

    RhoPollard Rho是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:MillerRabinMillerRabin素数测试. 操作流程 首先,我们先用MillerRabinMille ...

  4. git新手使用教程包含各种系统

    Git Tutorial 1.下载客户端 从Git官网下载客户端:   https://git-scm.com/   Windows版下载地址:   https://git-scm.com/downl ...

  5. PHP命令执行学习总结

    前言 最近学习了PHP命令执行,内容比较多,把自己学到的总结下来,加深理解,水平有限,欢迎大佬斧正. 什么是PHP命令注入攻击? Command Injection,即命令注入攻击,是指由于Web应用 ...

  6. 【Python】Django2.0集成Celery4.1详解

    环境准备 Python3.6 pip install Django==2.0.1 pip install celery==4.1.0 pip install eventlet (加入协程支持) 安装e ...

  7. Java——IO流超详细总结

    该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 在初学Java时,I ...

  8. Excel 函数 常见错误

    excel公式出错的值. 常见错误值类型: #N/A #VALUE! #NAME? #REF! #NUM! #NULL! #N/A 找不到指定内容,比如查找根本不存在的内容,便显示该错误值 比如在使用 ...

  9. 【HBase】带你了解一哈HBase的各种预分区

    目录 简单了解 概述 设置预分区 一.手动指定预分区 二.使用16进制算法生成预分区 三.将分区规则写在文本文件中 四.使用JavaAPI进行预分区 简单了解 概述 由上图可以看出,每一个表都有属于自 ...

  10. Java UDP小结

    UDP: * 发送端                                                                                           ...