package com.xcong.farmer.cms.core.template; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public abstract class Configuration { protected static String API_URL; protected static String STATIC_URL; protected static String staticPath; protected static String templatePath; protected static String outputPath; public Configuration() { } public Configuration(String templatePath, String staticPath, String outputPath, String apiUrl, String staticUrl) { Configuration.staticPath = staticPath; Configuration.templatePath = templatePath; Configuration.outputPath = outputPath; Configuration.API_URL = apiUrl; Configuration.STATIC_URL = staticUrl; } public static Map templateCode = new HashMap<>(); private final String PREFIX_DEFAULT = "{"; private final String SUFFIX_DEFAULT = "}"; public T parserTag(String attr, Class clazz) { if (!attr.startsWith(PREFIX_DEFAULT) || !attr.endsWith(SUFFIX_DEFAULT)) { return null; } int i = 1; int length = attr.length(); int startIndex = 1; int endIndex = 1; T object = null; try { object = clazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } boolean hasMulti = false; for (; i <= length - 1; i++) { if (attr.charAt(i) =='[') { hasMulti = true; } else if (attr.charAt(i) == ']') { hasMulti = false; } if (!hasMulti) { if (attr.charAt(i) == ',' || i == length - 1) { String columnStr = attr.substring(startIndex, endIndex); String[] columns = columnStr.split("="); if (columns.length != 2) { return null; } try { Field field = clazz.getDeclaredField(columns[0].trim()); field.setAccessible(true); field.set(object, columns[1].replace("[", "").replace("]", "").trim()); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); return null; } startIndex = i + 1; } } endIndex++; } return object; } public void put(String key, String templateCode) { Configuration.templateCode.put(key, templateCode); } public String get(String key) { return Configuration.templateCode.get(key); } public String getStaticPath() { return staticPath; } public void setStaticPath(String staticPath) { this.staticPath = staticPath; } public String getTemplatePath() { return templatePath; } public void setTemplatePath(String templatePath) { this.templatePath = templatePath; } public String getOutputPath() { return outputPath; } public void setOutputPath(String outputPath) { this.outputPath = outputPath; } }