Java HTML Jsoup Element collectItemRewards(final Element icontab, final BiConsumer collector)

Here you can find the source of collectItemRewards(final Element icontab, final BiConsumer collector)

Description

collect Item Rewards

License

Open Source License

Declaration

static void collectItemRewards(final Element icontab, final BiConsumer<String, Integer> collector) 

Method Source Code

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

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import java.util.function.BiConsumer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.nodes.Element;

public class Main {
    static void collectItemRewards(final Element icontab, final BiConsumer<String, Integer> collector) {
        Objects.requireNonNull(icontab);

        final Map<String, String> itemNamesByIconId = new LinkedHashMap<>();
        final Map<String, Integer> itemQuantitiesByIconId = new LinkedHashMap<>();

        // Item names are contained in the actual icontab, as are placeholders for the icon and quantity
        for (final Element iconPlaceholder : icontab.select("th[id]")) {
            final String iconId = iconPlaceholder.id();
            // the next element is a td with the link to the actual item
            final String itemName = iconPlaceholder.nextElementSibling().getElementsByTag("a").first().ownText();
            itemNamesByIconId.put(iconId, itemName);
        }//from w w  w .j  a v  a  2s. c  o m

        // Item quantities are filled through JavaScript.
        // Find all script elements in the document containing icontab initialization code
        final List<Element> itemScripts = icontab.ownerDocument().select("script:containsData(icontab)");

        for (final Element script : itemScripts) {
            // Parse JavaScript lines like
            // $WH.ge('icontab-icon1').appendChild(g_items.createIcon(115793, 1, "3"));
            // We're interested in what's inside ge() - the icon box ID -
            // and the contents of the last quotes (item quantity)
            final Pattern iconInitRegex = Pattern.compile(
                    Pattern.quote("$WH.ge('") + "([^']+)" + Pattern.quote("').appendChild(") + "[A-Za-z0-9_]+"
                            + Pattern.quote(".createIcon(") + "[^\"]+\"([^\"]+)\"" + Pattern.quote("));"));
            final Matcher matcher = iconInitRegex.matcher(script.data());

            while (matcher.find()) {
                // group 1 is icon box element ID, group 2 is item quantity (or 0 if no quantity should be displayed)
                itemQuantitiesByIconId.put(matcher.group(1), Integer.parseInt(matcher.group(2)));
            }
        }

        itemNamesByIconId.forEach((iconId, itemName) -> {
            Integer itemQuantity = itemQuantitiesByIconId.get(iconId);

            // "0" means draw no quantity on the icon
            if (itemQuantity != null && (itemQuantity == 0 || itemQuantity == 1)) {
                itemQuantity = null;
            }

            collector.accept(itemName, itemQuantity);
        });
    }
}

Related

  1. addStyle(Element element, String styleToBeAdded)
  2. addTag(Element e, String tagName)
  3. appendFragment(Element e, String fragment)
  4. appendText(Element element, StringBuffer stringBuffer)
  5. childrenByTag(final Element element, final String tag)
  6. collectTextUntilNextTag(final Element header, final String nextTagName)
  7. containsLink(Element element, String link)
  8. convertLinksToStrings(Elements links)
  9. convertNodeToText(Element element)