Java Swing HTML stripHTML(String html)

Here you can find the source of stripHTML(String html)

Description

strip HTML

License

Open Source License

Declaration

public static String stripHTML(String html) 

Method Source Code


//package com.java2s;
/*//w  w  w .  j av  a 2s .c  om
 * Copyright (c) 2014, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.IOException;
import java.io.StringReader;

public class Main {
    public static String stripHTML(String 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
                public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
                    if (t.breaksFlow()) {
                        builder.append("\n");
                    }
                }
            }, Boolean.TRUE);

            return builder.toString();
        } catch (IOException ex) {
            return html;
        }
    }
}

Related

  1. nameOf(Element element)
  2. plainToHtml(String plain)
  3. rtfToBody(String rtf)
  4. rtfToHtml(Reader rtf)
  5. rtfToHtml(String rtf)
  6. toBold(final T label)
  7. write(Writer w, HTML.Tag tag, AttributeSet attributes)