public synchronized boolean isIdentifier(String text, Project project) { xPathLexer.start(text); assert xPathLexer.getState() == 0; boolean b = xPathLexer.getTokenType() == XPathTokenTypes.NCNAME; xPathLexer.advance(); if (xPathLexer.getTokenType() == null) { return b; } else if (xPathLexer.getTokenType() == XPathTokenTypes.COL) { xPathLexer.advance(); b = xPathLexer.getTokenType() == XPathTokenTypes.NCNAME; xPathLexer.advance(); return b && xPathLexer.getTokenType() == null; } return false; }
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]); }