public class FtlRegexpUtil {
public static void main(String[] args) throws Exception {
String ftlPath = "E:\\workspace\\svn\\scsk_pj\\SccmPlatform\\04Code\\crm\\trunk\\plugins\\crm\\template\\client\\attributiveClassification";
String xmlPath = "E:\\workspace\\svn\\scsk_pj\\SccmPlatform\\04Code\\crm\\trunk\\plugins\\crm\\config\\CrmUiLabels-client.xml";
fileDir(ftlPath, xmlPath);
}
/**
* 扫描ftl文件目录
*
* @param ftlPath ftl文件目录
*/
public static void fileDir(String ftlPath, String xmlPath) {
File file = new File(ftlPath);
if (file.isDirectory()) {
System.out.println("正在读取" + ftlPath + "目录....");
String[] list = file.list();
for (int i = 0; i < list.length; i++) {
File file2 = new File(ftlPath + "\\" + list[i]);
if (file2.isDirectory()) {
System.out.println("文件夹:" + ftlPath + "\\" + list[i]);
String sonDirName = ftlPath + "\\" + list[i];
fileDir(sonDirName, xmlPath);
} else {
String pathName = ftlPath + "\\" + list[i];
updateFTL(pathName, xmlPath);
System.out.println("文件:" + ftlPath + "\\" + list[i] + "修改完成");
}
}
} else {
System.out.println(ftlPath + "不是一个目录。");
}
}
/**
* 修改ftl文件
*
* @param pathName ftl文件地址
*/
public static void updateFTL(String pathName, String xmlPath) {
try {
//ftl文件路径
File fileInput = new File(pathName);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileInput), "UTF-8"));
StringBuffer str = new StringBuffer();
while (in.ready()) {
str.append(in.readLine() + "\n");
}
//关闭流
in.close();
//xml文件路径
// String url = "E:\\workspace\\svn\\scsk_pj\\SccmPlatform\\04Code\\crm\\trunk\\plugins\\crm\\config\\CrmUiLabels-client.xml";
List<Map<String, String>> xmlList = readXml(xmlPath);
String content = str.toString();
//定义正则表达式
String pattern = "[\\u4e00-\\u9fa5]+";
//String pattern = "<label.*>(.*)</label>";
//编译正则表达式
Pattern patten = Pattern.compile(pattern);
// 指定要匹配的字符串
Matcher matcher = patten.matcher(content);
//此处find()每次被调用后,会偏移到下一个匹配
while (matcher.find()) {
//获取当前匹配的值 matcher.group()
for (Map<String, String> xmlMap : xmlList) {
for (Map.Entry<String, String> entry : xmlMap.entrySet()) {
if (matcher.group().equals(entry.getValue())) {
String replacement = "${uiLabelMap." + entry.getKey() + "}";
content = content.replace(matcher.group(), replacement);
} else {
break;
}
}
}
}
//将文件写入
FileWriter fw = new FileWriter(fileInput);
fw.write(content);
//关闭流
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取xml文件
*
* @param url xml文件地址
* @return list
*/
public static List<Map<String, String>> readXml(String url) {
List<Map<String, String>> xmlList = new ArrayList<>();
//创建DocumentBuilderFactory对象
DocumentBuilderFactory a = DocumentBuilderFactory.newInstance();
try {
//创建DocumentBuilder对象
DocumentBuilder b = a.newDocumentBuilder();
//通过DocumentBuilder对象的parse方法返回一个Document对象
Document document = b.parse(url);
//Document document = b.parse("E:\\workspace\\svn\\scsk_pj\\SccmPlatform\\04Code\\crm\\trunk\\plugins\\crm\\config\\CrmUiLabels-client.xml");
//通过Document对象的getElementsByTagName()返根节点的一个list集合
NodeList propertyList = document.getElementsByTagName("property");
for (int i = 0; i < propertyList.getLength(); i++) {
Map<String, String> map = new LinkedHashMap<String, String>();
//循环遍历获取每一个property
Node property = propertyList.item(i);
//通过Node对象的getAttributes()方法获取全的属性值
NamedNodeMap propertyMap = property.getAttributes();
//循环遍每一个property的属性值
for (int j = 0; j < propertyMap.getLength(); j++) {
Node node = propertyMap.item(j);
//通过Node对象的getNodeName()和getNodeValue()方法获取属性名和属性值
//map.put(node.getNodeName(), node.getNodeValue());
//解析property节点的子节点
NodeList childList = property.getChildNodes();
for (int t = 0; t < childList.getLength(); t++) {
//区分出text类型的node以及element类型的node
if (childList.item(t).getNodeType() == Node.ELEMENT_NODE) {
map.put(node.getNodeValue(), childList.item(t).getTextContent());
}
}
}
//解析property节点的子节点
// NodeList childList = property.getChildNodes();
// for (int t = 0; t < childList.getLength(); t++) {
// //区分出text类型的node以及element类型的node
// if (childList.item(t).getNodeType() == Node.ELEMENT_NODE) {
// map.put(childList.item(t).getNodeName(), childList.item(t).getTextContent());
// //System.out.print(childList.item(t).getNodeName()+":");
// //System.out.println(childList.item(t).getTextContent());
// }
// }
xmlList.add(map);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xmlList;
}
}
Ftl修改替换
最新推荐文章于 2024-07-19 19:03:03 发布