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.shared.ldap.model.schema.comparators;
021
022
023import org.apache.directory.shared.asn1.util.Oid;
024import org.apache.directory.shared.ldap.model.schema.LdapComparator;
025import org.apache.directory.shared.util.Chars;
026import org.apache.directory.shared.util.Strings;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * A comparator for Comparators. We compare the OIDs
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class ObjectIdentifierFirstComponentComparator extends LdapComparator<String>
037{
038    /** The serial version UID */
039    private static final long serialVersionUID = 2L;
040
041    /** A logger for this class */
042    private static final Logger LOG = LoggerFactory.getLogger( ObjectIdentifierFirstComponentComparator.class );
043
044    /**
045     * The ObjectIdentifierFirstComponentComparator constructor. Its OID is the 
046     * ObjectIdentifierFirstComponentMatch matching rule OID.
047     */
048    public ObjectIdentifierFirstComponentComparator( String oid )
049    {
050        super( oid );
051    }
052
053    
054    /**
055     * Get the OID from the SchemaObject description
056     */
057    private String getNumericOid( String s )
058    {
059        // Get the OID from the strings now
060        int pos = 0;
061        
062        if ( !Strings.isCharASCII( s, pos++, '(' ) )
063        {
064            return null;
065        }
066        
067        while ( Strings.isCharASCII( s, pos, ' ' ) )
068        {
069            pos++;
070        }
071        
072        int start = pos;
073        
074        while ( Chars.isDigit(s, pos) || Strings.isCharASCII( s, pos, '.' ) )
075        {
076            pos++;
077        }
078        
079        String numericOid = s.substring( start, pos );
080        
081        if ( Oid.isOid(numericOid) )
082        {
083            return numericOid;
084        }
085        else
086        {
087            return null;
088        }
089    }
090    
091    /**
092     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
093     */
094    public int compare( String s1, String s2 )
095    {
096        LOG.debug( "comparing ObjectIdentifierFirstComponent objects '{}' with '{}'", s1, s2 );
097
098        // -------------------------------------------------------------------
099        // Handle some basis cases
100        // -------------------------------------------------------------------
101        if ( s1 == null )
102        {
103            return ( s2 == null ) ? 0 : -1;
104        }
105        
106        if ( s2 == null )
107        {
108            return -1;
109        }
110
111        // Let's try to avoid a parse.
112        if ( s1.equals( s2 ) )
113        {
114            return 0;
115        }
116        
117        // Get the OID from the strings now
118        String oid1 = getNumericOid( s1 );
119        
120        if ( oid1 == null )
121        {
122            return -1;
123        }
124        
125        String oid2 = getNumericOid( s2 );
126
127        if ( oid2 == null )
128        {
129            return -1;
130        }
131        
132        if ( oid1.equals( oid2 ) )
133        {
134            return 0;
135        }
136        else
137        {
138            return -1;
139        }
140    }
141}