Helius
2022-07-07 95ef76d6440a5363c0b7981b1333f82f9345322d
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.xcong.farmer.cms.core.node;
 
import cn.hutool.core.collection.CollUtil;
import com.xcong.farmer.cms.core.node.PartNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
 
/**
 * @author wzy
 * @date 2022-06-22
 **/
public class Template {
 
    // 文件名称
    private String name = "index";
    // 文件保存路径
    private String path = "";
 
    private Document document;
    private Map<String, Map<String, Object>> params = new HashMap<>();
    private Map<String, Object> system;
    private List<PartNode> partNodes = new ArrayList<>();
 
 
    // 页面中包含的标签
    public static Set<String> TAGS;
    public static boolean HAS_PAGING = false;
 
    public Template() {
        TAGS = new HashSet<>();
    }
 
    public Template(File file, Map<String, Object> system) {
        TAGS = new HashSet<>();
 
        Document document = null;
        try {
            document = Jsoup.parse(file, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        if (document == null) {
            throw new NullPointerException();
        }
 
        this.document = document;
        this.system = system;
 
        Object templatePath = system.get("templatePath");
        if (templatePath != null) {
            this.path = (String) templatePath;
        }
 
        Object templateType = system.get("templateType");
        if ("article".equals(templateType)) {
            Object templateName = system.get("templateName");
            if (templateName != null) {
                this.name = (String) templateName;
            }
        } else if ("column".equals(templateType)) {
            Object page = system.get("page");
            if (!new Integer(1).equals(page)) {
                this.name = name + "_" + page;
            }
        } else {
 
        }
 
    }
 
    public void parser() {
        Elements children = document.body().children();
        if (CollUtil.isNotEmpty(children)) {
            for (Element child : children) {
                PartNode partNode = new PartNode(child, this.system);
                partNode.parser();
 
                this.add(partNode);
            }
        }
    }
 
    public void output(String outputPath) {
        String suffix = ".html";
        Document document = this.document;
        List<PartNode> partNodes = this.partNodes;
        StringBuilder sb = new StringBuilder();
        for (PartNode partNode : partNodes) {
            sb.append(partNode.getHtml());
        }
        document.body().empty().html(sb.toString());
        String outPath = path(outputPath);
 
        String html = document.html();
        try {
            String path = outPath + path(this.path);
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
 
            FileOutputStream outputStream = new FileOutputStream(path +this.name + suffix);
            outputStream.write(html.getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    private String path(String path) {
        if (!path.endsWith("/")) {
            path = path + "/";
        }
        return path;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Document getDocument() {
        return document;
    }
 
    public void setDocument(Document document) {
        this.document = document;
    }
 
    public void add(PartNode partNode) {
        this.partNodes.add(partNode);
    }
 
    public List<PartNode> getPartNodes() {
        return partNodes;
    }
 
    public Map<String, Map<String, Object>> getParams() {
        return params;
    }
 
    public void putParams(String key, Map<String, Object> value) {
        this.params.put(key, value);
    }
 
    public void systemData(Map<String, Object> data) {
        this.system = data;
    }
}