KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 public abstract class Clause {
34
35    public static final double EPS = 1e-6;
36
37    public abstract boolean filter( Dictionary JavaDoc props );
38    public abstract boolean areKeysCaseSensitive();
39    public abstract String JavaDoc getCanonicalForm();
40
41    protected Object JavaDoc getProperty( String JavaDoc key, Dictionary JavaDoc props ) {
42       if ( props == null ) {
43          return null;
44       }
45
46       if ( areKeysCaseSensitive() ) {
47          return props.get( key );
48       }
49       else {
50          Enumeration JavaDoc keys = props.keys();
51          String JavaDoc k;
52          while ( keys.hasMoreElements() ) {
53             k = (String JavaDoc)keys.nextElement();
54             if ( k.equalsIgnoreCase( key ) ) {
55                return props.get( k );
56             }
57          }
58          return null;
59       }
60    }
61
62    protected static int compare( Object JavaDoc obj, String JavaDoc value ) {
63       // Treat strings
64
if ( obj instanceof String JavaDoc ) {
65          if ( obj.toString().equals( value ) ) {
66             return 0;
67          }
68          else {
69             return Integer.MAX_VALUE;
70          }
71       }
72       // Treat numbers
73
else if ( obj instanceof Number JavaDoc ) {
74          try {
75             double d1 = ( (Number JavaDoc)obj ).doubleValue();
76             double d2 = Double.valueOf( value ).doubleValue();
77             if ( Math.abs( d1 - d2 ) < EPS ) {
78               return 0;
79             }
80             else if ( d1 < d2 ) {
81               return -1;
82             }
83             else {
84               return 1;
85             }
86          }
87          catch( NumberFormatException JavaDoc ignore ) { // go to end
88
}
89       }
90       // Treat characters
91
else if ( obj instanceof Character JavaDoc ) {
92          if ( value.length() == 1 ) {
93             char c1 = ( (Character JavaDoc)obj ).charValue();
94             char c2 = value.charAt( 0 );
95             return (int)c1 - (int)c2;
96          }
97       }
98       // Treat booleans
99
else if ( obj instanceof Boolean JavaDoc ) {
100          Boolean JavaDoc b1 = (Boolean JavaDoc)obj;
101          Boolean JavaDoc b2 = Boolean.valueOf( value );
102          if ( b1.equals( b2 ) ) {
103             return 0;
104          }
105       }
106       return Integer.MAX_VALUE;
107    }
108
109    protected int[] compareSets( Object JavaDoc obj, String JavaDoc value ) {
110       // Treat arrays
111
if ( obj instanceof Object JavaDoc[] ) {
112          Object JavaDoc[] array = (Object JavaDoc[])obj;
113          int[] ret = new int[ array.length ];
114          for ( int i = 0; i < array.length; i++ ) {
115             ret[ i ] = compare( array[ i ], value );
116          }
117          return ret;
118       }
119       // Treat vectors
120
else if ( obj instanceof Vector JavaDoc ) {
121          Vector JavaDoc vector = (Vector JavaDoc)obj;
122          int[] ret = new int[ vector.size() ];
123          for ( int i = 0; i < vector.size(); i++ ) {
124             ret[ i ] = compare( vector.elementAt( i ), value );
125          }
126          return ret;
127       }
128
129       int[] ret = new int[ 1 ];
130       ret[ 0 ] = Integer.MAX_VALUE;
131       return ret;
132    }
133
134 }
Popular Tags