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.normalizers; 021 022 023import org.apache.directory.api.i18n.I18n; 024import org.apache.directory.api.ldap.model.constants.SchemaConstants; 025import org.apache.directory.api.ldap.model.entry.StringValue; 026import org.apache.directory.api.ldap.model.entry.Value; 027import org.apache.directory.api.ldap.model.exception.LdapException; 028import org.apache.directory.api.ldap.model.name.Dn; 029import org.apache.directory.api.ldap.model.schema.Normalizer; 030import org.apache.directory.api.ldap.model.schema.SchemaManager; 031import org.apache.directory.api.util.Strings; 032 033 034/** 035 * A noirmalizer for UniqueMember 036 * 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 */ 039@SuppressWarnings("serial") 040public class UniqueMemberNormalizer extends Normalizer 041{ 042 /** A reference to the schema manager used to normalize the Dn */ 043 private SchemaManager schemaManager; 044 045 046 /** 047 * Creates a new UniqueMemberNormalizer instance 048 */ 049 public UniqueMemberNormalizer() 050 { 051 super( SchemaConstants.UNIQUE_MEMBER_MATCH_MR_OID ); 052 } 053 054 055 @Override 056 public Value<?> normalize( Value<?> value ) throws LdapException 057 { 058 String nameAndUid = value.getString(); 059 060 if ( nameAndUid.length() == 0 ) 061 { 062 return null; 063 } 064 065 // Let's see if we have an UID part 066 int sharpPos = nameAndUid.lastIndexOf( '#' ); 067 068 if ( sharpPos != -1 ) 069 { 070 // Now, check that we don't have another '#' 071 if ( nameAndUid.indexOf( '#' ) != sharpPos ) 072 { 073 // Yes, we have one : this is not allowed, it should have been 074 // escaped. 075 return null; 076 } 077 078 // This is an UID if the '#' is immediately 079 // followed by a BitString, except if the '#' is 080 // on the last position 081 String uid = nameAndUid.substring( sharpPos + 1 ); 082 083 if ( sharpPos > 0 ) 084 { 085 Dn dn = new Dn( schemaManager, nameAndUid.substring( 0, sharpPos ) ); 086 087 return new StringValue( dn.getNormName() + '#' + uid ); 088 } 089 else 090 { 091 throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) ); 092 } 093 } 094 else 095 { 096 // No UID, the strValue is a Dn 097 // Return the normalized Dn 098 return new StringValue( new Dn( nameAndUid ).getNormName() ); 099 } 100 } 101 102 103 @Override 104 public String normalize( String value ) throws LdapException 105 { 106 if ( Strings.isEmpty( value ) ) 107 { 108 return null; 109 } 110 111 // Let's see if we have an UID part 112 int sharpPos = value.lastIndexOf( '#' ); 113 114 if ( sharpPos != -1 ) 115 { 116 // Now, check that we don't have another '#' 117 if ( value.indexOf( '#' ) != sharpPos ) 118 { 119 // Yes, we have one : this is not allowed, it should have been 120 // escaped. 121 return null; 122 } 123 124 // This is an UID if the '#' is immediatly 125 // followed by a BitString, except if the '#' is 126 // on the last position 127 String uid = value.substring( sharpPos + 1 ); 128 129 if ( sharpPos > 0 ) 130 { 131 Dn dn = new Dn( schemaManager, value.substring( 0, sharpPos ) ); 132 133 return dn.getNormName() + '#' + uid; 134 } 135 else 136 { 137 throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) ); 138 } 139 } 140 else 141 { 142 // No UID, the strValue is a Dn 143 // Return the normalized Dn 144 return new Dn( schemaManager, value ).getNormName(); 145 } 146 } 147 148 149 /** 150 * {@inheritDoc} 151 */ 152 @Override 153 public void setSchemaManager( SchemaManager schemaManager ) 154 { 155 this.schemaManager = schemaManager; 156 } 157}