All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dromara.email.comm.utils.HtmlUtil Maven / Gradle / Ivy

The newest version!
package org.dromara.email.comm.utils;

import org.dromara.email.comm.errors.MailException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * HtmlUtil
 * 

Html相关工具 * * @author :Wind * 2023/6/7 20:15 **/ public final class HtmlUtil { private HtmlUtil() { } /** * readHtml *

从resource读取模板文件 * * @param name 模板文件名 * @author :Wind */ public static List readHtml(String name,Class clazz) throws MailException { try (InputStream is = clazz.getResourceAsStream("/template/" + name)) { return readHtml(is); } catch (IOException e) { throw new MailException(e); } } /** * readHtml *

从自定义路径读取模板文件 * * @param file 自定义路径file * @author :Wind */ public static List readHtml(File file) throws MailException { try (InputStream ip = Files.newInputStream(file.toPath())) { return readHtml(ip); } catch (IOException e) { throw new MailException(e); } } /** * readHtml *

从输入流读取模板文件 * * @param inputStream 输入流 * @author :Wind */ public static List readHtml(InputStream inputStream) throws MailException { List data = new ArrayList<>(); if (Objects.isNull(inputStream)) { throw new MailException("The template could not be found!"); } try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = br.readLine()) != null) { data.add(line); } } catch (IOException e) { throw new MailException(e); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return data; } /** * replacePlaceholder *

将所包含占位符的字符串替换为固定值 * * @param data 源数据 * @param parameter key为占位符名称 value为占位符应替换的值 * @author :Wind */ public static List replacePlaceholder(List data, Map parameter) { for (int i = 0; i < data.size(); i++) { for (Map.Entry s : parameter.entrySet()) { String piece = piece(s.getKey()); if (data.get(i).contains(piece)){ String replace = data.get(i).replace(piece, s.getValue()); data.set(i,replace); } } } return data; } /** * pieceHtml *

将数据拼合为html * * @param data 需要拼合的数据 * @author :Wind */ public static String pieceHtml(List data) { StringBuilder sb = new StringBuilder(); for (String datum : data) { sb.append(datum); sb.append("\r\n"); } return sb.toString(); } /** * piece *

将参数拼合为完整占位符 * * @author :Wind */ public static String piece(String parameter) { return "#{" + parameter + "}"; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy