Search in sources :

Example 11 with Element

use of com.smartandroid.sa.tag.nodes.Element in project SmartAndroidSource by jaychou2012.

the class HtmlTreeBuilder method resetInsertionMode.

void resetInsertionMode() {
    boolean last = false;
    Iterator<Element> it = stack.descendingIterator();
    while (it.hasNext()) {
        Element node = it.next();
        if (!it.hasNext()) {
            last = true;
            node = contextElement;
        }
        String name = node.nodeName();
        if ("select".equals(name)) {
            transition(HtmlTreeBuilderState.InSelect);
            // frag
            break;
        } else if (("td".equals(name) || "td".equals(name) && !last)) {
            transition(HtmlTreeBuilderState.InCell);
            break;
        } else if ("tr".equals(name)) {
            transition(HtmlTreeBuilderState.InRow);
            break;
        } else if ("tbody".equals(name) || "thead".equals(name) || "tfoot".equals(name)) {
            transition(HtmlTreeBuilderState.InTableBody);
            break;
        } else if ("caption".equals(name)) {
            transition(HtmlTreeBuilderState.InCaption);
            break;
        } else if ("colgroup".equals(name)) {
            transition(HtmlTreeBuilderState.InColumnGroup);
            // frag
            break;
        } else if ("table".equals(name)) {
            transition(HtmlTreeBuilderState.InTable);
            break;
        } else if ("head".equals(name)) {
            transition(HtmlTreeBuilderState.InBody);
            // frag
            break;
        } else if ("body".equals(name)) {
            transition(HtmlTreeBuilderState.InBody);
            break;
        } else if ("frameset".equals(name)) {
            transition(HtmlTreeBuilderState.InFrameset);
            // frag
            break;
        } else if ("html".equals(name)) {
            transition(HtmlTreeBuilderState.BeforeHead);
            // frag
            break;
        } else if (last) {
            transition(HtmlTreeBuilderState.InBody);
            // frag
            break;
        }
    }
}
Also used : FormElement(com.smartandroid.sa.tag.nodes.FormElement) Element(com.smartandroid.sa.tag.nodes.Element)

Example 12 with Element

use of com.smartandroid.sa.tag.nodes.Element in project SmartAndroidSource by jaychou2012.

the class Selector method select.

/**
	 * Find elements matching selector.
	 * 
	 * @param query
	 *            CSS selector
	 * @param roots
	 *            root elements to descend into
	 * @return matching elements, empty if not
	 */
public static Elements select(String query, Iterable<Element> roots) {
    Validate.notEmpty(query);
    Validate.notNull(roots);
    LinkedHashSet<Element> elements = new LinkedHashSet<Element>();
    for (Element root : roots) {
        elements.addAll(select(query, root));
    }
    return new Elements(elements);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Element(com.smartandroid.sa.tag.nodes.Element)

Example 13 with Element

use of com.smartandroid.sa.tag.nodes.Element in project SmartAndroidSource by jaychou2012.

the class ListLinks method main.

public static void main(String[] args) throws IOException {
    Validate.isTrue(args.length == 1, "usage: supply url to fetch");
    String url = args[0];
    print("Fetching %s...", url);
    Document doc = SmartTag.connect(url).get();
    Elements links = doc.select("a[href]");
    Elements media = doc.select("[src]");
    Elements imports = doc.select("link[href]");
    print("\nMedia: (%d)", media.size());
    for (Element src : media) {
        if (src.tagName().equals("img"))
            print(" * %s: <%s> %sx%s (%s)", src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), trim(src.attr("alt"), 20));
        else
            print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
    }
    print("\nImports: (%d)", imports.size());
    for (Element link : imports) {
        print(" * %s <%s> (%s)", link.tagName(), link.attr("abs:href"), link.attr("rel"));
    }
    print("\nLinks: (%d)", links.size());
    for (Element link : links) {
        print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
    }
}
Also used : Element(com.smartandroid.sa.tag.nodes.Element) Document(com.smartandroid.sa.tag.nodes.Document) Elements(com.smartandroid.sa.tag.select.Elements)

Example 14 with Element

use of com.smartandroid.sa.tag.nodes.Element in project SmartAndroidSource by jaychou2012.

the class HtmlTreeBuilder method insert.

Element insert(Token.StartTag startTag) {
    // won't generate this fake end tag.
    if (startTag.isSelfClosing()) {
        Element el = insertEmpty(startTag);
        stack.add(el);
        // handles <script />,
        tokeniser.transition(TokeniserState.Data);
        // otherwise needs
        // breakout steps from
        // script data
        // ensure we get out
        tokeniser.emit(new Token.EndTag(el.tagName()));
        // processing
        return el;
    }
    Element el = new Element(Tag.valueOf(startTag.name()), baseUri, startTag.attributes);
    insert(el);
    return el;
}
Also used : FormElement(com.smartandroid.sa.tag.nodes.FormElement) Element(com.smartandroid.sa.tag.nodes.Element)

Example 15 with Element

use of com.smartandroid.sa.tag.nodes.Element in project SmartAndroidSource by jaychou2012.

the class HtmlTreeBuilder method clearFormattingElementsToLastMarker.

void clearFormattingElementsToLastMarker() {
    while (!formattingElements.isEmpty()) {
        Element el = formattingElements.peekLast();
        formattingElements.removeLast();
        if (el == null)
            break;
    }
}
Also used : FormElement(com.smartandroid.sa.tag.nodes.FormElement) Element(com.smartandroid.sa.tag.nodes.Element)

Aggregations

Element (com.smartandroid.sa.tag.nodes.Element)21 FormElement (com.smartandroid.sa.tag.nodes.FormElement)13 Document (com.smartandroid.sa.tag.nodes.Document)4 Elements (com.smartandroid.sa.tag.select.Elements)2 Attribute (com.smartandroid.sa.tag.nodes.Attribute)1 Attributes (com.smartandroid.sa.tag.nodes.Attributes)1 Node (com.smartandroid.sa.tag.nodes.Node)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1