1 26 27 package com.opensugar.cube.ldap; 28 29 import java.util.Dictionary ; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 public abstract class Clause { 34 35 public static final double EPS = 1e-6; 36 37 public abstract boolean filter( Dictionary props ); 38 public abstract boolean areKeysCaseSensitive(); 39 public abstract String getCanonicalForm(); 40 41 protected Object getProperty( String key, Dictionary props ) { 42 if ( props == null ) { 43 return null; 44 } 45 46 if ( areKeysCaseSensitive() ) { 47 return props.get( key ); 48 } 49 else { 50 Enumeration keys = props.keys(); 51 String k; 52 while ( keys.hasMoreElements() ) { 53 k = (String )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 obj, String value ) { 63 if ( obj instanceof String ) { 65 if ( obj.toString().equals( value ) ) { 66 return 0; 67 } 68 else { 69 return Integer.MAX_VALUE; 70 } 71 } 72 else if ( obj instanceof Number ) { 74 try { 75 double d1 = ( (Number )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 ignore ) { } 89 } 90 else if ( obj instanceof Character ) { 92 if ( value.length() == 1 ) { 93 char c1 = ( (Character )obj ).charValue(); 94 char c2 = value.charAt( 0 ); 95 return (int)c1 - (int)c2; 96 } 97 } 98 else if ( obj instanceof Boolean ) { 100 Boolean b1 = (Boolean )obj; 101 Boolean 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 obj, String value ) { 110 if ( obj instanceof Object [] ) { 112 Object [] array = (Object [])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 else if ( obj instanceof Vector ) { 121 Vector vector = (Vector )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 |