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 system; private List partNodes = new ArrayList<>(); public volatile static boolean HAS_PAGING = false; public Template() { } public Template(File file, Map system) { 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.valueOf(templateName); } } else if ("column".equals(templateType)) { Object page = system.get("page"); if (!new Integer(1).equals(page)) { this.name = name + "_" + page; } } else if ("search".equals(templateType)){ Object templateName = system.get("templateName"); if (templateName != null) { this.name = String.valueOf(templateName); } else { this.name = "search"; } } else if ("message".equals(templateType)) { Object templateName = system.get("templateName"); if (templateName != null) { this.name = String.valueOf(templateName); } else { this.name = "message"; } } } public void parser() { Elements children = document.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 partNodes = this.partNodes; StringBuilder sb = new StringBuilder(); for (PartNode partNode : partNodes) { sb.append(partNode.getHtml()); } document = Jsoup.parse(sb.toString()); String outPath = path(outputPath) + system.get("companyCode"); 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 getPartNodes() { return partNodes; } public void systemData(Map data) { this.system = data; } }