Compares Ldap entries based on attribute name. An object of this class defines ordering when sorting LdapEntries, usually from search results. When using this Comparator, LdapEntry objects are sorted by the attribute names(s) passed in on the constructor, in ascending or descending order. The object is typically supplied to an implementation of the collection interfaces such as java.util.TreeSet which performs sorting. Comparison is performed via locale-sensitive Java String comparison, which may not correspond to the Ldap ordering rules by which an Ldap server would sort them.
Inheritance: System.Collections.IComparer
        /// <summary> Determines if this comparator is equal to the comparator passed in.
        ///
        ///  This will return true if the comparator is an instance of
        /// LdapCompareAttrNames and compares the same attributes names in the same
        /// order.
        ///
        /// </summary>
        /// <returns> true the comparators are equal
        /// </returns>
        public override bool Equals(System.Object comparator)
        {
            if (!(comparator is LdapCompareAttrNames))
            {
                return(false);
            }
            LdapCompareAttrNames comp = (LdapCompareAttrNames)comparator;

            // Test to see if the attribute to compare are the same length
            if ((comp.sortByNames.Length != this.sortByNames.Length) || (comp.sortAscending.Length != this.sortAscending.Length))
            {
                return(false);
            }

            // Test to see if the attribute names and sorting orders are the same.
            for (int i = 0; i < this.sortByNames.Length; i++)
            {
                if (comp.sortAscending[i] != this.sortAscending[i])
                {
                    return(false);
                }
                if (!comp.sortByNames[i].ToUpper().Equals(this.sortByNames[i].ToUpper()))
                {
                    return(false);
                }
            }
            return(true);
        }
示例#2
0
    public static void Main( String[] args )
    {
        if (args.Length != 5)
        {
            Console.WriteLine("Usage:   mono ClientSideSort <host name> "+
                       "<login dn> <password> <search base>\n"
                       + "         <search filter>");
            Console.WriteLine("Example: mono ClientSideSort Acme.com"
                       + " \"cn=admin,o=Acme\""
                       + " secret \"ou=sales,o=Acme\"\n"
                       + "         \"(objectclass=*)\"");
            Environment.Exit(0);
        }

        int ldapPort = LdapConnection.DEFAULT_PORT;
        int searchScope = LdapConnection.SCOPE_ONE;
        int ldapVersion  = LdapConnection.Ldap_V3;
        String ldapHost = args[0];
        String loginDN  = args[1];
        String password = args[2];
        String searchBase = args[3];
        String searchFilter = args[4];
        LdapConnection conn = new LdapConnection();

        try
        {
            // connect to the server
            conn.Connect( ldapHost, ldapPort );

            // bind to the server
            conn.Bind( ldapVersion, loginDN, password);

            LdapSearchResults searchResults = conn.Search(  searchBase,
                                                            searchScope,
                                                            searchFilter,
                                                            new String[] {"cn", "uid", "sn"}, //attributes
                                                            false);        // return attrs and values

            /* sortedResults will sort the entries according to the natural
             * ordering of LDAPEntry (by distiguished name).
             */

            ArrayList sortedResults = new ArrayList();
            while ( searchResults.hasMore())
            {
                try
                {
                    sortedResults.Add( searchResults.next() );
                }
                catch(LdapException e)
                {
                    Console.WriteLine("Error: " + e.ToString());
                    // Exception is thrown, go for next entry
                    continue;
                }
            }

            // print the sorted results
            Console.WriteLine( "\n"+
                       "****************************\n"+
                       "Search results sorted by DN:\n"+
                       "****************************");
            sortedResults.Sort();
            IEnumerator i = sortedResults.GetEnumerator(0,sortedResults.Count-1);
            while (i.MoveNext())
            {
                PrintEntry( (LdapEntry)(i.Current) );
            }

            /* resort the results an an array using a specific comparator */
            String[] namesToSortBy  = { "sn", "uid", "cn"  };
            bool[] sortAscending = { true, false, true };
            LdapCompareAttrNames myComparator = new LdapCompareAttrNames( namesToSortBy, sortAscending );

            Object[] sortedSpecial = sortedResults.ToArray();
            Array.Sort(sortedSpecial, myComparator);

            // print the re-sorted results
            Console.WriteLine( "\n" +
                   "*****************************************************\n" +
                   "Search results sorted by sn, uid(Descending), and cn:\n" +
                   "*****************************************************");
            for(int j=0; j< sortedSpecial.Length; j++)
            {
                PrintEntry( (LdapEntry) sortedSpecial[j] );
            }
            // disconnect with the server
            conn.Disconnect();
        }
        catch( LdapException e )
        {
            Console.WriteLine( "Error: " + e.ToString() );
        }
        catch( Exception e )
        {
            Console.WriteLine( "Error: " + e.ToString() );
        }
        Environment.Exit(0);
    }