MCMS提供的模板大多數都使用的是4.6版本的標籤,可是如今MCMS最新的已是4.7了,然而4.7並不能向下兼容4.6,因此在4.7的MCMS中使用4.6的模板須要處理其中的舊版本標籤,爲了方便以及複用,想到了使用正則表達式解析、替換標籤將4.6模板一鍵升級至4.7,代碼以下java
package live.autu.tools.mcms;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.ReUtil;
public class UpgradeTag {
private static Map<String,String> tagMatch=new LinkedHashMap<String, String>(){{
put("\\{ms:globalskin.url/\\}", "{ms:global.host/}/{ms:global.style/}");
put("$\\{global.host!''\\}", "{ms:global.host/}");
put("$\\{global.style!''\\}", "{ms:global.style/}");
put("\\[field.date fmt=yyyy\\-MM\\-dd/\\]","{ms:field.date?string(\"yyyy-MM-dd\")}");
put("\\{ms\\:field\\.date fmt\\=yyyy\\-MM\\-dd/\\}", "{ms:field.date?string(\"yyyy-MM-dd\")}");
put("\\[field.date fmt\\=yyyy\\-MM\\-dd hh:mm:ss/\\]}", "{ms:field.date?string(\"yyyy-MM-dd hh:mm:ss\")}");
put("\\{ms\\:field\\.date fmt\\=yyyy\\-MM\\-dd hh:mm:ss/\\}","{ms:field.date?string(\"yyyy-MM-dd\")/}");
put("\\{ms\\:field.date\\?string\\(\"yyyy\\-MM\\-dd\"\\)\\}", "{ms:field.date?string(\"yyyy-MM-dd\")/}");
put("\\[field.link/\\]", "{ms:global.url/}[field.link/]");
put("\\[field.typelink/\\]", "{ms:global.url/}[field.typelink/]");
put("\\{ms\\:global.url/\\}\\{ms\\:global.url/\\}", "{ms:global.url/}");
}};
public static void main(String[] args) {
String projectPath="D:\\work\\eclipse\\order\\mcms\\src\\main\\webapp\\templets\\1\\";
//須要處理的路徑
List<String> templets=new ArrayList<String>() {{
add("company1811");
add("company1811\\m");
}};
for (String templet : templets) {
renderTheme(projectPath.concat(templet));
}
}
private static void renderTheme(String filePath) {
File file=new File(filePath);
File[] files=file.listFiles();
for (File file2 : files) {
if(!file2.isFile()) {
continue;
}
String absolutePath=file2.getAbsolutePath();
Upgrade(absolutePath);
}
}
private static void Upgrade(String filePath) {
FileReader fileReader = new FileReader(filePath);
String content = fileReader.readString();
content = UpgradeInclude(content);
for (String key : tagMatch.keySet()) {
content=content.replaceAll(key,tagMatch.get(key));
}
FileWriter writer = new FileWriter(filePath);
writer.write(content);
System.out.println(filePath+":success");
}
private static String UpgradeInclude(String content) {
String includeTagRegex="\\{ms:include[\\s]*filename=+[\\S]*.htm/\\}";
String includeFileNameRegex="=[\\S]*/";
Map<String,String> result=new HashMap<>();
List<String> resultFindAll = ReUtil.findAll(includeTagRegex, content, 0, new ArrayList<String>());
resultFindAll.forEach( tag ->{
String fileName=ReUtil.get(includeFileNameRegex, tag,0);
fileName=fileName.replaceFirst("=", "").replace("/", "");
result.put(tag, "<#include \""+fileName+"\">");
});
for (String key : result.keySet()) {
String finalKey =key.replaceAll("\\{","\\\\{");
finalKey=finalKey.replaceAll("\\}","\\\\}");
content=content.replaceAll(finalKey, result.get(key));
}
return content;
}
}