Java Utililty Methods Swing HTML

List of utility methods to do Swing HTML

Description

The list of methods to do Swing HTML are organized into topic(s).

Method

StringhtmlToPlain(String html)
html To Plain
HTMLEditorKit htmlParser = new HTMLEditorKit();
Document document = htmlParser.createDefaultDocument();
try {
    htmlParser.read(new ByteArrayInputStream(html.getBytes()), document, 0);
    String plain = document.getText(0, document.getLength());
    return plain;
} catch (Exception e) {
    throw new RuntimeException("Error converting HTML to plain text", e);
...
booleanisUnderline(Element element)
is Underline
return containsAttribute(element, CSS.Attribute.TEXT_DECORATION, "underline");
StyleSheetloadStyleSheet(URL url)
Loads a set of rules that have been specified in terms of CSS1 grammar.
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StyleSheet ss = new StyleSheet();
ss.loadRules(reader, null);
return ss;
StyleSheetmakeStyleSheet(String name)
make Style Sheet
try {
    StyleSheet sheet = new StyleSheet();
    Reader reader = new InputStreamReader(System.class.getResourceAsStream("/css/" + name + ".css"));
    sheet.loadRules(reader, null);
    reader.close();
    return sheet;
} catch (Exception ex) {
    ex.printStackTrace();
...
HTML.TagnameOf(Element element)
name Of
return (HTML.Tag) element.getAttributes().getAttribute(AttributeSet.NameAttribute);
StringplainToHtml(String plain)
plain To Html
DefaultEditorKit plainParser = new DefaultEditorKit();
Document document = plainParser.createDefaultDocument();
try {
    plainParser.read(new ByteArrayInputStream(plain.getBytes()), document, 0);
    String html = document.getText(0, document.getLength());
    return html;
} catch (Exception e) {
    throw new RuntimeException("Error converting plain text to HTML", e);
...
StringrtfToBody(String rtf)
rtf To Body
String html = rtfToHtml(new StringReader(rtf));
if (html.contains("<body")) {
    html = BODY_PATTERN.matcher(html).replaceAll("$1");
return html;
StringrtfToHtml(Reader rtf)
rtf To Html
JEditorPane p = new JEditorPane();
p.setContentType("text/rtf");
EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
try {
    kitRtf.read(rtf, p.getDocument(), 0);
    kitRtf = null;
    EditorKit kitHtml = p.getEditorKitForContentType("text/html");
    Writer writer = new StringWriter();
...
StringrtfToHtml(String rtf)
rtf To Html
return rtfToHtml(new StringReader(rtf));
StringstripHTML(String html)
strip HTML
try {
    final StringBuilder builder = new StringBuilder();
    new ParserDelegator().parse(new StringReader(html), new HTMLEditorKit.ParserCallback() {
        @Override
        public void handleText(char[] text, int pos) {
            builder.append(text);
        @Override
...