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   *    https://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.asn1.util.Oid;
24  import org.apache.directory.api.i18n.I18n;
25  import org.apache.directory.api.ldap.model.schema.LdapComparator;
26  import org.apache.directory.api.util.Chars;
27  import org.apache.directory.api.util.Strings;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  
32  /**
33   * A comparator for Comparators. We compare the OIDs
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class ObjectIdentifierFirstComponentComparator extends LdapComparator<String>
38  {
39      /** The serial version UID */
40      private static final long serialVersionUID = 2L;
41  
42      /** A logger for this class */
43      private static final Logger LOG = LoggerFactory.getLogger( ObjectIdentifierFirstComponentComparator.class );
44  
45  
46      /**
47       * The ObjectIdentifierFirstComponentComparator constructor. Its OID is the 
48       * ObjectIdentifierFirstComponentMatch matching rule OID.
49       * 
50       * @param oid The Comparator's OID
51       */
52      public ObjectIdentifierFirstComponentComparator( String oid )
53      {
54          super( oid );
55      }
56  
57  
58      /**
59       * Get the OID from the SchemaObject description
60       * 
61       * @param s The string cntaining the OID
62       * @return The found OID
63       */
64      private String getNumericOid( String s )
65      {
66          // Get the OID from the strings now
67          int pos = 0;
68  
69          if ( !Strings.isCharASCII( s, pos++, '(' ) )
70          {
71              return null;
72          }
73  
74          while ( Strings.isCharASCII( s, pos, ' ' ) )
75          {
76              pos++;
77          }
78  
79          int start = pos;
80  
81          while ( Chars.isDigit( s, pos ) || Strings.isCharASCII( s, pos, '.' ) )
82          {
83              pos++;
84          }
85  
86          String numericOid = s.substring( start, pos );
87  
88          if ( Oid.isOid( numericOid ) )
89          {
90              return numericOid;
91          }
92          else
93          {
94              return null;
95          }
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     public int compare( String s1, String s2 )
103     {
104         if ( LOG.isDebugEnabled() )
105         {
106             LOG.debug( I18n.msg( I18n.MSG_13748_COMPARING_OBJECT_IDENTIFIER_FIRST_COMPONENT, s1, s2 ) );
107         }
108 
109         // -------------------------------------------------------------------
110         // Handle some basis cases
111         // -------------------------------------------------------------------
112         if ( s1 == null )
113         {
114             return ( s2 == null ) ? 0 : -1;
115         }
116 
117         if ( s2 == null )
118         {
119             return -1;
120         }
121 
122         // Let's try to avoid a parse.
123         if ( s1.equals( s2 ) )
124         {
125             return 0;
126         }
127 
128         // Get the OID from the strings now
129         String oid1 = getNumericOid( s1 );
130 
131         if ( oid1 == null )
132         {
133             return -1;
134         }
135 
136         String oid2 = getNumericOid( s2 );
137 
138         if ( oid2 == null )
139         {
140             return -1;
141         }
142 
143         if ( oid1.equals( oid2 ) )
144         {
145             return 0;
146         }
147         else
148         {
149             return -1;
150         }
151     }
152 }