/* Generated By:JavaCC: Do not edit this line. LdapFilter.java */ package org.ant4eclipse.lib.core.ldapfilter; import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.LinkedList; import java.util.Iterator; import java.util.regex.Pattern; /** * <p> * Implements a filter for LDAP strings. Usage: * <code>new LdapFilter(map, System.in).validate()</code > * </p> * * @author Gerd Wütherich (gerd@gerd-wuetherich.de) */ public class LdapFilter implements LdapFilterConstants { /** the (scoped) properties **/ private Map _properties = new HashMap(); /** * <p> * Creates a new instance of type {@link LdapFilter}. * </p> * * @param properties the (scoped) properties * @param stream the input stream */ public LdapFilter(Map properties, java.io.InputStream stream) { this (stream); // assert properies != null if (properties == null) { throw new RuntimeException(); } // set properties _properties = properties; } public LdapFilter(Map properties, java.io.Reader stream) { this (stream); // assert properies != null if (properties == null) { throw new RuntimeException(); } // set properties _properties = properties; } private boolean equals(String attributeKey, String value) { if (!_properties.containsKey(attributeKey)) { return false; } if (isPresentValue(value)) { return true; } String scopedProperty = (String) _properties.get(attributeKey); String regExpValue = value.replaceAll("\u005c\u005c*", ".*"); boolean result = Pattern.matches(regExpValue, scopedProperty); return result; } private boolean approx(String attributeKey, String value) throws ParseException { denyWildcardValue(value); return equals(attributeKey, value); } private boolean greater(String attributeKey, String value) throws ParseException { denyWildcardValue(value); return true; } private boolean less(String attributeKey, String value) throws ParseException { denyWildcardValue(value); return true; } private void denyWildcardValue(String value) throws ParseException { if (value.indexOf('*') != - 1) { throw new ParseException("Invalid filter definition."); } } private boolean isPresentValue(String value) { if ("*".equals(value)) { return true; } return false; } public static void main(String args []) throws ParseException { Map map = new HashMap(); map.put("papa", "gerd"); map.put("tochter1", "liena"); map.put("tochter2", "annie"); new LdapFilter(map, System.in).validate(); } final public boolean validate() throws ParseException { boolean value; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case BRACKET_OPEN: value = filter(); break; case AND: case AND_ORG: case OR: case OR_ORG: case NOT: case NOT_ORG: case STRING: value = filtercomp(); break; default: jj_la1[0] = jj_gen; jj_consume_token(-1); throw new ParseException(); } jj_consume_token(0); {if (true) return value;} throw new Error("Missing return statement in function"); } /** * filter = "(" filtercomp ")" */ final public boolean filter() throws ParseException { boolean value; jj_consume_token(BRACKET_OPEN); value = filtercomp(); jj_consume_token(BRACKET_CLOSE); {if (true) return value;} throw new Error("Missing return statement in function"); } /** * filtercomp = and / or / not / attributeValueComparison */ final public boolean filtercomp() throws ParseException { boolean value; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case AND: case AND_ORG: value = and(); break; case OR: case OR_ORG: value = or(); break; case NOT: case NOT_ORG: value = not(); break; case STRING: value = attributeValueComparison(); break; default: jj_la1[1] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return value;} throw new Error("Missing return statement in function"); } /** * simple = attr filtertype value * filtertype = equal / approx / greater / less * equal = "=" * approx = "~=" * greater = ">=" * less = "<=" */ final public boolean attributeValueComparison() throws ParseException { Token attribute = null; Token value = null; Token operator = null; attribute = jj_consume_token(STRING); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case EQUAL: operator = jj_consume_token(EQUAL); break; case APPROX: operator = jj_consume_token(APPROX); break; case GREATER: operator = jj_consume_token(GREATER); break; case LESS: operator = jj_consume_token(LESS); break; default: jj_la1[2] = jj_gen; jj_consume_token(-1); throw new ParseException(); } switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case STRING: value = jj_consume_token(STRING); break; case WILDCARD_STRING: value = jj_consume_token(WILDCARD_STRING); break; default: jj_la1[3] = jj_gen; jj_consume_token(-1); throw new ParseException(); } switch (operator.kind) { case LdapFilterConstants.EQUAL : { {if (true) return equals(attribute.image, value.image);} } case LdapFilterConstants.APPROX : { {if (true) return approx(attribute.image, value.image);} } case LdapFilterConstants.GREATER : { {if (true) return greater(attribute.image, value.image);} } case LdapFilterConstants.LESS : { {if (true) return less(attribute.image, value.image);} } default : {if (true) return false;} } throw new Error("Missing return statement in function"); } /** * not = "!" filter */ final public boolean not() throws ParseException { boolean filterresult; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case NOT: jj_consume_token(NOT); break; case NOT_ORG: jj_consume_token(NOT_ORG); break; default: jj_la1[4] = jj_gen; jj_consume_token(-1); throw new ParseException(); } filterresult = filter(); {if (true) return !filterresult;} throw new Error("Missing return statement in function"); } /** * and = "&" filterlist */ final public boolean and() throws ParseException { List values; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case AND: jj_consume_token(AND); break; case AND_ORG: jj_consume_token(AND_ORG); break; default: jj_la1[5] = jj_gen; jj_consume_token(-1); throw new ParseException(); } values = filterlist(); for (Iterator iterator = values.iterator(); iterator.hasNext(); ) { Boolean value = (Boolean) iterator.next(); if (value.booleanValue() == false) { {if (true) return false;} } } {if (true) return true;} throw new Error("Missing return statement in function"); } /** * or = "|" filterlist */ final public boolean or() throws ParseException { List values; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case OR: jj_consume_token(OR); break; case OR_ORG: jj_consume_token(OR_ORG); break; default: jj_la1[6] = jj_gen; jj_consume_token(-1); throw new ParseException(); } values = filterlist(); for (Iterator iterator = values.iterator(); iterator.hasNext(); ) { if (((Boolean) iterator.next()).booleanValue()) { {if (true) return true;} } } {if (true) return false;} throw new Error("Missing return statement in function"); } /** * filterlist = 1*filter */ final public List filterlist() throws ParseException { List values = new LinkedList(); boolean value; value = filter(); values.add(Boolean.valueOf(value)); label_1: while (true) { value = filter(); values.add(Boolean.valueOf(value)); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case BRACKET_OPEN: ; break; default: jj_la1[7] = jj_gen; break label_1; } } {if (true) return values;} throw new Error("Missing return statement in function"); } /** Generated Token Manager. */ public LdapFilterTokenManager token_source; SimpleCharStream jj_input_stream; /** Current token. */ public Token token; /** Next token. */ public Token jj_nt; private int jj_ntk; private int jj_gen; final private int[] jj_la1 = new int[8]; static private int[] jj_la1_0; static { jj_la1_init_0(); } private static void jj_la1_init_0() { jj_la1_0 = new int[] {0x17f00,0x13f00,0xf0,0x30000,0x3000,0x300,0xc00,0x4000,}; } /** Constructor with InputStream. */ public LdapFilter(java.io.InputStream stream) { this(stream, null); } /** Constructor with InputStream and supplied encoding */ public LdapFilter(java.io.InputStream stream, String encoding) { try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source = new LdapFilterTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 8; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.InputStream stream) { ReInit(stream, null); } /** Reinitialise. */ public void ReInit(java.io.InputStream stream, String encoding) { try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 8; i++) jj_la1[i] = -1; } /** Constructor. */ public LdapFilter(java.io.Reader stream) { jj_input_stream = new SimpleCharStream(stream, 1, 1); token_source = new LdapFilterTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 8; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.Reader stream) { jj_input_stream.ReInit(stream, 1, 1); token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 8; i++) jj_la1[i] = -1; } /** Constructor with generated Token Manager. */ public LdapFilter(LdapFilterTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 8; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(LdapFilterTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 8; i++) jj_la1[i] = -1; } private Token jj_consume_token(int kind) throws ParseException { Token oldToken; if ((oldToken = token).next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; if (token.kind == kind) { jj_gen++; return token; } token = oldToken; jj_kind = kind; throw generateParseException(); } /** Get the next Token. */ final public Token getNextToken() { if (token.next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; jj_gen++; return token; } /** Get the specific Token. */ final public Token getToken(int index) { Token t = token; for (int i = 0; i < index; i++) { if (t.next != null) t = t.next; else t = t.next = token_source.getNextToken(); } return t; } private int jj_ntk() { if ((jj_nt=token.next) == null) return (jj_ntk = (token.next=token_source.getNextToken()).kind); else return (jj_ntk = jj_nt.kind); } private java.util.List jj_expentries = new java.util.ArrayList(); private int[] jj_expentry; private int jj_kind = -1; /** Generate ParseException. */ public ParseException generateParseException() { jj_expentries.clear(); boolean[] la1tokens = new boolean[18]; if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } for (int i = 0; i < 8; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1<<j)) != 0) { la1tokens[j] = true; } } } } for (int i = 0; i < 18; i++) { if (la1tokens[i]) { jj_expentry = new int[1]; jj_expentry[0] = i; jj_expentries.add(jj_expentry); } } int[][] exptokseq = new int[jj_expentries.size()][]; for (int i = 0; i < jj_expentries.size(); i++) { exptokseq[i] = (int[])jj_expentries.get(i); } return new ParseException(token, exptokseq, tokenImage); } /** Enable tracing. */ final public void enable_tracing() { } /** Disable tracing. */ final public void disable_tracing() { } }