Search in sources :

Example 1 with XValue

use of cn.wanghaomiao.xpath.core.XValue in project JsoupXpath by zhegexiaohuozi.

the class JXDocument method selN.

public List<JXNode> selN(String xpath) throws XpathSyntaxErrorException {
    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.isElements()) {
            for (Element el : calRes.asElements()) {
                finalRes.add(JXNode.e(el));
            }
        } else if (calRes.isList()) {
            for (String str : calRes.asList()) {
                finalRes.add(JXNode.t(str));
            }
        }
    } catch (Exception e) {
        String msg = "Please check the syntax of your xpath expr, ";
        throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
    }
    return finalRes;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) XpathLexer(cn.wanghaomiao.xpath.antlr.XpathLexer) Element(org.jsoup.nodes.Element) XValue(cn.wanghaomiao.xpath.core.XValue) LinkedList(java.util.LinkedList) CharStream(org.antlr.v4.runtime.CharStream) DoFailOnErrorHandler(cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler) XpathSyntaxErrorException(cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException) XpathParser(cn.wanghaomiao.xpath.antlr.XpathParser) XpathSyntaxErrorException(cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException) XpathProcessor(cn.wanghaomiao.xpath.core.XpathProcessor) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with XValue

use of cn.wanghaomiao.xpath.core.XValue in project JsoupXpath by zhegexiaohuozi.

the class ExprTest method exp.

@Test
public void exp() {
    // CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[./div/div/span[@class='pl']/num()>10000]/div[@class='info']/h2/allText()");
    // CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[contains(self::li/div/div/span[@class='pl']//text(),'14582')]/div/h2//text()");
    CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[contains(./div/div/span[@class='pl']//text(),'14582')]/div/h2//text()");
    // CharStream input = CharStreams.fromString("//*[@id=\"subject_list\"]/ul/li[2]/div[2]/h2/a//text()");
    // CharStream input = CharStreams.fromString("//*[contains(@id,\"subject_\")]/ul/li[2]/div[2]/h2/a//text()");
    // CharStream input = CharStreams.fromString("//*[contains(@id,\"subject_\")]/ul/li[2]/div[2]/h2/a//text()");
    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(cn.wanghaomiao.xpath.antlr.XpathParser) XpathLexer(cn.wanghaomiao.xpath.antlr.XpathLexer) XpathProcessor(cn.wanghaomiao.xpath.core.XpathProcessor) XValue(cn.wanghaomiao.xpath.core.XValue) CharStream(org.antlr.v4.runtime.CharStream) DoFailOnErrorHandler(cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test) BaseTest(cn.wanghaomiao.xpath.BaseTest)

Example 3 with XValue

use of cn.wanghaomiao.xpath.core.XValue in project JsoupXpath by zhegexiaohuozi.

the class NumTest method testCall.

/**
 * Method: call(Elements context)
 */
@Test
public void testCall() throws Exception {
    Elements context = new Elements();
    Element el = new Element("V");
    el.appendText("test 33.69");
    context.add(el);
    Num n = new Num();
    XValue v = n.call(Scope.create(context));
    logger.info("v = {}", v);
    Assert.assertEquals(33.69, v.asDouble(), 0.00000000000001);
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) XValue(cn.wanghaomiao.xpath.core.XValue) Test(org.junit.Test) BaseTest(cn.wanghaomiao.xpath.BaseTest)

Example 4 with XValue

use of cn.wanghaomiao.xpath.core.XValue in project JsoupXpath by zhegexiaohuozi.

the class NumTest method testOnZero.

@Test
public void testOnZero() throws Exception {
    Elements context = new Elements();
    Element el = new Element("V");
    el.appendText("test 69.");
    context.add(el);
    Num n = new Num();
    XValue v = n.call(Scope.create(context));
    logger.info("v = {}", v);
    Assert.assertEquals(69, v.asDouble(), 0.00000000000001);
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) XValue(cn.wanghaomiao.xpath.core.XValue) Test(org.junit.Test) BaseTest(cn.wanghaomiao.xpath.BaseTest)

Example 5 with XValue

use of cn.wanghaomiao.xpath.core.XValue in project JsoupXpath by zhegexiaohuozi.

the class NumTest method testShort.

@Test
public void testShort() throws Exception {
    Elements context = new Elements();
    Element el = new Element("V");
    el.appendText("test .69");
    context.add(el);
    Num n = new Num();
    XValue v = n.call(Scope.create(context));
    logger.info("v = {}", v);
    Assert.assertEquals(0.69, v.asDouble(), 0.00000000000001);
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) XValue(cn.wanghaomiao.xpath.core.XValue) Test(org.junit.Test) BaseTest(cn.wanghaomiao.xpath.BaseTest)

Aggregations

XValue (cn.wanghaomiao.xpath.core.XValue)6 BaseTest (cn.wanghaomiao.xpath.BaseTest)4 Element (org.jsoup.nodes.Element)4 Test (org.junit.Test)4 Elements (org.jsoup.select.Elements)3 XpathLexer (cn.wanghaomiao.xpath.antlr.XpathLexer)2 XpathParser (cn.wanghaomiao.xpath.antlr.XpathParser)2 XpathProcessor (cn.wanghaomiao.xpath.core.XpathProcessor)2 DoFailOnErrorHandler (cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler)2 CharStream (org.antlr.v4.runtime.CharStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 NodeTest (cn.wanghaomiao.xpath.core.NodeTest)1 XpathSyntaxErrorException (cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException)1 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1