1 26 27 package com.opensugar.cube.ldap; 28 29 import java.util.Dictionary ; 30 import java.util.StringTokenizer ; 31 32 public class SubstringClause extends SimpleClause { 33 34 public static final char WILDCARD = '*'; 35 36 public SubstringClause( String key, String value, boolean keysCaseSensitive ) { 37 super( key, value, keysCaseSensitive ); 38 } 39 40 public boolean filter( Dictionary props ) { 41 Object obj = getProperty( getKey(), props ); 42 if ( obj == null ) { 43 return false; 44 } 45 46 if ( obj instanceof String ) { 47 return isSubstring( (String )obj, getValue() ); 48 } 49 return false; 50 } 51 52 private static boolean isSubstring( String s, String pattern ) { 53 StringTokenizer tokens = new StringTokenizer ( pattern, "" + WILDCARD, true ); 54 String token; 55 int position = 0; 56 while ( tokens.hasMoreTokens() ) { 57 token = tokens.nextToken(); 58 if ( token.equals( "" + WILDCARD ) ) { 59 if ( tokens.hasMoreTokens() ) { 60 token = tokens.nextToken(); 61 } 62 else { 63 return true; 64 } 65 } 66 position = s.indexOf( token, position ); 67 if ( position == -1 ) { 68 return false; 69 } 70 position += token.length(); 71 } 72 if ( position == s.length() ) { 73 return true; 74 } 75 return false; 76 } 77 78 public String getCanonicalForm() { 79 return "(" + getKey() + Filter.EQUALS + getValue() + ")"; 80 } 81 82 } 83 | Popular Tags |