fxi
Hentua
2024-01-10 51ea662e22121f9a0ddb5b40c4a8e93e098b34ec
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
package com.xcong.farmer.cms.core.template;
 
import com.xcong.farmer.cms.common.exception.GlobalException;
import com.xcong.farmer.cms.common.utils.FileUtils;
import com.xcong.farmer.cms.core.node.PartNode;
import com.xcong.farmer.cms.core.node.Template;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
 
/**
 * @author wzy
 * @date 2022-07-01
 **/
@Slf4j
public class TemplateConfiguration extends Configuration {
 
    private TemplateLoader templateLoader;
 
    public TemplateConfiguration(String templatePath, String staticPath, String outputPath, String baseUrl, String staticUrl) {
        super(templatePath, staticPath, outputPath, baseUrl, staticUrl);
    }
 
    public void templateLoader(TemplateLoader templateLoader) {
        this.templateLoader = templateLoader;
    }
 
    public void process(Map<String, Object> map, String templateName) {
        if (this.templateLoader == null) {
            throw new RuntimeException("TemplateLoader do not able to be null");
        }
        map.put("apiUrl", API_URL);
 
        try {
            String companyCode = (String) map.get("companyCode");
 
            log.info("解析开始执行--#类型:{}#--#模板名称:{}#--#ID:{}#", map.get("templateType"), templateName, map.get("id"));
            this.templateLoader.data(map);
            Template template = template(FileUtils.path(templatePath, companyCode), templateName);
            template.output(outputPath);
 
            // 判断是否有分页,有则执行。从第二页开始
            int i = 2;
            while (Template.HAS_PAGING) {
                map.put("page", i);
                this.templateLoader.data(map);
                Template pageTemplate = template(FileUtils.path(templatePath, companyCode), templateName);
                pageTemplate.output(outputPath);
                i++;
            }
        } catch (Exception e) {
            log.error("页面编译异常", e);
        }
    }
 
    public Template template(String templatePath, String templateName) {
        File file = new File(path(templatePath) + templateName);
        if (!file.exists()) {
            log.info("模板文件不存在 : {}", templateName);
            throw new GlobalException("模板文件不存在:" + templateName);
        }
 
        return template(file);
    }
 
    public Template template(File file) {
        if (file == null) {
            throw new RuntimeException("template not exist");
        }
 
        return this.templateLoader.template(file);
    }
 
    private String path(String path) {
        if (!path.endsWith("/")) {
            path = path + "/";
        }
        return path;
    }
}