Java HTML Jsoup Element getMoney(final Element container)

Here you can find the source of getMoney(final Element container)

Description

get Money

License

Open Source License

Declaration

static int getMoney(final Element container) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;

public class Main {
    static int getMoney(final Element container) {
        // Money rewards
        final List<Element> allMoneyElements = new ArrayList<Element>();
        allMoneyElements.addAll(container.select("span.moneygold"));
        allMoneyElements.addAll(container.select("span.moneysilver"));
        allMoneyElements.addAll(container.select("span.moneycopper"));

        if (allMoneyElements.isEmpty()) {
            return 0;
        }/*  w  ww  .ja  v a 2 s .  co  m*/

        // Find the leftmost money element (so we parse only the first group - second group is max level rewards)
        int money = 0;
        Element leftEl = null;

        for (final Element el : allMoneyElements) {
            if (leftEl == null || el.siblingIndex() < leftEl.siblingIndex()) {
                leftEl = el;
            }
        }

        Node node;

        // Parse all directly adjacent span nodes, possibly with whitespace in between
        for (node = leftEl; (node instanceof Element && ((Element) node).tagName().equals("span"))
                || (node instanceof TextNode && ((TextNode) node).text().trim().isEmpty()); node = node
                        .nextSibling()) {
            if (!(node instanceof Element)) {
                continue;
            }

            final Element el = (Element) node;

            if (el.hasClass("moneygold")) {
                money += Integer.parseInt(el.ownText()) * 10000;
            } else if (el.hasClass("moneysilver")) {
                money += Integer.parseInt(el.ownText()) * 100;
            } else if (el.hasClass("moneycopper")) {
                money += Integer.parseInt(el.ownText());
            }
        }

        // If the money group we just parsed is followed immediately by max level text, then it means that the only
        // money group is the max-level money group, so we should skip it.
        if (node instanceof TextNode && ((TextNode) node).text().trim().startsWith("if completed at level")) {
            return 0;
        }

        return money;
    }
}

Related

  1. getFirstWithOwnText(final Elements elementList, final String text)
  2. getFormattedText(Element element)
  3. getIndustry(Element element)
  4. getInt(Elements td, int i)
  5. getLink(Element element, int index)
  6. getNameFromAttributes(org.jsoup.nodes.Element e)
  7. getOneSubItemOfClass(Element parent, String className)
  8. getReviewAuthorIDFromMultipleSelect(Element doc, String location, String locationValue)
  9. getSingleElement(final Elements elements)