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 */ 020 package org.apache.directory.shared.ldap.schema.comparators; 021 022 023 import java.io.IOException; 024 import java.text.ParseException; 025 026 import org.apache.directory.shared.i18n.I18n; 027 import org.apache.directory.shared.ldap.schema.LdapComparator; 028 import org.apache.directory.shared.ldap.schema.PrepareString; 029 import org.apache.directory.shared.ldap.util.GeneralizedTime; 030 import org.slf4j.Logger; 031 import org.slf4j.LoggerFactory; 032 033 034 /** 035 * A class for the generalizedTimeOrderingMatch matchingRule (RFC 4517, par. 4.2.17) 036 * 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 * @version $Rev: 437007 $ 039 */ 040 public class GeneralizedTimeComparator extends LdapComparator<String> 041 { 042 /** A logger for this class */ 043 private static final Logger LOG = LoggerFactory.getLogger( GeneralizedTimeComparator.class ); 044 045 /** The serialVersionUID */ 046 private static final long serialVersionUID = 1L; 047 048 049 /** 050 * The GeneralizedTimeComparator constructor. Its OID is the 051 * generalizedTimeOrderingMatch matching rule OID. 052 */ 053 public GeneralizedTimeComparator( String oid ) 054 { 055 super( oid ); 056 } 057 058 059 /** 060 * Implementation of the Compare method 061 */ 062 public int compare( String backendValue, String assertValue ) 063 { 064 LOG.debug( "comparing generalizedTimeOrdering objects '{}' with '{}'", backendValue, assertValue ); 065 066 // First, shortcut the process by comparing 067 // references. If they are equals, then o1 and o2 068 // reference the same object 069 if ( backendValue == assertValue ) 070 { 071 return 0; 072 } 073 074 // Then, deal with one of o1 or o2 being null 075 // Both can't be null, because then they would 076 // have been caught by the previous test 077 if ( ( backendValue == null ) || ( assertValue == null ) ) 078 { 079 return ( backendValue == null ? -1 : 1 ); 080 } 081 082 // Both objects must be stored as String for generalized tim. 083 // But we need to normalize the values first. 084 GeneralizedTime backendTime; 085 try 086 { 087 String prepared = PrepareString.normalize( backendValue, PrepareString.StringType.DIRECTORY_STRING ); 088 backendTime = new GeneralizedTime( prepared ); 089 } 090 catch ( IOException ioe ) 091 { 092 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) ); 093 } 094 catch ( ParseException pe ) 095 { 096 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) ); 097 } 098 099 GeneralizedTime assertTime; 100 try 101 { 102 String prepared = PrepareString.normalize( assertValue, PrepareString.StringType.DIRECTORY_STRING ); 103 assertTime = new GeneralizedTime( prepared ); 104 } 105 catch ( IOException ioe ) 106 { 107 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) ); 108 } 109 catch ( ParseException pe ) 110 { 111 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) ); 112 } 113 114 return backendTime.compareTo( assertTime ); 115 } 116 }