public XPath(Parser parser, String path) { this.parser = parser; this.path = path; elements = split(path); // System.out.println(Arrays.toString(elements)); }
case XPathLexer.WILDCARD : return anywhere ? new XPathWildcardAnywhereElement() : new XPathWildcardElement(); case XPathLexer.TOKEN_REF : case XPathLexer.STRING : new XPathTokenAnywhereElement(word, ttype) : new XPathTokenElement(word, ttype); default : if ( ruleIndex==-1 ) { new XPathRuleAnywhereElement(word, ruleIndex) : new XPathRuleElement(word, ruleIndex);
public void recover(LexerNoViableAltException e) { throw e; } }; lexer.removeErrorListeners(); lexer.addErrorListener(new XPathLexerErrorListener()); CommonTokenStream tokenStream = new CommonTokenStream(lexer); try { int pos = lexer.getCharPositionInLine(); String msg = "Invalid tokens or characters at index "+pos+" in path '"+path+"'"; throw new IllegalArgumentException(msg, e); next = tokens.get(i); XPathElement pathElement = getXPathElement(next, anywhere); pathElement.invert = invert; elements.add(pathElement); case XPathLexer.RULE_REF : case XPathLexer.WILDCARD : elements.add( getXPathElement(el, false) ); i++; break;
switch ( _input.LA(1) ) { case '/': consume(); if ( _input.LA(1)=='/' ) { consume(); t = new CommonToken(ANYWHERE, "//"); consume(); t = new CommonToken(WILDCARD, "*"); break; case '!': consume(); t = new CommonToken(BANG, "!"); break; case '\'': String s = matchString(); t = new CommonToken(STRING, s); break; return new CommonToken(EOF, "<EOF>"); default: if ( isNameStartChar(_input.LA(1)) ) { String id = matchID(); if ( Character.isUpperCase(id.charAt(0)) ) t = new CommonToken(TOKEN_REF, id); else t = new CommonToken(RULE_REF, id);
/** * Find all nodes using XPath and then try to match those subtrees against * this tree pattern. * * @param tree The {@link ParseTree} to match against this pattern. * @param xpath An expression matching the nodes * * @return A collection of {@link ParseTreeMatch} objects describing the * successful matches. Unsuccessful matches are omitted from the result, * regardless of the reason for the failure. */ public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) { Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser()); List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>(); for (ParseTree t : subtrees) { ParseTreeMatch match = match(t); if ( match.succeeded() ) { matches.add(match); } } return matches; }
/** * Return a list of all nodes starting at {@code t} as root that satisfy the * path. The root {@code /} is relative to the node passed to * {@link #evaluate}. */ public Collection<ParseTree> evaluate(final ParseTree t) { ParserRuleContext dummyRoot = new ParserRuleContext(); dummyRoot.children = Collections.singletonList(t); // don't set t's parent. Collection<ParseTree> work = Collections.<ParseTree>singleton(dummyRoot); int i = 0; while ( i < elements.length ) { Collection<ParseTree> next = new LinkedHashSet<ParseTree>(); for (ParseTree node : work) { if ( node.getChildCount()>0 ) { // only try to match next element if it has children // e.g., //func/*/stat might have a token node for which // we can't go looking for stat nodes. Collection<? extends ParseTree> matching = elements[i].evaluate(node); next.addAll(matching); } } i++; work = next; } return work; } }
public String matchID() { int start = _input.index(); consume(); // drop start char while ( isNameChar(_input.LA(1)) ) { consume(); } return _input.getText(Interval.of(start,_input.index()-1)); }
@Override public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { switch (ruleIndex) { case 4: ID_action((RuleContext)_localctx, actionIndex); break; } } private void ID_action(RuleContext _localctx, int actionIndex) {
@Override public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { case 5: return NameChar_sempred(_localctx, predIndex); case 6: return NameStartChar_sempred(_localctx, predIndex); } return true; } private boolean NameChar_sempred(RuleContext _localctx, int predIndex) {
public XPath(Parser parser, String path) { this.parser = parser; this.path = path; elements = split(path); // System.out.println(Arrays.toString(elements)); }
@Override public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { switch (ruleIndex) { case 4: ID_action((RuleContext)_localctx, actionIndex); break; } } private void ID_action(RuleContext _localctx, int actionIndex) {
public XPath(Parser parser, String path) { this.parser = parser; this.path = path; elements = split(path); // System.out.println(Arrays.toString(elements)); }
@Override public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { switch (ruleIndex) { case 4: ID_action(_localctx, actionIndex); break; } } private void ID_action(RuleContext _localctx, int actionIndex) {
public XPath(Parser parser, String path) { this.parser = parser; this.path = path; elements = split(path); // System.out.println(Arrays.toString(elements)); }
public XPath(Parser parser, String path) { this.parser = parser; this.path = path; elements = split(path); // System.out.println(Arrays.toString(elements)); }