Search in sources :

Example 1 with Segment

use of net.htmlparser.jericho.Segment in project knox by apache.

the class HtmlFilterReaderBase method processCurrentSegment.

private void processCurrentSegment() {
    Segment segment = parser.getCurrentSegment();
    // ignore it as it was already output along with the previous tag.
    if (segment.getEnd() <= lastSegEnd) {
        return;
    }
    lastSegEnd = segment.getEnd();
    if (segment instanceof Tag) {
        if (segment instanceof StartTag) {
            processStartTag((StartTag) segment);
        } else if (segment instanceof EndTag) {
            processEndTag((EndTag) segment);
        } else {
            writer.write(segment.toString());
        }
    } else {
        processText(segment);
    }
}
Also used : EndTag(net.htmlparser.jericho.EndTag) EndTag(net.htmlparser.jericho.EndTag) Tag(net.htmlparser.jericho.Tag) StartTag(net.htmlparser.jericho.StartTag) Segment(net.htmlparser.jericho.Segment) StartTag(net.htmlparser.jericho.StartTag)

Example 2 with Segment

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

the class JerichoHtmlUtils method getChildNodes.

/**
 * Get all the child nodes of a given parent node.
 * This is a recursive search.
 * @param root Root node.
 * @return A list of nodes.
 */
public static List<Segment> getChildNodes(Element root) {
    List<Segment> nodes = new ArrayList<Segment>();
    Segment seg = root.getContent();
    for (Iterator<Segment> it = seg.getNodeIterator(); it.hasNext(); ) {
        Segment s = it.next();
        nodes.add(s);
    }
    return nodes;
}
Also used : ArrayList(java.util.ArrayList) Segment(net.htmlparser.jericho.Segment)

Example 3 with Segment

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

the class ItemPageParser method parseMoneyReward.

private Money parseMoneyReward(Element rewardDiv) {
    Money m = new Money();
    List<Segment> nodes = JerichoHtmlUtils.getChildNodes(rewardDiv);
    int nb = nodes.size();
    for (int i = 0; i < nb; i++) {
        Segment s = nodes.get(i);
        if (s.getClass() == Segment.class) {
            String text = s.toString();
            if (text.equalsIgnoreCase("Worth"))
                continue;
            int nbCoins = NumericTools.parseInt(s.toString(), 0);
            if ((nbCoins > 0) && (i < nb - 1)) {
                Segment tmp = nodes.get(i + 1);
                if (tmp instanceof StartTag) {
                    String className = ((StartTag) tmp).getAttributeValue("class");
                    if ("coin".equals(className)) {
                        String type = ((StartTag) tmp).getAttributeValue("src");
                        if (type != null) {
                            if (type.contains("silver")) {
                                m.setSilverCoins(nbCoins);
                            } else if (type.contains("copper")) {
                                m.setCopperCoins(nbCoins);
                            } else if (type.contains("gold")) {
                                m.setGoldCoins(nbCoins);
                            } else {
                                _logger.warn("Item [" + _key + "]. Unknown coin type [" + type + "]");
                            }
                        }
                    }
                }
            }
        }
    }
    return m;
/*
     <div class="itemworth">
        Worth&nbsp;&nbsp;&nbsp;3
        <img class="coin" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/currency/silver.gif">
        &nbsp;30
        <img class="coin" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/currency/copper.gif">
     </div>
    */
}
Also used : Money(delta.games.lotro.common.Money) Segment(net.htmlparser.jericho.Segment) StartTag(net.htmlparser.jericho.StartTag)

Example 4 with Segment

use of net.htmlparser.jericho.Segment in project vue-gwt by Axellience.

the class TemplateParser method processElement.

/**
 * Recursive method that will process the whole template DOM tree.
 * @param element Current element being processed
 */
private void processElement(Element element) {
    context.setCurrentSegment(element);
    currentProp = null;
    currentAttribute = null;
    Attributes attributes = element.getAttributes();
    Attribute vForAttribute = attributes != null ? attributes.get("v-for") : null;
    if (vForAttribute != null) {
        // Add a context layer for our v-for
        context.addContextLayer();
        // Process the v-for expression, and update our attribute
        String processedVForValue = processVForValue(vForAttribute.getValue());
        outputDocument.replace(vForAttribute.getValueSegment(), processedVForValue);
    }
    // Process the element
    if (attributes != null)
        processElementAttributes(element);
    // Process text segments
    StreamSupport.stream(((Iterable<Segment>) element::getNodeIterator).spliterator(), false).filter(segment -> !(segment instanceof Tag) && !(segment instanceof CharacterReference)).filter(segment -> {
        for (Element child : element.getChildElements()) if (child.encloses(segment))
            return false;
        return true;
    }).forEach(this::processTextNode);
    // Recurse downwards
    element.getChildElements().forEach(this::processElement);
    // After downward recursion, pop the context layer
    if (vForAttribute != null)
        context.popContextLayer();
}
Also used : Attributes(net.htmlparser.jericho.Attributes) TemplateExpression(com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression) CastExpr(com.github.javaparser.ast.expr.CastExpr) Any(jsinterop.base.Any) OutputDocument(net.htmlparser.jericho.OutputDocument) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) Type(com.github.javaparser.ast.type.Type) GeneratorsUtil.stringTypeToTypeName(com.axellience.vuegwt.processors.utils.GeneratorsUtil.stringTypeToTypeName) Prop(com.axellience.vuegwt.core.annotations.component.Prop) VariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo) CharacterReference(net.htmlparser.jericho.CharacterReference) Expression(com.github.javaparser.ast.expr.Expression) StreamSupport(java.util.stream.StreamSupport) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) Source(net.htmlparser.jericho.Source) LinkedList(java.util.LinkedList) Messager(javax.annotation.processing.Messager) NodeWithType(com.github.javaparser.ast.nodeTypes.NodeWithType) Element(net.htmlparser.jericho.Element) GeneratorsNameUtil.propNameToAttributeName(com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.propNameToAttributeName) TemplateParserLoggerProvider(com.axellience.vuegwt.processors.component.template.parser.jericho.TemplateParserLoggerProvider) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TemplateParserResult(com.axellience.vuegwt.processors.component.template.parser.result.TemplateParserResult) Set(java.util.Set) NameExpr(com.github.javaparser.ast.expr.NameExpr) LocalComponent(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent) Config(net.htmlparser.jericho.Config) Collectors(java.util.stream.Collectors) LocalVariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo) Attribute(net.htmlparser.jericho.Attribute) List(java.util.List) ParseProblemException(com.github.javaparser.ParseProblemException) Tag(net.htmlparser.jericho.Tag) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) LocalComponentProp(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponentProp) Pattern(java.util.regex.Pattern) TemplateParserContext(com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext) Segment(net.htmlparser.jericho.Segment) JavaParser(com.github.javaparser.JavaParser) Attribute(net.htmlparser.jericho.Attribute) Element(net.htmlparser.jericho.Element) Attributes(net.htmlparser.jericho.Attributes) CharacterReference(net.htmlparser.jericho.CharacterReference) Tag(net.htmlparser.jericho.Tag)

Example 5 with Segment

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

the class QuestPageParser method parseQuestField.

private void parseQuestField(Element questField) {
    List<Element> strongs = questField.getAllElements(HTMLElementName.STRONG);
    if ((strongs != null) && (strongs.size() == 1)) {
        List<Segment> nodes = JerichoHtmlUtils.getChildNodes(questField);
        Element strong = strongs.get(0);
        String key = CharacterReference.decodeCollapseWhiteSpace(strong.getContent());
        key = cleanupFieldName(key);
        if ("Category".equals(key)) {
            String value = nodes.get(3).toString().trim();
            _quest.setCategory(value);
        } else if ("Scope".equals(key)) {
            String value = nodes.get(3).toString().trim();
            if ("n/a".equals(value))
                value = "";
            _quest.setQuestScope(value);
        } else if ("Minimum Level".equals(key)) {
            String value = nodes.get(3).toString().trim();
            Integer minLevel = NumericTools.parseInteger(value);
            _quest.setMinimumLevel(minLevel);
        } else if ("Maximum Level".equals(key)) {
            String value = nodes.get(3).toString().trim();
            Integer maxLevel = NumericTools.parseInteger(value);
            _quest.setMaximumLevel(maxLevel);
        } else if ("Required Classes".equals(key)) {
            List<Element> as = questField.getAllElements(HTMLElementName.A);
            for (Element a : as) {
                String className = CharacterReference.decodeCollapseWhiteSpace(a.getContent());
                _quest.addRequiredClass(className);
            }
        } else if ((PREREQUISITE_QUESTS.equals(key)) || (NEXT_QUESTS.equals(key))) {
            List<Element> dds = questField.getAllElements("dd");
            if ((dds != null) && (dds.size() == 1)) {
                parseQuestsList(dds.get(0), key);
            }
        } else if ("Arc(s)".equals(key)) {
            String value = nodes.get(5).toString().trim();
            _quest.setQuestArc(value);
        } else if ("Required Races".equals(key)) {
            List<Element> as = questField.getAllElements(HTMLElementName.A);
            for (Element a : as) {
                String race = CharacterReference.decodeCollapseWhiteSpace(a.getContent());
                _quest.addRequiredRace(race);
            }
        } else if ("Monster Play Quest".equals(key)) {
            _quest.setFaction(FACTION.MONSTER_PLAY);
        } else {
            _logger.warn("Quest [" + _key + "]. Unmanaged quest field key [" + key + "]");
        }
    } else {
        parseQuestIcons(questField);
    }
}
Also used : Element(net.htmlparser.jericho.Element) ArrayList(java.util.ArrayList) List(java.util.List) Segment(net.htmlparser.jericho.Segment)

Aggregations

Segment (net.htmlparser.jericho.Segment)9 Element (net.htmlparser.jericho.Element)6 Money (delta.games.lotro.common.Money)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Source (net.htmlparser.jericho.Source)2 StartTag (net.htmlparser.jericho.StartTag)2 Prop (com.axellience.vuegwt.core.annotations.component.Prop)1 TemplateParserContext (com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext)1 LocalComponent (com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent)1 LocalComponentProp (com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponentProp)1 TemplateParserLoggerProvider (com.axellience.vuegwt.processors.component.template.parser.jericho.TemplateParserLoggerProvider)1 TemplateExpression (com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression)1 TemplateParserResult (com.axellience.vuegwt.processors.component.template.parser.result.TemplateParserResult)1 LocalVariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo)1 VariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo)1 GeneratorsNameUtil.propNameToAttributeName (com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.propNameToAttributeName)1 GeneratorsUtil.stringTypeToTypeName (com.axellience.vuegwt.processors.utils.GeneratorsUtil.stringTypeToTypeName)1 JavaParser (com.github.javaparser.JavaParser)1 ParseProblemException (com.github.javaparser.ParseProblemException)1