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.ldap.model.exception.LdapException; 024import org.apache.directory.api.ldap.model.schema.LdapComparator; 025import org.apache.directory.api.ldap.model.schema.Normalizer; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029 030/** 031 * A comparator which normalizes a value first before using a subordinate 032 * comparator to compare them. 033 * 034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 035 */ 036public class NormalizingComparator 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( NormalizingComparator.class ); 043 044 /** the Normalizer to normalize values with before comparing */ 045 private Normalizer normalizer; 046 047 /** the underlying comparator to use for comparisons */ 048 private LdapComparator<String> comparator; 049 050 private boolean onServer = false; 051 052 053 /** 054 * A comparator which normalizes a value first before comparing them. 055 * 056 * @param oid The Comparator's OID 057 * @param normalizer the Normalizer to normalize values with before comparing 058 * @param comparator the underlying comparator to use for comparisons 059 */ 060 public NormalizingComparator( String oid, Normalizer normalizer, LdapComparator<String> comparator ) 061 { 062 super( oid ); 063 this.normalizer = normalizer; 064 this.comparator = comparator; 065 } 066 067 068 /** 069 * {@inheritDoc} 070 */ 071 public int compare( String o1, String o2 ) 072 { 073 if ( onServer ) 074 { 075 return comparator.compare( o1, o2 ); 076 } 077 078 String n1; 079 String n2; 080 081 try 082 { 083 n1 = normalizer.normalize( o1 ); 084 } 085 catch ( LdapException e ) 086 { 087 LOG.warn( "Failed to normalize: " + o1, e ); 088 n1 = o1; 089 } 090 091 try 092 { 093 n2 = normalizer.normalize( o2 ); 094 } 095 catch ( LdapException e ) 096 { 097 LOG.warn( "Failed to normalize: " + o2, e ); 098 n2 = o2; 099 } 100 101 return comparator.compare( n1, n2 ); 102 } 103 104 105 /** 106 * {@inheritDoc} 107 * 108 * This implementation makes sure we update the oid property of the contained normalizer and 109 * comparator. 110 */ 111 @Override 112 public void setOid( String oid ) 113 { 114 super.setOid( oid ); 115 normalizer.setOid( oid ); 116 comparator.setOid( oid ); 117 } 118 119 120 /** 121 * tells that the normalizingComparator should not normalize values which are 122 * already normalized on the server 123 */ 124 public void setOnServer() 125 { 126 this.onServer = true; 127 } 128}