get Body Element from Swing HTMLDocument - Java Swing

Java examples for Swing:Swing HTML

Description

get Body Element from Swing HTMLDocument

Demo Code


//package com.java2s;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;

public class Main {

    public static Element getBodyElement(final HTMLDocument document) {
        final Element body = findBodyElement(document
                .getDefaultRootElement());
        if (body == null) {
            throw new IllegalArgumentException(
                    "Not found <body> tag in given document.");
        }/*ww w  .  java 2s . c  o m*/

        return body;
    }

    private static Element findBodyElement(final Element element) {
        if (element.getName().equals("p")) {
            return element.getElement(0);
        }

        for (int i = 0; i < element.getElementCount(); i++) {
            final Element child = findBodyElement(element.getElement(i));
            if (child != null) {
                return child;
            }
        }

        return null;
    }
}

Related Tutorials