001package top.cenze.rulepolicy.frame.utils;
002
003import lombok.NoArgsConstructor;
004
005import javax.swing.text.html.HTMLEditorKit;
006import javax.swing.text.html.parser.ParserDelegator;
007import java.io.*;
008
009@NoArgsConstructor
010public class Html2TextUtil extends HTMLEditorKit.ParserCallback {
011    private static Html2TextUtil html2TextUtil = new Html2TextUtil();
012
013    StringBuffer s;
014
015    public void parse(String str) throws IOException {
016
017        InputStream iin = new ByteArrayInputStream(str.getBytes());
018        Reader in = new InputStreamReader(iin);
019        s = new StringBuffer();
020        ParserDelegator delegator = new ParserDelegator();
021        // the third parameter is TRUE to ignore charset directive
022        delegator.parse(in, this, Boolean.TRUE);
023        iin.close();
024        in.close();
025    }
026
027    @Override
028    public void handleText(char[] text, int pos) {
029        s.append(text);
030    }
031
032    public String getText() {
033        return s.toString();
034    }
035
036    public static String getContent(String str) {
037        try {
038            html2TextUtil.parse(str);
039        } catch (IOException e) {
040            // TODO Auto-generated catch block
041            e.printStackTrace();
042        }
043        return html2TextUtil.getText();
044    }
045}