Search in sources :

Example 1 with XPathLexer

use of org.antlr.v4.runtime.tree.xpath.XPathLexer 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 XPathLexer

use of org.antlr.v4.runtime.tree.xpath.XPathLexer 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 3 with XPathLexer

use of org.antlr.v4.runtime.tree.xpath.XPathLexer 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)

Example 4 with XPathLexer

use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project antlr4 by tunnelvisionlabs.

the class XPath method split.

// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
    XPathLexer lexer = new XPathLexer(CharStreams.fromString(path)) {

        @Override
        public void recover(LexerNoViableAltException e) {
            throw e;
        }
    };
    lexer.removeErrorListeners();
    lexer.addErrorListener(new XPathLexerErrorListener());
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException e) {
        int pos = lexer.getCharPositionInLine();
        String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
        throw new IllegalArgumentException(msg, e);
    }
    List<Token> tokens = tokenStream.getTokens();
    // System.out.println("path="+path+"=>"+tokens);
    List<XPathElement> elements = new ArrayList<XPathElement>();
    int n = tokens.size();
    int i = 0;
    loop: while (i < n) {
        Token el = tokens.get(i);
        Token next = null;
        switch(el.getType()) {
            case XPathLexer.ROOT:
            case XPathLexer.ANYWHERE:
                boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
                i++;
                next = tokens.get(i);
                boolean invert = next.getType() == XPathLexer.BANG;
                if (invert) {
                    i++;
                    next = tokens.get(i);
                }
                XPathElement pathElement = getXPathElement(next, anywhere);
                pathElement.invert = invert;
                elements.add(pathElement);
                i++;
                break;
            case XPathLexer.TOKEN_REF:
            case XPathLexer.RULE_REF:
            case XPathLexer.WILDCARD:
                elements.add(getXPathElement(el, false));
                i++;
                break;
            case Token.EOF:
                break loop;
            default:
                throw new IllegalArgumentException("Unknowth path element " + el);
        }
    }
    return elements.toArray(new XPathElement[0]);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token)

Example 5 with XPathLexer

use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project antlr4 by antlr.

the class XPath method split.

// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
    ANTLRInputStream in;
    try {
        in = new ANTLRInputStream(new StringReader(path));
    } catch (IOException ioe) {
        throw new IllegalArgumentException("Could not read path: " + path, ioe);
    }
    XPathLexer lexer = new XPathLexer(in) {

        @Override
        public void recover(LexerNoViableAltException e) {
            throw e;
        }
    };
    lexer.removeErrorListeners();
    lexer.addErrorListener(new XPathLexerErrorListener());
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException e) {
        int pos = lexer.getCharPositionInLine();
        String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
        throw new IllegalArgumentException(msg, e);
    }
    List<Token> tokens = tokenStream.getTokens();
    // System.out.println("path="+path+"=>"+tokens);
    List<XPathElement> elements = new ArrayList<XPathElement>();
    int n = tokens.size();
    int i = 0;
    loop: while (i < n) {
        Token el = tokens.get(i);
        Token next = null;
        switch(el.getType()) {
            case XPathLexer.ROOT:
            case XPathLexer.ANYWHERE:
                boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
                i++;
                next = tokens.get(i);
                boolean invert = next.getType() == XPathLexer.BANG;
                if (invert) {
                    i++;
                    next = tokens.get(i);
                }
                XPathElement pathElement = getXPathElement(next, anywhere);
                pathElement.invert = invert;
                elements.add(pathElement);
                i++;
                break;
            case XPathLexer.TOKEN_REF:
            case XPathLexer.RULE_REF:
            case XPathLexer.WILDCARD:
                elements.add(getXPathElement(el, false));
                i++;
                break;
            case Token.EOF:
                break loop;
            default:
                throw new IllegalArgumentException("Unknowth path element " + el);
        }
    }
    return elements.toArray(new XPathElement[0]);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) IOException(java.io.IOException) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) StringReader(java.io.StringReader) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 CharStream (org.antlr.v4.runtime.CharStream)5 ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 Test (org.junit.Test)3 XpathLexer (cn.wanghaomiao.xpath.antlr.XpathLexer)2 XpathParser (cn.wanghaomiao.xpath.antlr.XpathParser)2 XValue (cn.wanghaomiao.xpath.core.XValue)2 XpathProcessor (cn.wanghaomiao.xpath.core.XpathProcessor)2 DoFailOnErrorHandler (cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 LexerNoViableAltException (org.antlr.v4.runtime.LexerNoViableAltException)2 Token (org.antlr.v4.runtime.Token)2 Element (org.jsoup.nodes.Element)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 BaseTest (cn.wanghaomiao.xpath.BaseTest)1