package com.mzl.flower.utils;
|
|
import javax.swing.text.html.HTMLEditorKit;
|
import javax.swing.text.html.parser.ParserDelegator;
|
import java.io.*;
|
|
public class Html2TextUtil extends HTMLEditorKit.ParserCallback {
|
|
private static Html2TextUtil html2Text = new Html2TextUtil();
|
|
StringBuffer s;
|
|
public Html2TextUtil() {
|
}
|
|
public void parse(String str) throws IOException {
|
|
InputStream iin = new ByteArrayInputStream(str.getBytes());
|
Reader in = new InputStreamReader(iin);
|
s = new StringBuffer();
|
ParserDelegator delegator = new ParserDelegator();
|
delegator.parse(in, this, Boolean.TRUE);
|
iin.close();
|
in.close();
|
}
|
|
public void handleText(char[] text, int pos) {
|
s.append(text);
|
}
|
|
public String getText() {
|
return s.toString();
|
}
|
|
public static String getContent(String str) {
|
if(str == null || "".equals(str.trim())){
|
return str;
|
}
|
try {
|
html2Text.parse(str);
|
} catch (IOException e) {
|
}
|
return html2Text.getText();
|
}
|
}
|