From 51ea662e22121f9a0ddb5b40c4a8e93e098b34ec Mon Sep 17 00:00:00 2001
From: Hentua <wangdoubleone@gmail.com>
Date: Wed, 10 Jan 2024 22:22:44 +0800
Subject: [PATCH] fxi

---
 src/main/java/com/xcong/farmer/cms/core/node/Template.java |  118 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 101 insertions(+), 17 deletions(-)

diff --git a/src/main/java/com/xcong/farmer/cms/core/node/Template.java b/src/main/java/com/xcong/farmer/cms/core/node/Template.java
index d160592..a2621db 100644
--- a/src/main/java/com/xcong/farmer/cms/core/node/Template.java
+++ b/src/main/java/com/xcong/farmer/cms/core/node/Template.java
@@ -2,33 +2,89 @@
 
 import cn.hutool.core.collection.CollUtil;
 import com.xcong.farmer.cms.core.node.PartNode;
+import lombok.extern.slf4j.Slf4j;
+import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.*;
 
 /**
  * @author wzy
  * @date 2022-06-22
  **/
+@Slf4j
 public class Template {
 
-    private String name;
+    // 文件名称
+    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<>();
 
-    private static boolean hasPaging = false;
+    public volatile static boolean HAS_PAGING = false;
+
+    public Template() {
+    }
+
+    public Template(File file, Map<String, Object> 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.body().children();
+        Elements children = document.children();
         if (CollUtil.isNotEmpty(children)) {
             for (Element child : children) {
                 PartNode partNode = new PartNode(child, this.system);
@@ -37,6 +93,42 @@
                 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) + 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();
+
+            document = null;
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private String path(String path) {
+        if (!path.endsWith("/")) {
+            path = path + "/";
+        }
+        return path;
     }
 
     public String getName() {
@@ -61,14 +153,6 @@
 
     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) {

--
Gitblit v1.9.1