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