Search in sources :

Example 1 with DoFailOnErrorHandler

use of org.seimicrawler.xpath.exception.DoFailOnErrorHandler in project JsoupXpath by zhegexiaohuozi.

the class JXDocument method selN.

public List<JXNode> selN(String xpath) {
    List<JXNode> finalRes = new LinkedList<>();
    try {
        CharStream input = CharStreams.fromString(xpath);
        XpathLexer lexer = new XpathLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        XpathParser parser = new XpathParser(tokens);
        parser.setErrorHandler(new DoFailOnErrorHandler());
        ParseTree tree = parser.main();
        XpathProcessor processor = new XpathProcessor(elements);
        XValue calRes = processor.visit(tree);
        if (calRes == null) {
            finalRes.add(JXNode.create(""));
            return finalRes;
        }
        if (calRes.isElements()) {
            for (Element el : calRes.asElements()) {
                finalRes.add(JXNode.create(el));
            }
            return finalRes;
        }
        if (calRes.isList()) {
            for (String str : calRes.asList()) {
                finalRes.add(JXNode.create(str));
            }
            return finalRes;
        }
        if (calRes.isString()) {
            finalRes.add(JXNode.create(calRes.asString()));
            return finalRes;
        }
        if (calRes.isNumber()) {
            Class vType = calRes.valType();
            if (vType.isAssignableFrom(Long.class) || vType.isAssignableFrom(Integer.class)) {
                finalRes.add(JXNode.create(calRes.asLong()));
            } else {
                finalRes.add(JXNode.create(calRes.asDouble()));
            }
            return finalRes;
        }
        if (calRes.isBoolean()) {
            finalRes.add(JXNode.create(calRes.asBoolean()));
            return finalRes;
        }
        if (calRes.isDate()) {
            finalRes.add(JXNode.create(calRes.asDate()));
            return finalRes;
        }
        finalRes.add(JXNode.create(calRes.asString()));
    } catch (Exception e) {
        String msg = "Please check the syntax of your xpath expr or commit a Issue. ";
        throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
    }
    return finalRes;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) XpathLexer(org.seimicrawler.xpath.antlr.XpathLexer) Element(org.jsoup.nodes.Element) XValue(org.seimicrawler.xpath.core.XValue) LinkedList(java.util.LinkedList) CharStream(org.antlr.v4.runtime.CharStream) DoFailOnErrorHandler(org.seimicrawler.xpath.exception.DoFailOnErrorHandler) XpathParserException(org.seimicrawler.xpath.exception.XpathParserException) XpathSyntaxErrorException(org.seimicrawler.xpath.exception.XpathSyntaxErrorException) XpathParser(org.seimicrawler.xpath.antlr.XpathParser) XpathSyntaxErrorException(org.seimicrawler.xpath.exception.XpathSyntaxErrorException) XpathProcessor(org.seimicrawler.xpath.core.XpathProcessor) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with DoFailOnErrorHandler

use of org.seimicrawler.xpath.exception.DoFailOnErrorHandler in project JsoupXpath by zhegexiaohuozi.

the class ExprTest method exp.

@Test
public void exp() {
    String xpath = "//a[@id]/@href";
    CharStream input = CharStreams.fromString(xpath);
    XpathLexer lexer = new XpathLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    XpathParser parser = new XpathParser(tokens);
    parser.setErrorHandler(new DoFailOnErrorHandler());
    ParseTree tree = parser.main();
    XpathProcessor processor = new XpathProcessor(root);
    XValue value = processor.visit(tree);
    logger.info("visit res = {}", value);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) XpathParser(org.seimicrawler.xpath.antlr.XpathParser) XpathLexer(org.seimicrawler.xpath.antlr.XpathLexer) XpathProcessor(org.seimicrawler.xpath.core.XpathProcessor) XValue(org.seimicrawler.xpath.core.XValue) CharStream(org.antlr.v4.runtime.CharStream) DoFailOnErrorHandler(org.seimicrawler.xpath.exception.DoFailOnErrorHandler) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test) BaseTest(org.seimicrawler.xpath.BaseTest)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 XpathLexer (org.seimicrawler.xpath.antlr.XpathLexer)2 XpathParser (org.seimicrawler.xpath.antlr.XpathParser)2 XValue (org.seimicrawler.xpath.core.XValue)2 XpathProcessor (org.seimicrawler.xpath.core.XpathProcessor)2 DoFailOnErrorHandler (org.seimicrawler.xpath.exception.DoFailOnErrorHandler)2 LinkedList (java.util.LinkedList)1 Element (org.jsoup.nodes.Element)1 Test (org.junit.Test)1 BaseTest (org.seimicrawler.xpath.BaseTest)1 XpathParserException (org.seimicrawler.xpath.exception.XpathParserException)1 XpathSyntaxErrorException (org.seimicrawler.xpath.exception.XpathSyntaxErrorException)1