铭飞MCMS将4.6模板标签升级至4.7


个人博客 地址:https://www.wenhaofan.com/article/20190610145529

介绍

MCMS提供的模板大多数都使用的是4.6版本的标签,但是现在MCMS最新的已经是4.7了,然而4.7并不能向下兼容4.6,所以在4.7的MCMS中使用4.6的模板需要处理其中的旧版本标签,为了方便以及复用,想到了使用正则表达式解析、替换标签将4.6模板一键升级至4.7,代码如下

代码

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;
}
}

下载

http://qiniu.wenhaofan.com/UpgradeTag.java

原文地址:https://www.cnblogs.com/fanwenhao/p/10997658.html