Helius
2022-07-05 853b3da7f705938071156fadcddb668b7546e719
fix 完成文件输出
10 files modified
209 ■■■■■ changed files
src/main/java/com/xcong/farmer/cms/core/handler/ArticlesDataParserHandler.java 5 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/handler/ColumnDataParserHandler.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/handler/NavDataParserHandler.java 12 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java 23 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/node/Template.java 97 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/template/TemplateConfiguration.java 33 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/template/TemplateLoader.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/core/service/ICmsCoreService.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/core/service/impl/CmsCoreServiceImpl.java 7 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/ReleaseServiceImpl.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/handler/ArticlesDataParserHandler.java
@@ -4,6 +4,7 @@
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.farmer.cms.configurations.properties.CmsProperties;
import com.xcong.farmer.cms.conversion.ArticleConversion;
import com.xcong.farmer.cms.core.node.AttrNode;
import com.xcong.farmer.cms.core.tag.data.ArticleData;
@@ -26,8 +27,8 @@
public class ArticlesDataParserHandler implements DataParserHandler  {
    private ArticleMapper articleMapper = SpringContextHolder.getBean(ArticleMapper.class);
    private CmsProperties cmsProperties = SpringContextHolder.getBean(CmsProperties.class);
    private String BASE_URL = "http://192.168.0.1";
    @Override
    public void dataParser(AttrNode node) {
        log.info("文章列表解析");
@@ -60,7 +61,7 @@
        if (article.getType() == 2) {
            articleData.setUrl(article.getArticleUrl());
        } else {
            articleData.setUrl(BASE_URL + article.getPath() + "/" + article.getId() + ".html");
            articleData.setUrl(cmsProperties.getBaseUrl() + article.getPath() + "/" + article.getId() + ".html");
        }
src/main/java/com/xcong/farmer/cms/core/handler/ColumnDataParserHandler.java
@@ -1,6 +1,7 @@
package com.xcong.farmer.cms.core.handler;
import cn.hutool.core.collection.CollUtil;
import com.xcong.farmer.cms.configurations.properties.CmsProperties;
import com.xcong.farmer.cms.conversion.ColumnConversion;
import com.xcong.farmer.cms.core.node.AttrNode;
import com.xcong.farmer.cms.core.tag.data.ColumnData;
@@ -25,7 +26,7 @@
    private ColumnMapper columnMapper = SpringContextHolder.getBean(ColumnMapper.class);
    private ArticleMapper articleMapper = SpringContextHolder.getBean(ArticleMapper.class);
    private String BASE_URL = "http://192.168.0.1";
    private CmsProperties cmsProperties = SpringContextHolder.getBean(CmsProperties.class);
    @Override
    public void dataParser(AttrNode attrNode) {
@@ -55,14 +56,14 @@
    public ColumnData columnToData(ColumnEntity column) {
        ColumnData columnData = ColumnConversion.INSTANCE.entityToData(column);
        columnData.setUrl(BASE_URL + column.getPath());
        columnData.setUrl(cmsProperties.getBaseUrl() + column.getPath());
        if (column.getType() == 2) {
            if (column.getTargetType() == 1) {
                ArticleEntity article = this.articleMapper.selectArticleById(Long.parseLong(column.getTargetUrl()));
                columnData.setUrl(BASE_URL + article.getPath() + "/" + article.getId() + ".html");
                columnData.setUrl(cmsProperties.getBaseUrl() + article.getPath() + "/" + article.getId() + ".html");
            } else if (column.getTargetType() == 2) {
                ColumnEntity columnEntity = this.columnMapper.selectByCodeAndCompanyId(column.getTargetUrl(), column.getCompanyId());
                columnData.setUrl(BASE_URL + columnEntity.getPath());
                columnData.setUrl(cmsProperties.getBaseUrl() + columnEntity.getPath());
            } else {
                columnData.setUrl(column.getTargetUrl());
            }
src/main/java/com/xcong/farmer/cms/core/handler/NavDataParserHandler.java
@@ -1,6 +1,7 @@
package com.xcong.farmer.cms.core.handler;
import cn.hutool.core.collection.CollUtil;
import com.xcong.farmer.cms.configurations.properties.CmsProperties;
import com.xcong.farmer.cms.conversion.ColumnConversion;
import com.xcong.farmer.cms.core.node.AttrNode;
import com.xcong.farmer.cms.core.tag.data.NavData;
@@ -27,7 +28,8 @@
    private ColumnMapper columnMapper = SpringContextHolder.getBean(ColumnMapper.class);
    private ArticleMapper articleMapper = SpringContextHolder.getBean(ArticleMapper.class);
    private String BASE_URL = "http://192.168.0.1";
    private CmsProperties cmsProperties = SpringContextHolder.getBean(CmsProperties.class);
    @Override
    public void dataParser(AttrNode node) {
        log.info("导航栏解析");
@@ -43,7 +45,7 @@
        NavData index = new NavData();
        index.setTitle("首页");
        index.setUrl(BASE_URL);
        index.setUrl(cmsProperties.getBaseUrl());
        index.setCode("index");
        list.add(index);
@@ -73,14 +75,14 @@
    public NavData columnToNav(ColumnEntity column) {
        NavData navData = ColumnConversion.INSTANCE.columnToNav(column);
        navData.setUrl(BASE_URL + column.getPath());
        navData.setUrl(cmsProperties.getBaseUrl() + column.getPath());
        if (column.getType() == 2) {
            if (column.getTargetType() == 1) {
                ArticleEntity article = this.articleMapper.selectArticleById(Long.parseLong(column.getTargetUrl()));
                navData.setUrl(BASE_URL + article.getPath() + "/" + article.getId() + ".html");
                navData.setUrl(cmsProperties.getBaseUrl() + article.getPath() + "/" + article.getId() + ".html");
            } else if (column.getTargetType() == 2) {
                ColumnEntity columnEntity = this.columnMapper.selectByCodeAndCompanyId(column.getTargetUrl(), column.getCompanyId());
                navData.setUrl(BASE_URL + columnEntity.getPath());
                navData.setUrl(cmsProperties.getBaseUrl() + columnEntity.getPath());
            } else {
                navData.setUrl(column.getTargetUrl());
            }
src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java
@@ -52,22 +52,6 @@
        this.parserData = parserData;
    }
    public static void main(String[] args) {
//        String data = "{id=[1,2,3], page=1, limit=5, field=art}";
//        Articles articles = new AttrNode().parserTag(data, Articles.class);
//        String value = "{id=${col.id}, page=1, limit=5, field=art}";
//        String pattern = "(?<=\\$\\{)[\\s\\S]*?(?=\\})";
//        Matcher matcher = Pattern.compile(pattern).matcher(value);
//        while (matcher.find()) {
//            String group = matcher.group();
//            System.out.println(1);
//        }
        System.out.println(1);
    }
    private boolean isNeedEmpty() {
        Elements children = this.element.children();
        if (CollUtil.isNotEmpty(children)) {
@@ -112,6 +96,7 @@
            i++;
            try {
                Template.TAGS.add(tagsEnum.getName());
                // {id=${col.id}, page=1, limit=5, field=art} ${col.id} 形式需先设置值
                String tagValue = attributes.get(tagsEnum.getName());
                tagValue = attrValueFormat(tagValue);
@@ -139,6 +124,10 @@
    public void runDataInject() {
        Attributes attributes = this.element.attributes();
        for (Attribute attribute : attributes) {
            if (attribute.getKey().startsWith("\\$")) {
                Template.TAGS.add(attribute.getKey());
            }
            String key = attribute.getKey().replaceAll("\\$", "");
            String value = attribute.getValue();
@@ -158,9 +147,7 @@
                this.element.removeAttr("class");
                this.element.attr("class", evaluate);
            } else if (value.contains( "${")) {
                String result = attrValueFormat(value);
                System.out.println(result);
                if ("text".equals(key)) {
                    this.element.text(result);
                } else {
src/main/java/com/xcong/farmer/cms/core/node/Template.java
@@ -2,14 +2,15 @@
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.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
@@ -17,15 +18,63 @@
 **/
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 HAS_PAGING = false;
    // 页面中包含的标签
    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();
@@ -39,6 +88,40 @@
        }
    }
    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;
    }
src/main/java/com/xcong/farmer/cms/core/template/TemplateConfiguration.java
@@ -32,9 +32,9 @@
        }
        this.templateLoader.data(map);
        output(template(templateName));
        Template template = template(templateName);
        template.output(this.outputPath);
    }
    public Template template(String templateName) {
        return template(new File(path(this.templatePath) + templateName));
@@ -46,35 +46,6 @@
        }
        return this.templateLoader.template(file);
    }
//    public void columnProcess(Map<String, Object> data, String templateName) {
//        process;
//    }
//
//
//    public void articleProcess(Map<String, Object> data, String templateName) {
//        process(data, templateName);
//    }
    public void output(Template template) {
        Document document = template.getDocument();
        List<PartNode> partNodes = template.getPartNodes();
        StringBuilder sb = new StringBuilder();
        for (PartNode partNode : partNodes) {
            sb.append(partNode.getHtml());
        }
        document.body().empty().html(sb.toString());
        String outPath = path(this.outputPath);
        String html = document.html();
        try {
            FileOutputStream outputStream = new FileOutputStream(outPath + template.getName());
            outputStream.write(html.getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private String path(String path) {
src/main/java/com/xcong/farmer/cms/core/template/TemplateLoader.java
@@ -22,22 +22,7 @@
    }
    public Template template(File file) {
        Document document = null;
        try {
            document = Jsoup.parse(file, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (document == null) {
            throw new NullPointerException();
        }
        Template template = new Template();
        template.setDocument(document);
        template.setName(file.getName());
        template.systemData(this.systemData);
        Template template = new Template(file, this.systemData);
        template.parser();
        return template;
    }
src/main/java/com/xcong/farmer/cms/modules/core/service/ICmsCoreService.java
@@ -10,7 +10,7 @@
     * @param id
     * @param templateName
     */
    void articleProcess(Long id, String templateName);
    void articleProcess(Long id, String templateName, String templatPath);
    /**
     * 栏目编译
src/main/java/com/xcong/farmer/cms/modules/core/service/impl/CmsCoreServiceImpl.java
@@ -22,10 +22,13 @@
    private TemplateConfiguration cfg;
    @Override
    public void articleProcess(Long id, String templateName) {
    public void articleProcess(Long id, String templateName, String templatePath) {
        Map<String, Object> data = new HashMap<>();
        data.put("id", id);
        data.put("companyId", 23L);
        data.put("templateType", "article");
        data.put("templatePath", templatePath);
        data.put("templateName", id);
        if (StrUtil.isEmpty(templateName)) {
            templateName = "defualt.artile.html";
        }
@@ -35,6 +38,8 @@
    @Override
    public void columnProcess(Map<String, Object> data, String templateName, boolean article) {
        data.put("companyId", 23L);
        data.put("templateType", "column");
        data.put("page", 1);
        if (StrUtil.isEmpty(templateName)) {
            templateName = "defualt.list.html";
        }
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/ReleaseServiceImpl.java
@@ -35,7 +35,7 @@
        ArticleEntity article = articleMapper.selectById(id);
        ColumnEntity column = columnMapper.selectById(article.getColumnId());
        cmsCoreService.articleProcess(article.getId(), column.getArticleTemplate());
        cmsCoreService.articleProcess(article.getId(), column.getArticleTemplate(), column.getPath());
    }
    @Override
@@ -50,7 +50,7 @@
            ColumnEntity parent = columnMapper.selectById(columnEntity.getParentId());
            map.put("parentCode", parent.getColumnCode());
        }
        map.put("templatePath", columnEntity.getPath());
        cmsCoreService.columnProcess(map, columnEntity.getListTemplate(), article);
    }
}