public LDAPExpr parse(String strExpr) throws LDAPParseException { if (strExpr == null || strExpr.trim().length() == 0) { return LDAPExpr.ACCEPT_ALL; } ParseState ps = new ParseState(strExpr); LDAPExpr expr = parseExpr(ps); ps.skipWhitespace(); if (!ps.isEndOfString()) { error("expected end of expression ", ps); } return expr; }
private SimpleTerm parseSimple(ParseState ps) throws LDAPParseException { // read name StringBuffer name = new StringBuffer(16); for (char c = ps.peek(); !ps.isEndOfString() && isNameChar(c); c = ps.peek()) { ps.read(); name.append(c); } ps.skipWhitespace(); Ops op = null; // read op if (ps.lookingAt("=")) { op = EQ; ps.skip(1); } else if (ps.lookingAt(">=")) { op = GE; ps.skip(2); } else if (ps.lookingAt("<=")) { op = LE; ps.skip(2); } else if (ps.lookingAt(">")) { op = GT; ps.skip(1); } else if (ps.lookingAt("<")) { op = LT; ps.skip(1); } else if (ps.lookingAt("-=")) { op = APPROX; ps.skip(2); } else if (ps.isEndOfString()) { error("unexpected end of expression", ps); } else { error("unexpected character: '" + ps.peek() + "'", ps); } ps.skipWhitespace(); boolean escaped = false; StringBuffer value = new StringBuffer(16); while (!ps.isEndOfString() && !Character.isWhitespace(ps.peek()) && !(ps.peek() == ')' && !escaped)) { char ch = ps.peek(); if (ch == '\\') { escaped = true; ps.read(); } else if (ch == '*') { if (escaped) { value.append(ch); escaped = false; } else { value.append(SimpleTerm.WILDCARD); } ps.read(); } else if (isLiteralValue(ch)) { if (escaped) { error("incorrectly applied escape of '" + ch + "'", ps); } value.append(ps.read()); } else if (isEscapedValue(ch)) { if (!escaped) { error("missing escape for '" + ch + "'", ps); } value.append(ps.read()); escaped = false; } else { error("unexpected character: '" + ps.peek() + "'", ps); } } ps.skipWhitespace(); SimpleTerm expr = new SimpleTerm(name.toString(), op, value.toString()); return expr; }