首页>代码>SpringBoot爬取html生成CHM帮助文档>/springboot-chm/src/main/java/com/simon/springbootchm/util/ChmUtils.java
package com.simon.springbootchm.util;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Template;
import com.simon.springbootchm.vo.ContentsNode;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/**
 * 根据模板生成chm工程文件,继而生成chm
 *
 * @author Simon
 */
public class ChmUtils {
    private String projectName;
    private String basePath;
    Kv kv;

    ChmUtils(String projectName, ArrayList<ContentsNode> nodes, String indexPage, String title) {
        this.projectName = projectName;
        this.basePath = CommandUtils.getPath(projectName);
        if (StrKit.isBlank(indexPage)) {
            indexPage = getFirstPage(nodes);
        }
        if (StrKit.isBlank(title)) {
            title = projectName + "帮助文档";
        }
        this.kv = Kv.by("nodes", ContentsNode.makeTreeData(nodes)).set("projectName", projectName).set("index", "source\\" + indexPage).set("title", title);
    }

    public void make() {
        writePropFile("hhc");
        writePropFile("hhk");
        writePropFile("hhp");
        makeChm();
    }

    public void makeIndexPage() {
        writePropFile("index.html", "source/index.html", "utf8");
        ArrayList<ContentsNode> nodes = (ArrayList<ContentsNode>) kv.get("nodes");
        nodes.add(0, ContentsNode.newTopic("速查表", "index.html"));
    }

    private void makeChm() {
        String command = "\"" + CommandUtils.getPath("chm") + "/hhc.exe\" " + projectName + ".hhp";
        CommandUtils.run(command, null, basePath);
    }

    private void writePropFile(String suffix) {
        writePropFile("main." + suffix, projectName + "." + suffix, "gbk");
    }

    private void writePropFile(String oldName, String newName, String charset) {
        BufferedWriter output = null;
        try {
            output = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(basePath + "/" + newName), charset));
            Template template = CommandUtils.getTemplate("chm", oldName);
            template.render(kv, output);
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String getFirstPage(ArrayList<ContentsNode> nodes) {
        for (ContentsNode node : nodes) {
            if (StrKit.isBlank(node.getPath())) {
                if (node.getChildren() != null && node.getChildren().size() > 0) {
                    return getFirstPage(node.getChildren());
                } else {
                    return "";
                }
            } else {
                return node.getPath();
            }
        }
        return "";
    }

    public static void createChm(String name, ArrayList<ContentsNode> nodes, String indexPage, String title, boolean needMakeIndexPage) {
        ChmUtils makeChm = new ChmUtils(name, nodes, indexPage, title);
        if (needMakeIndexPage) {
            makeChm.makeIndexPage();
        }
        makeChm.make();
    }
}
最近下载更多
139465  LV12 2023年3月29日
crosa_Don  LV18 2023年3月2日
最代码官方  LV167 2023年2月19日
最近浏览更多
茶茶茶百道qq 2023年9月20日
暂无贡献等级
多加两块钱  LV4 2023年6月12日
skook7  LV2 2023年6月1日
szf123  LV12 2023年5月30日
duanzhouyang  LV10 2023年5月12日
Pro_Guoli 2023年5月12日
暂无贡献等级
fewfsdaf  LV4 2023年4月18日
做你的景天  LV7 2023年4月12日
master_guo  LV7 2023年4月12日
ming_123_9715  LV23 2023年3月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友