diff options
Diffstat (limited to 'pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java')
-rw-r--r-- | pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java | 183 |
1 files changed, 113 insertions, 70 deletions
diff --git a/pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java b/pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java index ac91af825..3ac8f7502 100644 --- a/pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java +++ b/pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java @@ -17,6 +17,7 @@ // --- END COPYRIGHT BLOCK --- package com.netscape.cmscore.ldap; + import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; @@ -28,16 +29,19 @@ import com.netscape.certsrv.ldap.ELdapException; import com.netscape.certsrv.publish.ILdapExpression; import com.netscape.cmscore.util.Debug; + /** * Default implementation of predicate parser. - * + * * Limitations: - * - * 1. Currently parentheses are not suported. 2. Only ==, != <, >, <= and >= - * operators are supported. 3. The only boolean operators supported are AND and - * OR. AND takes precedence over OR. Example: a AND b OR e OR c AND d is treated - * as (a AND b) OR e OR (c AND d) 4. If this is n't adequate, roll your own. - * + * + * 1. Currently parentheses are not suported. + * 2. Only ==, != <, >, <= and >= operators are supported. + * 3. The only boolean operators supported are AND and OR. AND takes precedence + * over OR. Example: a AND b OR e OR c AND d + * is treated as (a AND b) OR e OR (c AND d) + * 4. If this is n't adequate, roll your own. + * * @author mzhao * @version $Revision$, $Date$ */ @@ -53,22 +57,22 @@ public class LdapPredicateParser { /** * Parse the predicate expression and return a vector of expressions. - * - * @param predicateExp The predicate expression as read from the config - * file. - * @return expVector The vector of expressions. + * + * @param predicateExp The predicate expression as read from the config file. + * @return expVector The vector of expressions. */ public static ILdapExpression parse(String predicateExpression) - throws ELdapException { - if (predicateExpression == null || predicateExpression.length() == 0) + throws ELdapException { + if (predicateExpression == null || + predicateExpression.length() == 0) return null; PredicateTokenizer pt = new PredicateTokenizer(predicateExpression); if (pt == null || !pt.hasMoreTokens()) return null; - // The first token cannot be an operator. We are not dealing with - // reverse-polish notation. + // The first token cannot be an operator. We are not dealing with + // reverse-polish notation. String token = pt.nextToken(); boolean opANDSeen; boolean opORSeen; @@ -76,8 +80,7 @@ public class LdapPredicateParser { if (getOP(token) != EXPRESSION) { if (Debug.ON) Debug.trace("Malformed expression: " + predicateExpression); - throw new ELdapException(CMS.getUserMessage( - "CMS_LDAP_BAD_LDAP_EXPRESSION", predicateExpression)); + throw new ELdapException(CMS.getUserMessage("CMS_LDAP_BAD_LDAP_EXPRESSION", predicateExpression)); } ILdapExpression current = parseExpression(token); boolean malformed = false; @@ -88,8 +91,8 @@ public class LdapPredicateParser { token = pt.nextToken(); int curType = getOP(token); - if ((prevType != EXPRESSION && curType != EXPRESSION) - || (prevType == EXPRESSION && curType == EXPRESSION)) { + if ((prevType != EXPRESSION && curType != EXPRESSION) || + (prevType == EXPRESSION && curType == EXPRESSION)) { malformed = true; break; } @@ -100,8 +103,7 @@ public class LdapPredicateParser { continue; } - // If the previous type was an OR token, add the current expression - // to + // If the previous type was an OR token, add the current expression to // the expression set; if (prevType == OP_OR) { expSet.addElement(current); @@ -119,8 +121,9 @@ public class LdapPredicateParser { if (malformed) { if (Debug.ON) Debug.trace("Malformed expression: " + predicateExpression); - throw new ELdapException(CMS.getUserMessage( - "CMS_LDAP_BAD_LDAP_EXPRESSION", predicateExpression)); + throw new ELdapException( + CMS.getUserMessage("CMS_LDAP_BAD_LDAP_EXPRESSION", + predicateExpression)); } // Form an LdapOrExpression @@ -131,12 +134,12 @@ public class LdapPredicateParser { if (size == 0) return null; - LdapOrExpression orExp = new LdapOrExpression( - (ILdapExpression) expSet.elementAt(0), null); + LdapOrExpression orExp = new + LdapOrExpression((ILdapExpression) expSet.elementAt(0), null); for (int i = 1; i < size; i++) orExp = new LdapOrExpression(orExp, - (ILdapExpression) expSet.elementAt(i)); + (ILdapExpression) expSet.elementAt(i)); return orExp; } @@ -150,7 +153,7 @@ public class LdapPredicateParser { } private static ILdapExpression parseExpression(String input) - throws ELdapException { + throws ELdapException { // If the expression has multiple parts separated by commas // we need to construct an AND expression. Else we will return a // simple expression. @@ -162,30 +165,28 @@ public class LdapPredicateParser { Vector expVector = new Vector(); while (commaIndex > 0) { - LdapSimpleExpression exp = (LdapSimpleExpression) LdapSimpleExpression - .parse(input.substring(currentIndex, commaIndex)); + LdapSimpleExpression exp = (LdapSimpleExpression) + LdapSimpleExpression.parse(input.substring(currentIndex, + commaIndex)); expVector.addElement(exp); currentIndex = commaIndex + 1; commaIndex = input.indexOf(COMMA, currentIndex); } if (currentIndex < (input.length() - 1)) { - LdapSimpleExpression exp = (LdapSimpleExpression) LdapSimpleExpression - .parse(input.substring(currentIndex)); + LdapSimpleExpression exp = (LdapSimpleExpression) + LdapSimpleExpression.parse(input.substring(currentIndex)); expVector.addElement(exp); } int size = expVector.size(); - LdapSimpleExpression exp1 = (LdapSimpleExpression) expVector - .elementAt(0); - LdapSimpleExpression exp2 = (LdapSimpleExpression) expVector - .elementAt(1); + LdapSimpleExpression exp1 = (LdapSimpleExpression) expVector.elementAt(0); + LdapSimpleExpression exp2 = (LdapSimpleExpression) expVector.elementAt(1); LdapAndExpression andExp = new LdapAndExpression(exp1, exp2); for (int i = 2; i < size; i++) { - andExp = new LdapAndExpression(andExp, - (LdapSimpleExpression) expVector.elementAt(i)); + andExp = new LdapAndExpression(andExp, (LdapSimpleExpression) expVector.elementAt(i)); } return andExp; } @@ -193,40 +194,79 @@ public class LdapPredicateParser { public static void main(String[] args) { /** - * AttributeSet req = new AttributeSet(); try { req.set("ou", "people"); - * req.set("cn", "John Doe"); req.set("uid", "jdoes"); req.set("o", - * "airius.com"); req.set("certtype", "client"); req.set("request", - * "issuance"); req.set("id", new Integer(10)); req.set("dualcerts", new - * Boolean(true)); - * - * Vector v = new Vector(); v.addElement("one"); v.addElement("two"); - * v.addElement("three"); req.set("count", v); } catch (Exception - * e){e.printStackTrace();} String[] array = { - * "ou == people AND certtype == client", - * "ou == servergroup AND certtype == server", - * "uid == jdoes, ou==people, o==airius.com OR ou == people AND certType == client OR certType == server AND cn == needles.mcom.com" - * , }; for (int i = 0; i < array.length; i++) { System.out.println(); - * System.out.println("String: " + array[i]); ILdapExpression exp = - * null; try { exp = parse(array[i]); if (exp != null) { - * System.out.println("Parsed Expression: " + exp); boolean result = - * exp.evaluate(req); System.out.println("Result: " + result); } } catch - * (Exception e) {e.printStackTrace(); } } - * - * - * try { BufferedReader rdr = new BufferedReader( new - * FileReader(args[0])); String line; while((line=rdr.readLine()) != - * null) { System.out.println(); System.out.println("Line Read: " + - * line); ILdapExpression exp = null; try { exp = parse(line); if (exp - * != null) { System.out.println(exp); boolean result = - * exp.evaluate(req); System.out.println("Result: " + result); } - * - * }catch (Exception e){e.printStackTrace();} } } catch (Exception - * e){e.printStackTrace(); } + AttributeSet req = new AttributeSet(); + try + { + req.set("ou", "people"); + req.set("cn", "John Doe"); + req.set("uid", "jdoes"); + req.set("o", "airius.com"); + req.set("certtype", "client"); + req.set("request", "issuance"); + req.set("id", new Integer(10)); + req.set("dualcerts", new Boolean(true)); + + Vector v = new Vector(); + v.addElement("one"); + v.addElement("two"); + v.addElement("three"); + req.set("count", v); + } + catch (Exception e){e.printStackTrace();} + String[] array = { "ou == people AND certtype == client", + "ou == servergroup AND certtype == server", + "uid == jdoes, ou==people, o==airius.com OR ou == people AND certType == client OR certType == server AND cn == needles.mcom.com", + }; + for (int i = 0; i < array.length; i++) + { + System.out.println(); + System.out.println("String: " + array[i]); + ILdapExpression exp = null; + try + { + exp = parse(array[i]); + if (exp != null) + { + System.out.println("Parsed Expression: " + exp); + boolean result = exp.evaluate(req); + System.out.println("Result: " + result); + } + } + catch (Exception e) {e.printStackTrace(); } + } + + + try + { + BufferedReader rdr = new BufferedReader( + new FileReader(args[0])); + String line; + while((line=rdr.readLine()) != null) + { + System.out.println(); + System.out.println("Line Read: " + line); + ILdapExpression exp = null; + try + { + exp = parse(line); + if (exp != null) + { + System.out.println(exp); + boolean result = exp.evaluate(req); + System.out.println("Result: " + result); + } + + }catch (Exception e){e.printStackTrace();} + } + } + catch (Exception e){e.printStackTrace(); } + **/ } } + class PredicateTokenizer { String input; int currentIndex; @@ -308,27 +348,30 @@ class PredicateTokenizer { } } + class AttributeSet implements IAttrSet { /** * */ private static final long serialVersionUID = -3155846653754028803L; Hashtable ht = new Hashtable(); - public AttributeSet() { } - public void delete(String name) throws EBaseException { + public void delete(String name) + throws EBaseException { Object ob = ht.get(name); ht.remove(ob); } - public Object get(String name) throws EBaseException { + public Object get(String name) + throws EBaseException { return ht.get(name); } - public void set(String name, Object ob) throws EBaseException { + public void set(String name, Object ob) + throws EBaseException { ht.put(name, ob); } |