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.valueOf(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.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 = Jsoup.parse(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;
|
}
|
}
|