html工具类

实现的功能就是将提交的HTML文件处理成可以用富文本编辑器进行在线编辑

package com.delin.contract.coder.coderUtil;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Text;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultText;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class getHtmlCharacter {
    public static Map<String,String> HtmlCharacter(File file){
        Map<String,String> result=new HashMap();
        // 读取html字符串
        String htmlStr = FileUtil.readString(file, CharsetUtil.defaultCharsetName());

        // 处理非法标签及特殊字符
        htmlStr = htmlStr.replaceAll("<META([\\s\\S]*?)>", "");
        htmlStr = htmlStr.replaceAll("<meta([\\s\\S]*?)>", "");

        htmlStr = htmlStr.replaceAll("&ldquo;", "“");
        htmlStr = htmlStr.replaceAll("&rdquo;", "”");
        htmlStr = htmlStr.replaceAll("&radic;", "√");
        FileUtil.writeString(htmlStr, file,CharsetUtil.defaultCharsetName());

        try {

            // 获取文档sax解析器,读取流获取document对象
            SAXReader reader = new SAXReader();
            org.dom4j.Document doc = reader.read(file);

            // 获取html标签
            Element htmlNode = doc.getRootElement();

            // 获取body标签并递归处理子标签
            Element bodyNode = htmlNode.element("body");
            treeWalk(bodyNode);

            // 分别获取 style 和 body 内容
            Element style = htmlNode.element("head").element("style");
            Element body = htmlNode.element("body");

            String bodyStr = body.asXML();
            bodyStr = bodyStr.replaceAll("nbsp;", "&nbsp;");
            bodyStr = bodyStr.replaceAll("\r|\n", "");

            result.put("style", style.asXML());
            result.put("body", bodyStr);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static void treeWalk(Element element) {

        for (int i = 0, size = element.nodeCount(); i < size; i++) {

            Node node = element.node(i);

            if (node instanceof Element) {

                Element nodeElement = (Element) node;

                int count = nodeElement.nodeCount();
                if(count == 0){
                    String name = nodeElement.getName();
                    if("p".equals(name)){
                        nodeElement.add(new DefaultText("nbsp;"));
                    }
                }

                treeWalk(nodeElement);

            } else if(node instanceof Text){

                Text textNode = (Text)node;

                String text = textNode.getText();
                textNode.setText(text.replaceAll(" ", "nbsp;"));

            }
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值