1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37 public class ObjectIdentifierFirstComponentComparator extends LdapComparator<String>
38 {
39
40 private static final long serialVersionUID = 2L;
41
42
43 private static final Logger LOG = LoggerFactory.getLogger( ObjectIdentifierFirstComponentComparator.class );
44
45
46
47
48
49
50
51
52 public ObjectIdentifierFirstComponentComparator( String oid )
53 {
54 super( oid );
55 }
56
57
58
59
60
61
62
63
64 private String getNumericOid( String s )
65 {
66
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
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
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
123 if ( s1.equals( s2 ) )
124 {
125 return 0;
126 }
127
128
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 }