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.normalizers;
021
022
023 import javax.naming.NamingException;
024
025 import org.apache.directory.shared.i18n.I18n;
026 import org.apache.directory.shared.ldap.constants.SchemaConstants;
027 import org.apache.directory.shared.ldap.entry.Value;
028 import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
029 import org.apache.directory.shared.ldap.name.DN;
030 import org.apache.directory.shared.ldap.schema.Normalizer;
031 import org.apache.directory.shared.ldap.schema.SchemaManager;
032 import org.apache.directory.shared.ldap.util.StringTools;
033
034
035 /**
036 * A noirmalizer for UniqueMember
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 * @version $Rev$
040 */
041 public class UniqueMemberNormalizer extends Normalizer
042 {
043 // The serial UID
044 private static final long serialVersionUID = 1L;
045
046 /** A reference to the schema manager used to normalize the DN */
047 private SchemaManager schemaManager;
048
049
050 public UniqueMemberNormalizer()
051 {
052 super( SchemaConstants.UNIQUE_MEMBER_MATCH_MR_OID );
053 }
054
055
056 public Value<?> normalize( Value<?> value ) throws NamingException
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( nameAndUid.substring( 0, sharpPos ) );
086
087 dn.normalize( schemaManager.getNormalizerMapping() );
088
089 return new ClientStringValue( dn.getNormName() + '#' + uid );
090 }
091 else
092 {
093 throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) );
094 }
095 }
096 else
097 {
098 // No UID, the strValue is a DN
099 // Return the normalized DN
100 return new ClientStringValue( new DN( nameAndUid ).getNormName() );
101 }
102 }
103
104
105 public String normalize( String value ) throws NamingException
106 {
107 if ( StringTools.isEmpty( value ) )
108 {
109 return null;
110 }
111
112 // Let's see if we have an UID part
113 int sharpPos = value.lastIndexOf( '#' );
114
115 if ( sharpPos != -1 )
116 {
117 // Now, check that we don't have another '#'
118 if ( value.indexOf( '#' ) != sharpPos )
119 {
120 // Yes, we have one : this is not allowed, it should have been
121 // escaped.
122 return null;
123 }
124
125 // This is an UID if the '#' is immediatly
126 // followed by a BitString, except if the '#' is
127 // on the last position
128 String uid = value.substring( sharpPos + 1 );
129
130 if ( sharpPos > 0 )
131 {
132 DN dn = new DN( value.substring( 0, sharpPos ) );
133
134 dn.normalize( schemaManager.getNormalizerMapping() );
135
136 return dn.getNormName() + '#' + uid;
137 }
138 else
139 {
140 throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) );
141 }
142 }
143 else
144 {
145 // No UID, the strValue is a DN
146 // Return the normalized DN
147 return new DN( value ).normalize( schemaManager.getNormalizerMapping() ).getNormName();
148 }
149 }
150
151
152 /**
153 * {@inheritDoc}
154 */
155 public void setSchemaManager( SchemaManager schemaManager )
156 {
157 this.schemaManager = schemaManager;
158 }
159 }