Search in sources :

Example 46 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class JerichoHtmlUtils method findElementByTagName.

/**
 * Find a tag.
 * @param root Parent tag.
 * @param tagName Tag to search.
 * @return A tag element or <code>null</code> if not found.
 */
public static Element findElementByTagName(Segment root, String tagName) {
    Element ret = null;
    List<Element> elements = root.getAllElements(tagName);
    if (elements.size() > 0) {
        ret = elements.get(0);
    }
    return ret;
}
Also used : Element(net.htmlparser.jericho.Element)

Example 47 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class JerichoHtmlUtils method findElementsByTagNameAndAttributeValue.

/**
 * Find tags.
 * @param root Parent tag.
 * @param tagName Tag to search.
 * @param attrName Attribute to look.
 * @param expectedValue Expected value for this attribute.
 * @return A possibly empty list of tag elements.
 */
public static List<Element> findElementsByTagNameAndAttributeValue(Segment root, String tagName, String attrName, String expectedValue) {
    List<Element> ret = new ArrayList<Element>();
    List<Element> elements = root.getAllElements(tagName);
    for (Element element : elements) {
        StartTag tag = element.getStartTag();
        String value = tag.getAttributeValue(attrName);
        if (expectedValue.equals(value)) {
            ret.add(element);
        }
    }
    return ret;
}
Also used : Element(net.htmlparser.jericho.Element) ArrayList(java.util.ArrayList) StartTag(net.htmlparser.jericho.StartTag)

Example 48 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class JerichoHtmlUtils method findElementByTagNameAndAttributeValue.

/**
 * Find a tag.
 * @param root Parent tag.
 * @param tagName Tag to search.
 * @param attrName Attribute to look.
 * @param expectedValue Expected value for this attribute.
 * @return A tag element or <code>null</code> if not found.
 */
public static Element findElementByTagNameAndAttributeValue(Segment root, String tagName, String attrName, String expectedValue) {
    Element ret = null;
    List<Element> elements = root.getAllElements(tagName);
    for (Element element : elements) {
        StartTag tag = element.getStartTag();
        String value = tag.getAttributeValue(attrName);
        if (expectedValue.equals(value)) {
            ret = element;
            break;
        }
    }
    return ret;
}
Also used : Element(net.htmlparser.jericho.Element) StartTag(net.htmlparser.jericho.StartTag)

Example 49 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class QuestPageParser method parseQuestSection.

private QuestDescription parseQuestSection(Element questSection) {
    QuestDescription ret = null;
    try {
        _quest = new QuestDescription();
        Element officialSection = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(questSection, HTMLElementName.DIV, "class", "officialsection");
        if (officialSection != null) {
            parseQuestDescription(officialSection);
        }
        // Texts
        StringBuilder description = new StringBuilder();
        StringBuilder bestower = new StringBuilder();
        StringBuilder bestowerText = new StringBuilder();
        StringBuilder objectives = new StringBuilder();
        List<Element> textSections = JerichoHtmlUtils.findElementsByTagNameAndAttributeValue(questSection, HTMLElementName.DIV, "class", "iteminfosection widget ui-corner-all");
        for (Element textSection : textSections) {
            Element titleSection = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(textSection, HTMLElementName.DIV, "class", "widget-head ui-widget-header ui-corner-top");
            if (titleSection != null) {
                String textSectionTitle = CharacterReference.decodeCollapseWhiteSpace(titleSection.getContent());
                Element contentsSection = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(textSection, HTMLElementName.DIV, "class", "widget-body ui-widget-content ui-corner-bottom");
                if (contentsSection != null) {
                    StringBuilder link = null;
                    StringBuilder text = null;
                    if ("Description".equals(textSectionTitle)) {
                        text = description;
                    } else if ("Bestower".equals(textSectionTitle)) {
                        link = bestower;
                        text = bestowerText;
                    } else if ("Objectives".equals(textSectionTitle)) {
                        text = objectives;
                    }
                    if (text != null) {
                        Element bestowerTag = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(contentsSection, HTMLElementName.DIV, "class", "bestowertext");
                        if (bestowerTag != null) {
                            String contents = JerichoHtmlUtils.getTextFromTag(bestowerTag);
                            text.append(contents);
                        }
                    }
                    if (link != null) {
                        Element bestowerLink = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(contentsSection, HTMLElementName.DIV, "class", "bestowerlink");
                        if (bestowerLink != null) {
                            String contents = JerichoHtmlUtils.getTextFromTag(bestowerLink);
                            link.append(contents);
                        }
                    }
                }
            }
        }
        _quest.setDescription(description.toString().trim());
        _quest.setBestower(bestower.toString().trim());
        _quest.setBestowerText(bestowerText.toString().trim());
        _quest.setObjectives(objectives.toString().trim());
        ret = _quest;
        _quest = null;
    } catch (Exception e) {
        ret = null;
        _logger.error("Quest [" + _key + "]. Cannot parse quest section!", e);
    }
    return ret;
}
Also used : QuestDescription(delta.games.lotro.lore.quests.QuestDescription) Element(net.htmlparser.jericho.Element)

Example 50 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class QuestPageParser method findIdentifiers.

private void findIdentifiers(List<QuestDescription> quests) {
    String url = "http://lorebook.lotro.com/index.php?title=Quest:" + _key + "&action=edit";
    DownloadService downloader = DownloadService.getInstance();
    try {
        String page = downloader.getPage(url);
        Source s = new Source(page);
        // <textarea id="wpTextbox1"
        Element pageSource = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(s, HTMLElementName.TEXTAREA, "id", "wpTextbox1");
        String text = JerichoHtmlUtils.getTextFromTag(pageSource);
        parsePageSource(text, quests);
    } catch (Exception e) {
        _logger.error("Parsing error", e);
    }
}
Also used : Element(net.htmlparser.jericho.Element) DownloadService(delta.games.lotro.utils.DownloadService) InputSource(org.xml.sax.InputSource) Source(net.htmlparser.jericho.Source)

Aggregations

Element (net.htmlparser.jericho.Element)70 Source (net.htmlparser.jericho.Source)17 DownloadService (delta.games.lotro.utils.DownloadService)11 ArrayList (java.util.ArrayList)11 Segment (net.htmlparser.jericho.Segment)6 InputSource (org.xml.sax.InputSource)6 Context (com.cflint.plugins.Context)4 Matcher (java.util.regex.Matcher)4 StartTag (net.htmlparser.jericho.StartTag)4 BasicStatsSet (delta.games.lotro.character.stats.BasicStatsSet)3 Item (delta.games.lotro.lore.items.Item)3 List (java.util.List)3 Attribute (net.htmlparser.jericho.Attribute)3 CFScriptStatement (cfml.parsing.cfscript.script.CFScriptStatement)2 ParseException (cfml.parsing.reporting.ParseException)2 CFLintScanException (com.cflint.exception.CFLintScanException)2 ContextMessage (com.cflint.plugins.Context.ContextMessage)2 Money (delta.games.lotro.common.Money)2 CraftingResult (delta.games.lotro.lore.crafting.recipes.CraftingResult)2 Ingredient (delta.games.lotro.lore.crafting.recipes.Ingredient)2