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. 搭建vsftpd文件服务器并创建虚拟用户

    一.安装     1. 查看是否安装vsftpd         rpm -qa | grep vsftpd     2. 安装          yum -y install vsftpd      ...

  2. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  3. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) C. Restoring

    C. Restoring Permutation time limit per test1 second memory limit per test256 megabytes inputstandar ...

  4. [计算机视觉]从零开始构建一个微软how-old.net服务/面部属性识别

    大概两三年前微软发布了一个基于Cognitive Service API的how-old.net网站,用户可以上传一张包含人脸的照片,后台通过调用深度学习算法可以预测照片中的人脸.年龄以及性别,然后将 ...

  5. E. Count The Blocks

    E. Count The Blocks 这是一个计数题,又把我卡自闭了...之前也碰到过类似的题目,这次居然还没有写出来,感觉自己还是太菜了,加油补题吧. 题目大意: 给你一个数字 \(n\),代表的 ...

  6. VA01信贷使用

    1业务场景 业务在正式机中发现,当使用VA01输入客户编号回车后会报错 2解决方法 1. SE24进入CL_IM_UKM_SD_FSCM_INTEGR1 2. 双击方法IF_EX_BADI_SD_CM ...

  7. ABAP 内表与XML转换

    1需求说明 在系统交互中需要将SAP内表转换为XML文件,发送给其他系统,并且将其他系统返回的XML文件转换为SAP内表. 2创建转换编辑器 事务代码:STRANS 选择简单转换 以图形方式编辑 右键 ...

  8. STM32 TIM1高级定时器配置快速入门

    layout: post tags: [STM32] comments: true 文章目录 layout: post tags: [STM32] comments: true 重点内容 时基单元 计 ...

  9. 一看就懂的Ubuntu系统下samba服务器安装配置教程

    文章目录 前言 环境搭建 安装 配置 Examples 1 创建共享(任何人都可以访问) 2 单用户权限(需要密码访问) 添加samba用户 配置参数 3 支持游客访问(单用户拥有管理员权限) 前言 ...

  10. vue.js 实现点击展开收起动画

    最近公司项目加了个页面,其中要求是这样的,点击对应列表,展开和收起, 其实就是显示和隐藏内容部分:说来惭愧,我花了半天时间才搞出来(自黑一下~), ,,接下来分享给大家,先上效果图: .vue页面: ...