KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.StringTokenizer JavaDoc;
31
32 public class SubstringClause extends SimpleClause {
33
34    public static final char WILDCARD = '*';
35
36    public SubstringClause( String JavaDoc key, String JavaDoc value, boolean keysCaseSensitive ) {
37       super( key, value, keysCaseSensitive );
38    }
39
40    public boolean filter( Dictionary JavaDoc props ) {
41       Object JavaDoc obj = getProperty( getKey(), props );
42       if ( obj == null ) {
43          return false;
44       }
45
46       if ( obj instanceof String JavaDoc ) {
47          return isSubstring( (String JavaDoc)obj, getValue() );
48       }
49       return false;
50    }
51
52    private static boolean isSubstring( String JavaDoc s, String JavaDoc pattern ) {
53       StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( pattern, "" + WILDCARD, true );
54       String JavaDoc 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 JavaDoc getCanonicalForm() {
79       return "(" + getKey() + Filter.EQUALS + getValue() + ")";
80    }
81
82 }
83
Popular Tags