All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.bliki.htmlcleaner.util.HtmlFormExtractor Maven / Gradle / Ivy

The newest version!
package info.bliki.htmlcleaner.util;

import info.bliki.htmlcleaner.TagNode;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * Extracts a <div class="errorbox">...error text...</div>
 * content.
 *
 */
public class HtmlFormExtractor extends AbstractHtmlExtractor {
    public static final String FORM_TAG = "form";

    public static final String INPUT_TAG = "input";

    public static final String TEXTAREA_TAG = "textarea";

    private final String fFormID;

    public HtmlFormExtractor(HtmlForm resultForm) {
        this(resultForm, "editform");
    }

    public HtmlFormExtractor(HtmlForm resultForm, String formID) {
        super(resultForm);
        fFormID = formID;
    }

    @Override
    public void appendContent(List nodes) {
        if (nodes != null && !nodes.isEmpty()) {
            for (Object item : nodes) {
                if (item != null && (item instanceof TagNode)) {
                    TagNode tagNode = (TagNode) item;
                    if (tagNode.getName().equalsIgnoreCase(INPUT_TAG) || tagNode.getName().equalsIgnoreCase(TEXTAREA_TAG)) {
                        Map attributes = tagNode.getAttributes();
                        int sz = attributes.size();
                        HtmlForm.ElementAttribute[] elementAttributes = new HtmlForm.ElementAttribute[sz];
                        Set> eSet = attributes.entrySet();
                        int i = 0;
                        for (Map.Entry entry : eSet) {
                            elementAttributes[i++] = new HtmlForm.ElementAttribute(entry.getKey(), "CDATA", entry.getValue());
                        }
                        fResultObject.addElement(new HtmlForm.Element(tagNode.getName(), elementAttributes));
                    }

                    Map atts = ((TagNode) item).getAttributes();
                    String attributeValue = atts.get("class");
                    if (attributeValue != null && attributeValue.toLowerCase().equals("errorbox")) {

                    } else if (item instanceof List) {
                        @SuppressWarnings("unchecked")
                        final List list = (List) item;
                        appendContent(list);
                    }
                }
            }
        }
    }

    @Override
    public boolean isFound(TagNode tagNode) {
        String tagName = tagNode.getName();
        if (tagName.equals(FORM_TAG)) {
            Map atts = tagNode.getAttributes();
            String attributeValue = atts.get("id");
            if (attributeValue != null && attributeValue.toLowerCase().equals(fFormID)) {
                int method = HtmlForm.POST;
                fResultObject.setID(attributeValue);
                attributeValue = atts.get("name");
                if (attributeValue != null) {
                    fResultObject.setName(attributeValue);
                }
                attributeValue = atts.get("method");
                if (attributeValue != null) {
                    if ("get".equalsIgnoreCase(attributeValue)) {
                        method = HtmlForm.GET;
                        fResultObject.setMethod(method);
                    }
                }
                attributeValue = atts.get("action");
                if (attributeValue != null) {
                    fResultObject.setAction(attributeValue);
                }
                attributeValue = atts.get("enctype");
                if (attributeValue != null) {
                    fResultObject.setEncType(attributeValue);
                }
                return true;
            }
        }
        return false;
    }
}