KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > ldap > ApproxClause


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube.ldap;
28
29 import java.util.Dictionary JavaDoc;
30
31 // checks string proximity (case-insensitive, ignores white spaces)
32
public class ApproxClause extends SimpleClause {
33
34    public ApproxClause( String JavaDoc key, String JavaDoc value, boolean keysCaseSensitive ) {
35       super( key, value, keysCaseSensitive );
36    }
37
38    public boolean filter( Dictionary JavaDoc props ) {
39       Object JavaDoc obj = getProperty( getKey(), props );
40       if ( obj == null ) {
41          return false;
42       }
43
44       if ( obj instanceof String JavaDoc ) {
45          return removeWhiteSpace( (String JavaDoc)obj ).toLowerCase().equals( removeWhiteSpace( getValue() ).toLowerCase() );
46       }
47       else if ( obj instanceof String JavaDoc[] ) {
48          String JavaDoc[] s = (String JavaDoc[])obj;
49          for ( int i = 0; i < s.length; i++ ) {
50             if ( s[ i ] != null && removeWhiteSpace( s[ i ] ).toLowerCase().equals( removeWhiteSpace( getValue() ).toLowerCase() ) ) {
51                return true;
52             }
53          }
54       }
55       return false;
56    }
57
58    private static String JavaDoc removeWhiteSpace( String JavaDoc s ) {
59       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60       char c;
61       for ( int i = 0; i < s.length(); i++ ) {
62          c = s.charAt( i );
63          if ( !Character.isWhitespace( c ) ) {
64             sb.append( c );
65          }
66       }
67       return sb.toString();
68    }
69
70    public String JavaDoc getCanonicalForm() {
71       return "(" + getKey() + Filter.APPROX + getValue() + ")";
72    }
73
74 }
75
Popular Tags