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