Search in sources :

Example 1 with Elements

use of com.smartandroid.sa.tag.select.Elements in project SmartAndroidSource by jaychou2012.

the class Document method normaliseStructure.

// merge multiple <head> or <body> contents into one, delete the remainder,
// and ensure they are owned by <html>
private void normaliseStructure(String tag, Element htmlEl) {
    Elements elements = this.getElementsByTag(tag);
    // will always be available as
    Element master = elements.first();
    // created above if not existent
    if (elements.size() > 1) {
        // dupes, move contents to master
        List<Node> toMove = new ArrayList<Node>();
        for (int i = 1; i < elements.size(); i++) {
            Node dupe = elements.get(i);
            for (Node node : dupe.childNodes) toMove.add(node);
            dupe.remove();
        }
        for (Node dupe : toMove) master.appendChild(dupe);
    }
    // ensure parented by <html>
    if (!master.parent().equals(htmlEl)) {
        // includes remove()
        htmlEl.appendChild(master);
    }
}
Also used : ArrayList(java.util.ArrayList) Elements(com.smartandroid.sa.tag.select.Elements)

Example 2 with Elements

use of com.smartandroid.sa.tag.select.Elements in project SmartAndroidSource by jaychou2012.

the class Element method getElementById.

/**
	 * Find an element by ID, including or under this element.
	 * <p>
	 * Note that this finds the first matching ID, starting with this element.
	 * If you search down from a different starting point, it is possible to
	 * find a different element by ID. For unique element by ID within a
	 * Document, use {@link Document#getElementById(String)}
	 * 
	 * @param id
	 *            The ID to search for.
	 * @return The first matching element by ID, starting with this element, or
	 *         null if none found.
	 */
public Element getElementById(String id) {
    Validate.notEmpty(id);
    Elements elements = Collector.collect(new Evaluator.Id(id), this);
    if (elements.size() > 0)
        return elements.get(0);
    else
        return null;
}
Also used : Elements(com.smartandroid.sa.tag.select.Elements) Evaluator(com.smartandroid.sa.tag.select.Evaluator)

Example 3 with Elements

use of com.smartandroid.sa.tag.select.Elements in project SmartAndroidSource by jaychou2012.

the class Element method parents.

/**
	 * Get this element's parent and ancestors, up to the document root.
	 * 
	 * @return this element's stack of parents, closest first.
	 */
public Elements parents() {
    Elements parents = new Elements();
    accumulateParents(this, parents);
    return parents;
}
Also used : Elements(com.smartandroid.sa.tag.select.Elements)

Example 4 with Elements

use of com.smartandroid.sa.tag.select.Elements in project SmartAndroidSource by jaychou2012.

the class Element method siblingElements.

/**
	 * Get sibling elements. If the element has no sibling elements, returns an
	 * empty list. An element is not a sibling of itself, so will not be
	 * included in the returned list.
	 * 
	 * @return sibling elements
	 */
public Elements siblingElements() {
    if (parentNode == null)
        return new Elements(0);
    List<Element> elements = parent().children();
    Elements siblings = new Elements(elements.size() - 1);
    for (Element el : elements) if (el != this)
        siblings.add(el);
    return siblings;
}
Also used : Elements(com.smartandroid.sa.tag.select.Elements)

Example 5 with Elements

use of com.smartandroid.sa.tag.select.Elements 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)

Aggregations

Elements (com.smartandroid.sa.tag.select.Elements)6 Element (com.smartandroid.sa.tag.nodes.Element)2 Document (com.smartandroid.sa.tag.nodes.Document)1 FormElement (com.smartandroid.sa.tag.nodes.FormElement)1 Evaluator (com.smartandroid.sa.tag.select.Evaluator)1 ArrayList (java.util.ArrayList)1