src/share/classes/com/sun/jndi/toolkit/dir/SearchFilter.java

Print this page




 186 
 187     protected void consumeChars(int i) {
 188         pos += i;
 189     }
 190 
 191     protected int relIndexOf(int ch) {
 192         return filter.indexOf(ch, pos) - pos;
 193     }
 194 
 195     protected String relSubstring(int beginIndex, int endIndex){
 196         if(debug){System.out.println("relSubString: " + beginIndex +
 197                                      " " + endIndex);}
 198         return filter.substring(beginIndex+pos, endIndex+pos);
 199     }
 200 
 201 
 202    /**
 203      * A class for dealing with compound filters ("and" & "or" filters).
 204      */
 205     final class CompoundFilter implements StringFilter {
 206         private Vector  subFilters;
 207         private boolean polarity;
 208 
 209         CompoundFilter(boolean polarity) {
 210             subFilters = new Vector();
 211             this.polarity = polarity;
 212         }
 213 
 214         public void parse() throws InvalidSearchFilterException {
 215             SearchFilter.this.consumeChar(); // consume the "&"
 216             while(SearchFilter.this.getCurrentChar() != END_FILTER_TOKEN) {
 217                 if (debug) {System.out.println("CompoundFilter: adding");}
 218                 StringFilter filter = SearchFilter.this.createNextFilter();
 219                 subFilters.addElement(filter);
 220                 skipWhiteSpace();
 221             }
 222         }
 223 
 224         public boolean check(Attributes targetAttrs) throws NamingException {
 225             for(int i = 0; i<subFilters.size(); i++) {
 226                 StringFilter filter = (StringFilter)subFilters.elementAt(i);
 227                 if(filter.check(targetAttrs) != this.polarity) {
 228                     return !polarity;
 229                 }
 230             }
 231             return polarity;
 232         }
 233     } /* CompoundFilter */
 234 
 235    /**
 236      * A class for dealing with NOT filters
 237      */
 238     final class NotFilter implements StringFilter {
 239         private StringFilter    filter;
 240 
 241         public void parse() throws InvalidSearchFilterException {
 242             SearchFilter.this.consumeChar(); // consume the "!"
 243             filter = SearchFilter.this.createNextFilter();
 244         }
 245 
 246         public boolean check(Attributes targetAttrs) throws NamingException {


 313 
 314                 //update our position
 315                 SearchFilter.this.consumeChars(endPos);
 316 
 317             } catch (Exception e) {
 318                 if (debug) {System.out.println(e.getMessage());
 319                             e.printStackTrace();}
 320                 InvalidSearchFilterException sfe =
 321                     new InvalidSearchFilterException("Unable to parse " +
 322                     "character " + SearchFilter.this.pos + " in \""+
 323                     SearchFilter.this.filter + "\"");
 324                 sfe.setRootCause(e);
 325                 throw(sfe);
 326             }
 327 
 328             if(debug) {System.out.println("AtomicFilter: " + attrID + "=" +
 329                                           value);}
 330         }
 331 
 332         public boolean check(Attributes targetAttrs) {
 333             Enumeration candidates;
 334 
 335             try {
 336                 Attribute attr = targetAttrs.get(attrID);
 337                 if(attr == null) {
 338                     return false;
 339                 }
 340                 candidates = attr.getAll();
 341             } catch (NamingException ne) {
 342                 if (debug) {System.out.println("AtomicFilter: should never " +
 343                                                "here");}
 344                 return false;
 345             }
 346 
 347             while(candidates.hasMoreElements()) {
 348                 String val = candidates.nextElement().toString();
 349                 if (debug) {System.out.println("Atomic: comparing: " + val);}
 350                 switch(matchType) {
 351                 case APPROX_MATCH:
 352                 case EQUAL_MATCH:
 353                     if(substringMatch(this.value, val)) {


 424             return true;
 425         }
 426 
 427     } /* AtomicFilter */
 428 
 429     // ----- static methods for producing string filters given attribute set
 430     // ----- or object array
 431 
 432 
 433     /**
 434       * Creates an LDAP filter as a conjuction of the attributes supplied.
 435       */
 436     public static String format(Attributes attrs) throws NamingException {
 437         if (attrs == null || attrs.size() == 0) {
 438             return "objectClass=*";
 439         }
 440 
 441         String answer;
 442         answer = "(& ";
 443         Attribute attr;
 444         for (NamingEnumeration e = attrs.getAll(); e.hasMore(); ) {
 445             attr = (Attribute)e.next();

 446             if (attr.size() == 0 || (attr.size() == 1 && attr.get() == null)) {
 447                 // only checking presence of attribute
 448                 answer += "(" + attr.getID() + "=" + "*)";
 449             } else {
 450                 for (NamingEnumeration ve = attr.getAll();
 451                      ve.hasMore();
 452                         ) {
 453                     String val = getEncodedStringRep(ve.next());
 454                     if (val != null) {
 455                         answer += "(" + attr.getID() + "=" + val + ")";
 456                     }
 457                 }
 458             }
 459         }
 460 
 461         answer += ")";
 462         //System.out.println("filter: " + answer);
 463         return answer;
 464     }
 465 
 466     // Writes the hex representation of a byte to a StringBuffer.
 467     private static void hexDigit(StringBuffer buf, byte x) {
 468         char c;
 469 
 470         c = (char) ((x >> 4) & 0xf);




 186 
 187     protected void consumeChars(int i) {
 188         pos += i;
 189     }
 190 
 191     protected int relIndexOf(int ch) {
 192         return filter.indexOf(ch, pos) - pos;
 193     }
 194 
 195     protected String relSubstring(int beginIndex, int endIndex){
 196         if(debug){System.out.println("relSubString: " + beginIndex +
 197                                      " " + endIndex);}
 198         return filter.substring(beginIndex+pos, endIndex+pos);
 199     }
 200 
 201 
 202    /**
 203      * A class for dealing with compound filters ("and" & "or" filters).
 204      */
 205     final class CompoundFilter implements StringFilter {
 206         private Vector<StringFilter>  subFilters;
 207         private boolean polarity;
 208 
 209         CompoundFilter(boolean polarity) {
 210             subFilters = new Vector<>();
 211             this.polarity = polarity;
 212         }
 213 
 214         public void parse() throws InvalidSearchFilterException {
 215             SearchFilter.this.consumeChar(); // consume the "&"
 216             while(SearchFilter.this.getCurrentChar() != END_FILTER_TOKEN) {
 217                 if (debug) {System.out.println("CompoundFilter: adding");}
 218                 StringFilter filter = SearchFilter.this.createNextFilter();
 219                 subFilters.addElement(filter);
 220                 skipWhiteSpace();
 221             }
 222         }
 223 
 224         public boolean check(Attributes targetAttrs) throws NamingException {
 225             for(int i = 0; i<subFilters.size(); i++) {
 226                 StringFilter filter = subFilters.elementAt(i);
 227                 if(filter.check(targetAttrs) != this.polarity) {
 228                     return !polarity;
 229                 }
 230             }
 231             return polarity;
 232         }
 233     } /* CompoundFilter */
 234 
 235    /**
 236      * A class for dealing with NOT filters
 237      */
 238     final class NotFilter implements StringFilter {
 239         private StringFilter    filter;
 240 
 241         public void parse() throws InvalidSearchFilterException {
 242             SearchFilter.this.consumeChar(); // consume the "!"
 243             filter = SearchFilter.this.createNextFilter();
 244         }
 245 
 246         public boolean check(Attributes targetAttrs) throws NamingException {


 313 
 314                 //update our position
 315                 SearchFilter.this.consumeChars(endPos);
 316 
 317             } catch (Exception e) {
 318                 if (debug) {System.out.println(e.getMessage());
 319                             e.printStackTrace();}
 320                 InvalidSearchFilterException sfe =
 321                     new InvalidSearchFilterException("Unable to parse " +
 322                     "character " + SearchFilter.this.pos + " in \""+
 323                     SearchFilter.this.filter + "\"");
 324                 sfe.setRootCause(e);
 325                 throw(sfe);
 326             }
 327 
 328             if(debug) {System.out.println("AtomicFilter: " + attrID + "=" +
 329                                           value);}
 330         }
 331 
 332         public boolean check(Attributes targetAttrs) {
 333             Enumeration<?> candidates;
 334 
 335             try {
 336                 Attribute attr = targetAttrs.get(attrID);
 337                 if(attr == null) {
 338                     return false;
 339                 }
 340                 candidates = attr.getAll();
 341             } catch (NamingException ne) {
 342                 if (debug) {System.out.println("AtomicFilter: should never " +
 343                                                "here");}
 344                 return false;
 345             }
 346 
 347             while(candidates.hasMoreElements()) {
 348                 String val = candidates.nextElement().toString();
 349                 if (debug) {System.out.println("Atomic: comparing: " + val);}
 350                 switch(matchType) {
 351                 case APPROX_MATCH:
 352                 case EQUAL_MATCH:
 353                     if(substringMatch(this.value, val)) {


 424             return true;
 425         }
 426 
 427     } /* AtomicFilter */
 428 
 429     // ----- static methods for producing string filters given attribute set
 430     // ----- or object array
 431 
 432 
 433     /**
 434       * Creates an LDAP filter as a conjuction of the attributes supplied.
 435       */
 436     public static String format(Attributes attrs) throws NamingException {
 437         if (attrs == null || attrs.size() == 0) {
 438             return "objectClass=*";
 439         }
 440 
 441         String answer;
 442         answer = "(& ";
 443         Attribute attr;
 444         for (NamingEnumeration<? extends Attribute> e = attrs.getAll();
 445              e.hasMore(); ) {
 446             attr = e.next();
 447             if (attr.size() == 0 || (attr.size() == 1 && attr.get() == null)) {
 448                 // only checking presence of attribute
 449                 answer += "(" + attr.getID() + "=" + "*)";
 450             } else {
 451                 for (NamingEnumeration<?> ve = attr.getAll();
 452                      ve.hasMore();
 453                         ) {
 454                     String val = getEncodedStringRep(ve.next());
 455                     if (val != null) {
 456                         answer += "(" + attr.getID() + "=" + val + ")";
 457                     }
 458                 }
 459             }
 460         }
 461 
 462         answer += ")";
 463         //System.out.println("filter: " + answer);
 464         return answer;
 465     }
 466 
 467     // Writes the hex representation of a byte to a StringBuffer.
 468     private static void hexDigit(StringBuffer buf, byte x) {
 469         char c;
 470 
 471         c = (char) ((x >> 4) & 0xf);