diff options
author | Ade Lee <alee@redhat.com> | 2011-12-07 16:58:12 -0500 |
---|---|---|
committer | Ade Lee <alee@redhat.com> | 2011-12-07 16:58:12 -0500 |
commit | 32150d3ee32f8ac27118af7c792794b538c78a2f (patch) | |
tree | 52dd96f664a6fa51be25b28b6f10adc5f2c9f660 /pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java | |
parent | f05d58a46795553beb8881039cc922974b40db34 (diff) | |
download | pki-32150d3ee32f8ac27118af7c792794b538c78a2f.tar.gz pki-32150d3ee32f8ac27118af7c792794b538c78a2f.tar.xz pki-32150d3ee32f8ac27118af7c792794b538c78a2f.zip |
Formatting
Formatted project according to eclipse project settings
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, 70 insertions, 113 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 3ac8f7502..ac91af825 100644 --- a/pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java +++ b/pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java @@ -17,7 +17,6 @@ // --- END COPYRIGHT BLOCK --- package com.netscape.cmscore.ldap; - import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; @@ -29,19 +28,16 @@ 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$ */ @@ -57,22 +53,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; @@ -80,7 +76,8 @@ 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; @@ -91,8 +88,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; } @@ -103,7 +100,8 @@ 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); @@ -121,9 +119,8 @@ 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 @@ -134,12 +131,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; } @@ -153,7 +150,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. @@ -165,28 +162,30 @@ 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; } @@ -194,79 +193,40 @@ 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; @@ -348,30 +308,27 @@ 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); } |