View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.comparators;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
26  import org.apache.directory.api.ldap.model.name.Dn;
27  import org.apache.directory.api.ldap.model.schema.LdapComparator;
28  import org.apache.directory.api.ldap.model.schema.SchemaManager;
29  
30  
31  /**
32   * A comparator that sorts OIDs based on their numeric id value.  Needs a 
33   * OidRegistry to properly do it's job.  Public method to set the oid 
34   * registry will be used by the server after instantiation in deserialization.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class UniqueMemberComparator extends LdapComparator<String>
39  {
40      /** The serial version UID */
41      private static final long serialVersionUID = 2L;
42  
43      /** A reference to the schema manager */
44      private SchemaManager schemaManager;
45  
46  
47      /**
48       * The IntegerComparator constructor. Its OID is the IntegerOrderingMatch matching
49       * rule OID.
50       * 
51       * @param oid The Comparator's OID
52       */
53      public UniqueMemberComparator( String oid )
54      {
55          super( oid );
56      }
57  
58  
59      /**
60       * {@inheritDoc}
61       */
62      public int compare( String dnstr1, String dnstr2 )
63      {
64          int dash1 = dnstr1.lastIndexOf( '#' );
65          int dash2 = dnstr2.lastIndexOf( '#' );
66  
67          if ( ( dash1 == -1 ) && ( dash2 == -1 ) )
68          {
69              // no UID part
70              try
71              {
72                  Dn dn1 = getDn( dnstr1 );
73                  Dn dn2 = getDn( dnstr2 );
74  
75                  if ( dn1.equals( dn2 ) )
76                  {
77                      return 0;
78                  }
79                  else
80                  {
81                      return -1;
82                  }
83              }
84              catch ( LdapInvalidDnException ne )
85              {
86                  return -1;
87              }
88          }
89          else
90          {
91              // Now, check that we don't have another '#'
92              if ( dnstr1.indexOf( '#' ) != dash1 )
93              {
94                  // Yes, we have one : this is not allowed, it should have been
95                  // escaped.
96                  return -1;
97              }
98  
99              if ( dnstr2.indexOf( '#' ) != dash1 )
100             {
101                 // Yes, we have one : this is not allowed, it should have been
102                 // escaped.
103                 return 1;
104             }
105 
106             Dn dn1;
107             Dn dn2;
108 
109             // This is an UID if the '#' is immediatly
110             // followed by a BitString, except if the '#' is
111             // on the last position
112             String uid1 = dnstr1.substring( dash1 + 1 );
113 
114             if ( dash1 > 0 )
115             {
116                 try
117                 {
118                     dn1 = new Dn( dnstr1.substring( 0, dash1 ) );
119                 }
120                 catch ( LdapException ne )
121                 {
122                     return -1;
123                 }
124             }
125             else
126             {
127                 return -1;
128             }
129 
130             // This is an UID if the '#' is immediatly
131             // followed by a BitString, except if the '#' is
132             // on the last position
133             String uid2 = dnstr2.substring( dash2 + 1 );
134 
135             if ( dash2 > 0 )
136             {
137                 try
138                 {
139                     dn2 = new Dn( dnstr1.substring( 0, dash2 ) );
140                 }
141                 catch ( LdapException ne )
142                 {
143                     return 1;
144                 }
145             }
146             else
147             {
148                 return 1;
149             }
150 
151             if ( dn1.equals( dn2 ) )
152             {
153                 return uid1.compareTo( uid2 );
154             }
155 
156             return -1;
157         }
158     }
159 
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public void setSchemaManager( SchemaManager schemaManager )
166     {
167         this.schemaManager = schemaManager;
168     }
169 
170 
171     /**
172      * Get the DN from the given object
173      *
174      * @param obj The object containing a DN (either as an instance of Dn or as a String)
175      * @return A Dn instance
176      * @throws LdapInvalidDnException If the Dn is invalid
177      */
178     public Dn getDn( Object obj ) throws LdapInvalidDnException
179     {
180         Dn dn;
181 
182         if ( obj instanceof Dn )
183         {
184             dn = ( Dn ) obj;
185 
186             dn = dn.isSchemaAware() ? dn : dn.apply( schemaManager );
187         }
188         else if ( obj instanceof String )
189         {
190             dn = new Dn( schemaManager, ( String ) obj );
191         }
192         else
193         {
194             throw new IllegalStateException( I18n.err( I18n.ERR_04218, obj == null ? null : obj.getClass()  ) );
195         }
196 
197         return dn;
198     }
199 }