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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations