Helius
2022-07-04 c305b09fbd20849a39631ddd43980cb09e1805e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.xcong.farmer.cms.core.template;
 
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
 
public abstract class Configuration {
 
    public String staticPath;
    public String templatePath;
    public String outputPath;
 
    public Configuration() {
    }
 
    public Configuration(String templatePath, String staticPath, String outputPath) {
        this.staticPath = staticPath;
        this.templatePath = templatePath;
        this.outputPath = outputPath;
    }
 
    public static Map<String, String> templateCode = new HashMap<>();
 
    private final String PREFIX_DEFAULT = "{";
    private final String SUFFIX_DEFAULT = "}";
 
    public <T> T parserTag(String attr, Class<T> 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;
    }
}