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      public UniqueMemberComparator( String oid )
52      {
53          super( oid );
54      }
55  
56  
57      /**
58       * Implementation of the Compare method
59       */
60      public int compare( String dnstr1, String dnstr2 )
61      {
62          int dash1 = dnstr1.lastIndexOf( '#' );
63          int dash2 = dnstr2.lastIndexOf( '#' );
64  
65          if ( ( dash1 == -1 ) && ( dash2 == -1 ) )
66          {
67              // no UID part
68              try
69              {
70                  Dn dn1 = getDn( dnstr1 );
71                  Dn dn2 = getDn( dnstr2 );
72  
73                  if ( dn1.equals( dn2 ) )
74                  {
75                      return 0;
76                  }
77                  else
78                  {
79                      return -1;
80                  }
81              }
82              catch ( LdapInvalidDnException ne )
83              {
84                  return -1;
85              }
86          }
87          else
88          {
89              // Now, check that we don't have another '#'
90              if ( dnstr1.indexOf( '#' ) != dash1 )
91              {
92                  // Yes, we have one : this is not allowed, it should have been
93                  // escaped.
94                  return -1;
95              }
96  
97              if ( dnstr2.indexOf( '#' ) != dash1 )
98              {
99                  // Yes, we have one : this is not allowed, it should have been
100                 // escaped.
101                 return 1;
102             }
103 
104             Dn dn1 = null;
105             Dn dn2 = null;
106 
107             // This is an UID if the '#' is immediatly
108             // followed by a BitString, except if the '#' is
109             // on the last position
110             String uid1 = dnstr1.substring( dash1 + 1 );
111 
112             if ( dash1 > 0 )
113             {
114                 try
115                 {
116                     dn1 = new Dn( dnstr1.substring( 0, dash1 ) );
117                 }
118                 catch ( LdapException ne )
119                 {
120                     return -1;
121                 }
122             }
123             else
124             {
125                 return -1;
126             }
127 
128             // This is an UID if the '#' is immediatly
129             // followed by a BitString, except if the '#' is
130             // on the last position
131             String uid2 = dnstr2.substring( dash2 + 1 );
132 
133             if ( dash2 > 0 )
134             {
135                 try
136                 {
137                     dn2 = new Dn( dnstr1.substring( 0, dash2 ) );
138                 }
139                 catch ( LdapException ne )
140                 {
141                     return 1;
142                 }
143             }
144             else
145             {
146                 return 1;
147             }
148 
149             if ( dn1.equals( dn2 ) )
150             {
151                 return uid1.compareTo( uid2 );
152             }
153 
154             return -1;
155         }
156     }
157 
158 
159     /**
160      * {@inheritDoc}
161      */
162     public void setSchemaManager( SchemaManager schemaManager )
163     {
164         this.schemaManager = schemaManager;
165     }
166 
167 
168     public Dn getDn( Object obj ) throws LdapInvalidDnException
169     {
170         Dn dn = null;
171 
172         if ( obj instanceof Dn )
173         {
174             dn = ( Dn ) obj;
175 
176             dn = ( dn.isSchemaAware() ? dn : dn.apply( schemaManager ) );
177         }
178         else if ( obj instanceof String )
179         {
180             dn = new Dn( schemaManager, ( String ) obj );
181         }
182         else
183         {
184             throw new IllegalStateException( I18n.err( I18n.ERR_04218, ( obj == null ? null : obj.getClass() ) ) );
185         }
186 
187         return dn;
188     }
189 }