| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 2001 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | import java.io.Serializable; |
| 41 | import java.text.Collator; |
| 42 | import java.util.Comparator; |
| 43 | import java.util.Iterator; |
| 44 | import java.util.Locale; |
| 45 | |
| 46 | /** |
| 47 | * Compares LDAP entries based on one or more attribute values. |
| 48 | * <P> |
| 49 | * |
| 50 | * To use this comparison for sorting search results, pass |
| 51 | * an object of this class to the <CODE>sort</CODE> method in |
| 52 | * <CODE>LDAPSearchResults</CODE>. |
| 53 | * <P> |
| 54 | * |
| 55 | * @version 1.0 |
| 56 | * @see LDAPSearchResults#sort |
| 57 | */ |
| 58 | |
| 59 | public class LDAPCompareAttrNames<E> |
| 60 | implements Comparator<E>, Serializable { |
| 61 | |
| 62 | static final long serialVersionUID = -2567450425231175944L; |
| 63 | private String _attrs[]; |
| 64 | private boolean _ascending[]; |
| 65 | private Locale _locale = null; |
| 66 | private Collator _collator = null; |
| 67 | private boolean _sensitive = true; |
| 68 | |
| 69 | /** |
| 70 | * Constructs a comparator that compares the string values of |
| 71 | * a named attribute in LDAP entries and sorts the entries in |
| 72 | * ascending order. |
| 73 | * <P> |
| 74 | * |
| 75 | * @param attribute name of attribute for comparisons |
| 76 | */ |
| 77 | public LDAPCompareAttrNames (String attribute) { |
| 78 | _attrs = new String[1]; |
| 79 | _attrs[0] = attribute; |
| 80 | _ascending = new boolean[1]; |
| 81 | _ascending[0] = true; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Constructs a comparator that compares the string values of |
| 86 | * a named attribute in LDAP entries and that allows you to sort |
| 87 | * entries either in ascending or descending order. |
| 88 | * <P> |
| 89 | * |
| 90 | * @param attribute name of attribute for comparisons |
| 91 | * @param ascendingFlag if <CODE>true</CODE>, sort in ascending order |
| 92 | */ |
| 93 | public LDAPCompareAttrNames (String attribute, |
| 94 | boolean ascendingFlag) { |
| 95 | _attrs = new String[1]; |
| 96 | _attrs[0] = attribute; |
| 97 | _ascending = new boolean[1]; |
| 98 | _ascending[0] = ascendingFlag; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Constructs a comparator that compares the string values of |
| 103 | * a set of named attributes in LDAP entries and that sort |
| 104 | * the entries in ascending order. |
| 105 | * <P> |
| 106 | * |
| 107 | * Use an array of strings to specify the set of attributes |
| 108 | * to use for sorting. If the values of the first attribute |
| 109 | * (the name specified in <CODE>attribute[0]</CODE>) are equal, |
| 110 | * then the values of the next attribute are compared. |
| 111 | * <P> |
| 112 | * |
| 113 | * For example, if <CODE>attributes[0] = "cn"</CODE> and |
| 114 | * <CODE>attributes[1]="uid"</CODE>, results are first sorted |
| 115 | * by the <CODE>cn</CODE> attribute. If two entries have the |
| 116 | * same value for <CODE>cn</CODE>, then the <CODE>uid</CODE> |
| 117 | * attribute is used to sort the entries. |
| 118 | * <P> |
| 119 | * |
| 120 | * @param attributes array of the attribute names used for comparisons |
| 121 | */ |
| 122 | public LDAPCompareAttrNames (String[] attributes) { |
| 123 | _attrs = attributes; |
| 124 | _ascending = new boolean[attributes.length]; |
| 125 | for( int i = 0; i < attributes.length; i++ ) |
| 126 | _ascending[i] = true; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Constructs a comparator that compares the string values of |
| 131 | * a set of named attributes in LDAP entries and allows you |
| 132 | * to sort the entries in ascending or descending order. |
| 133 | * <P> |
| 134 | * |
| 135 | * Use an array of strings to specify the set of attributes |
| 136 | * to use for sorting. If the values of the first attribute |
| 137 | * (the name specified in <CODE>attribute[0]</CODE>) |
| 138 | * are equal, then the values of the next attribute are compared. |
| 139 | * <P> |
| 140 | * |
| 141 | * For example, if <CODE>attributes[0] = "cn"</CODE> and |
| 142 | * <CODE>attributes[1]="uid"</CODE>, results are first sorted |
| 143 | * by the <CODE>cn</CODE> attribute. If two entries have the |
| 144 | * same value for <CODE>cn</CODE>, then the <CODE>uid</CODE> |
| 145 | * attribute is used to sort the entries. |
| 146 | * <P> |
| 147 | * |
| 148 | * Use an array of boolean values to specify whether each attribute |
| 149 | * should be sorted in ascending or descending order. For example, |
| 150 | * suppose that <CODE>attributes[0] = "cn"</CODE> and |
| 151 | * <CODE>attributes[1]="roomNumber"</CODE>. If |
| 152 | * <CODE>ascendingFlags[0]=true</CODE> and |
| 153 | * <CODE>ascendingFlags[1]=false</CODE>, attributes are sorted first by |
| 154 | * <CODE>cn</CODE> in ascending order, then by <CODE>roomNumber</CODE> |
| 155 | * in descending order. |
| 156 | * <P> |
| 157 | * |
| 158 | * If the size of the array of attribute names is not the same as |
| 159 | * the size of the array of boolean values, an |
| 160 | * <CODE>LDAPException</CODE> is thrown. |
| 161 | * <P> |
| 162 | * |
| 163 | * @param attribute array of the attribute names to use for comparisons |
| 164 | * @param ascendingFlags array of boolean values specifying ascending |
| 165 | * or descending order to use for each attribute name. If |
| 166 | * <CODE>true</CODE>, the attributes are sorted in ascending order. |
| 167 | */ |
| 168 | public LDAPCompareAttrNames (String[] attributes, |
| 169 | boolean[] ascendingFlags) { |
| 170 | _attrs = attributes; |
| 171 | _ascending = ascendingFlags; |
| 172 | if ( _ascending == null ) { |
| 173 | _ascending = new boolean[attributes.length]; |
| 174 | for( int i = 0; i < attributes.length; i++ ) |
| 175 | _ascending[i] = true; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Indicates if some other object is "equal to" this Comparator |
| 181 | * |
| 182 | * @param obj the object to compare |
| 183 | * @return true if the object equals this object |
| 184 | */ |
| 185 | public boolean equals( Object obj ) { |
| 186 | return super.equals( obj ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Gets the locale, if any, used for collation. If the locale is null, |
| 191 | * an ordinary string comparison is used for sorting. |
| 192 | * |
| 193 | * @return the locale used for collation, or null. |
| 194 | */ |
| 195 | public Locale getLocale() { |
| 196 | return _locale; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Set the locale, if any, used for collation. If the locale is null, |
| 201 | * an ordinary string comparison is used for sorting. If sorting |
| 202 | * has been set to case-insensitive, the collation strength is set |
| 203 | * to Collator.PRIMARY, otherwise to Collator.IDENTICAL. If a |
| 204 | * different collation strength setting is required, use the signature |
| 205 | * that takes a collation strength parameter. |
| 206 | * |
| 207 | * @param locale the locale used for collation, or null. |
| 208 | */ |
| 209 | public void setLocale( Locale locale ) { |
| 210 | if ( _sensitive ) { |
| 211 | setLocale( locale, Collator.IDENTICAL ); |
| 212 | } else { |
| 213 | setLocale( locale, Collator.PRIMARY ); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Sets the locale, if any, used for collation. If the locale is null, |
| 219 | * an ordinary string comparison is used for sorting. |
| 220 | * |
| 221 | * @param locale the locale used for collation, or null. |
| 222 | * @param strength collation strength: Collator.PRIMARY, |
| 223 | * Collator.SECONDARY, Collator.TERTIARY, or Collator.IDENTICAL |
| 224 | */ |
| 225 | public void setLocale( Locale locale, int strength ) { |
| 226 | _locale = locale; |
| 227 | if ( _locale == null ) { |
| 228 | _collator = null; |
| 229 | } else { |
| 230 | _collator = Collator.getInstance( _locale ); |
| 231 | _collator.setStrength(strength); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Gets the state of the case-sensitivity flag. This only applies to |
| 237 | * Unicode sort order; for locale-specific sorting, case-sensitivity |
| 238 | * is controlled by the collation strength. |
| 239 | * |
| 240 | * @return <code>true</code> for case-sensitive sorting; this is |
| 241 | * the default |
| 242 | */ |
| 243 | public boolean getCaseSensitive() { |
| 244 | return _sensitive; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Sets the state of the case-sensitivity flag. This only applies to |
| 249 | * Unicode sort order; for locale-specific sorting, case-sensitivity |
| 250 | * is controlled by the collation strength. |
| 251 | * |
| 252 | * @param sensitive <code>true</code> for case-sensitive sorting; |
| 253 | * this is the default |
| 254 | */ |
| 255 | public void setCaseSensitive( boolean sensitive ) { |
| 256 | _sensitive = sensitive; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Compares its two arguments for order |
| 261 | * |
| 262 | * @param entry1 the first entry to be compared |
| 263 | * @param entry2 the second entry to be compared |
| 264 | * @return a negative integer, zero, or a positive integer if the first |
| 265 | * argument is less than, equal to, or greater than the second |
| 266 | * @exception ClassCastException if either of the arguments is not |
| 267 | * an LDAPEntry |
| 268 | */ |
| 269 | public int compare( Object entry1, Object entry2 ) { |
| 270 | if ( !(entry1 instanceof LDAPEntry) || |
| 271 | !(entry2 instanceof LDAPEntry) ) { |
| 272 | throw new ClassCastException( "Must compare LDAPEntry objects" ); |
| 273 | } |
| 274 | |
| 275 | return attrGreater( (LDAPEntry)entry1, (LDAPEntry)entry2, 0 ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Compares a particular attribute in both entries. If equal, |
| 280 | * moves on to the next. |
| 281 | * @param entry1 the first entry to be compared |
| 282 | * @param entry2 the second entry to be compared |
| 283 | * @return a negative integer, zero, or a positive integer if the first |
| 284 | * argument is less than, equal to, or greater than the second |
| 285 | */ |
| 286 | int attrGreater( LDAPEntry entry1, LDAPEntry entry2, |
| 287 | int attrPos ) { |
| 288 | Iterator<LDAPAttribute> attrSet1 = |
| 289 | entry1.getAttributeSet().iterator(); |
| 290 | Iterator<LDAPAttribute> attrSet2 = |
| 291 | entry2.getAttributeSet().iterator(); |
| 292 | |
| 293 | String value1 = null; |
| 294 | String value2 = null; |
| 295 | String attrName = _attrs[attrPos]; |
| 296 | boolean ascending = _ascending[attrPos]; |
| 297 | |
| 298 | while ( attrSet2.hasNext() ) { |
| 299 | LDAPAttribute currAttr = attrSet2.next(); |
| 300 | if ( !attrName.equalsIgnoreCase (currAttr.getName()) ) { |
| 301 | continue; |
| 302 | } |
| 303 | value2 = currAttr.getStringValues().nextElement(); |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | while ( attrSet1.hasNext() ) { |
| 308 | LDAPAttribute currAttr = attrSet1.next(); |
| 309 | if ( !attrName.equalsIgnoreCase(currAttr.getName()) ) { |
| 310 | continue; |
| 311 | } |
| 312 | value1 = currAttr.getStringValues().nextElement(); |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | if ( (value2 == null) ^ (value1 == null) ) { |
| 317 | return (value1 != null) ? 1 : -1; |
| 318 | } |
| 319 | |
| 320 | // Check for equality |
| 321 | if ( (value2 == null) || |
| 322 | ((_collator != null) && |
| 323 | (_collator.compare( value1, value2 ) == 0) ) || |
| 324 | ((_collator == null) && _sensitive && |
| 325 | value2.equals(value1)) || |
| 326 | ((_collator == null) && !_sensitive && |
| 327 | value2.equalsIgnoreCase(value1)) ) { |
| 328 | |
| 329 | if ( attrPos == _attrs.length - 1 ) { |
| 330 | return -1; |
| 331 | } else { |
| 332 | return attrGreater ( entry1, entry2, attrPos+1 ); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Not equal, check for order |
| 337 | if ( ascending ) { |
| 338 | if ( _collator != null ) { |
| 339 | return _collator.compare( value1, value2 ); |
| 340 | } else if ( _sensitive ) { |
| 341 | return value1.compareTo( value2 ); |
| 342 | } else { |
| 343 | return value1.toLowerCase().compareTo( value2.toLowerCase() ); |
| 344 | } |
| 345 | } else { |
| 346 | if ( _collator != null ) { |
| 347 | return _collator.compare( value1, value2 ); |
| 348 | } else if ( _sensitive ) { |
| 349 | return value1.compareTo( value2 ); |
| 350 | } else { |
| 351 | return value1.toLowerCase().compareTo( value2.toLowerCase() ); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |