Java工具类(21)—Freemarker工具类
package com.awifi.capacity.docker.manager.utils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.*;
import static com.awifi.capacity.docker.manager.utils.FileUtil.getFileContent;
@Data
public class FreemarkerUtil {
private static String defaultCharacter = "UTF-8";
private static Configuration cfg;
static {
cfg = new Configuration();
cfg.setDefaultEncoding(defaultCharacter);
cfg.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
}
public static String processTemplate(String myTemplate, Map<String, Object> map){
String result = null;
String name = "template";
try {
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate(name, myTemplate);
cfg.setTemplateLoader(stringTemplateLoader);
Template template = cfg.getTemplate(name, defaultCharacter);
StringWriter out = new StringWriter();
template.process(map, out);
out.flush();
result = out.toString();
out.close();
} catch (Exception e){
e.printStackTrace();
}
return result;
}
public static String convertListToString(List<String> strlist){
StringBuffer sb = new StringBuffer();
String result=null;
if(CollectionUtils.isNotEmpty(strlist)){
for (int i=0;i<strlist.size();i++) {
if(i==0){
sb.append("'").append(strlist.get(i)).append("'");
}else{
sb.append(",").append("'").append(strlist.get(i)).append("'");
}
}
result="["+sb.toString()+"]";
}
return result;
}
public static void createFile(String pathOld, String str) {
File file = new File(pathOld);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream output = new FileOutputStream(file);
byte[] bytes = str.getBytes();
output.write(bytes);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String path = "D:\\project\\project-manager\\rdp-pm-1.0.6\\rdp-project-manager-products\\rdp-pm-products-provider\\src\\main\\resources\\jenkinsdmavenSetting.ftl";
String pathold = "D:/setting.xml";
String pathnew = "/opt/maven/apache-maven-3.5.4";
String creds = String.format("%s:%s", pathold, pathnew);
System.out.println(creds);
ArrayList<Object> list = new ArrayList<>();
String fileContent= getFileContent(path);
Map<String,Object> map= new HashMap<String, Object>();
map.put("repositoryId", "aWifi-rep-test1");
map.put("username", "zhangxin");
map.put("password", "123456");
Map<String,Object> map1= new HashMap<String, Object>();
map1.put("repositoryId", "aWifi-rep-test2");
map1.put("username", "zhangxin");
map1.put("password", "123456");
list.add(map);
list.add(map1);
HashMap<String, Object> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("list",list);
System.out.println("源文件------"+fileContent);
String result = processTemplate(fileContent, objectObjectHashMap);
createFile(pathold,result);
System.out.println("result----"+result);
}
}