package top.octopusyan.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * @author : octopus yan
 * @email : octopus_yan@foxmail.com
 * @description : html解析工具类
 * @create : 2022-4-8 11:04
 */
public class JsoupUtil {

    public static Document getDocument(String htmlStr) {
        return Jsoup.parse(htmlStr);
    }

    public static boolean isAlertSuccess(String htmlStr) {
        return !htmlStr.contains("alert-danger");
    }

    public static String getErrorMessage(String htmlStr) {
        return getDocument(htmlStr).body().getElementsByClass("alert alert-danger alert-dismissable").text().substring(1);
    }

    public static String getSuccessMessage(String htmlStr) {
        return getDocument(htmlStr).getElementsByClass("alert alert-success alert-dismissable").text().substring(1);
    }

    public static String getServerConfiguration(String htmlStr) {
        Document document = getDocument(htmlStr);
        String configuration = document.getElementsByTag("pre").get(0).text();
        int end = configuration.indexOf("\n\n");
        // 服务器配置获取失败
        if (end == -1 && !configuration.contains("[common]")) return null;
        // 没有
        if (end == -1) {
            configuration += "\n\n";
            return configuration;
        }
        return configuration.substring(0, end + 2);
    }
}
