1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema;
21
22
23 import java.util.Comparator;
24
25 import javax.naming.Name;
26 import javax.naming.NamingException;
27
28 import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
29 import org.apache.directory.server.schema.registries.Registries;
30 import org.apache.directory.shared.ldap.name.LdapDN;
31
32
33
34
35
36
37
38
39 public class DnComparator implements Comparator
40 {
41
42 private AttributeTypeRegistry attrRegistry;
43
44
45 public DnComparator( AttributeTypeRegistry attrRegistry )
46 {
47 this.attrRegistry = attrRegistry;
48 }
49
50
51 public DnComparator()
52 {
53 }
54
55
56 public void setRegistries( Registries registries )
57 {
58 attrRegistry = registries.getAttributeTypeRegistry();
59 }
60
61
62 public int compare( Object obj0, Object obj1 )
63 {
64 LdapDN dn0 = null;
65 LdapDN dn1 = null;
66
67 try
68 {
69 dn0 = getDn( obj0 );
70 dn1 = getDn( obj1 );
71 }
72 catch ( NamingException e )
73 {
74
75 return -1;
76 }
77
78 return dn0.compareTo( dn1 );
79 }
80
81
82 public LdapDN getDn( Object obj ) throws NamingException
83 {
84 LdapDN dn = null;
85
86 if ( obj instanceof LdapDN )
87 {
88 dn = (LdapDN)obj;
89
90 dn = ( dn.isNormalized() ? dn : LdapDN.normalize( dn, attrRegistry.getNormalizerMapping() ) );
91 }
92 else if ( obj instanceof Name )
93 {
94 dn = new LdapDN( ( Name ) obj );
95 dn.normalize( attrRegistry.getNormalizerMapping() );
96 }
97 else if ( obj instanceof String )
98 {
99 dn = new LdapDN( ( String ) obj );
100 dn.normalize( attrRegistry.getNormalizerMapping() );
101 }
102 else
103 {
104 throw new IllegalStateException( "I do not know how to handle dn comparisons with objects of class: "
105 + (obj == null ? null : obj.getClass() ) );
106 }
107
108 return dn;
109 }
110 }