001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.model.schema.comparators; 021 022 023import org.apache.directory.api.asn1.util.Oid; 024import org.apache.directory.api.i18n.I18n; 025import org.apache.directory.api.ldap.model.schema.LdapComparator; 026import org.apache.directory.api.util.Chars; 027import org.apache.directory.api.util.Strings; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031 032/** 033 * A comparator for Comparators. We compare the OIDs 034 * 035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 036 */ 037public class ObjectIdentifierFirstComponentComparator extends LdapComparator<String> 038{ 039 /** The serial version UID */ 040 private static final long serialVersionUID = 2L; 041 042 /** A logger for this class */ 043 private static final Logger LOG = LoggerFactory.getLogger( ObjectIdentifierFirstComponentComparator.class ); 044 045 046 /** 047 * The ObjectIdentifierFirstComponentComparator constructor. Its OID is the 048 * ObjectIdentifierFirstComponentMatch matching rule OID. 049 * 050 * @param oid The Comparator's OID 051 */ 052 public ObjectIdentifierFirstComponentComparator( String oid ) 053 { 054 super( oid ); 055 } 056 057 058 /** 059 * Get the OID from the SchemaObject description 060 * 061 * @param s The string cntaining the OID 062 * @return The found OID 063 */ 064 private String getNumericOid( String s ) 065 { 066 // Get the OID from the strings now 067 int pos = 0; 068 069 if ( !Strings.isCharASCII( s, pos++, '(' ) ) 070 { 071 return null; 072 } 073 074 while ( Strings.isCharASCII( s, pos, ' ' ) ) 075 { 076 pos++; 077 } 078 079 int start = pos; 080 081 while ( Chars.isDigit( s, pos ) || Strings.isCharASCII( s, pos, '.' ) ) 082 { 083 pos++; 084 } 085 086 String numericOid = s.substring( start, pos ); 087 088 if ( Oid.isOid( numericOid ) ) 089 { 090 return numericOid; 091 } 092 else 093 { 094 return null; 095 } 096 } 097 098 099 /** 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}