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.exception.LdapOtherException;
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.schema.syntaxCheckers.NumericOidSyntaxChecker;
032    
033    
034    /**
035     * A name or numeric id normalizer.  Needs an OID registry to operate properly.
036     * The OID registry is injected into this class after instantiation if a 
037     * setRegistries(Registries) method is exposed.
038     * 
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$
042     */
043    public class NameOrNumericIdNormalizer extends Normalizer
044    {
045        /** The serial UID */
046        public static final long serialVersionUID = 1L;
047    
048        private NumericOidSyntaxChecker checker = new NumericOidSyntaxChecker();
049    
050        /** A reference to the schema manager used to normalize the Name */
051        private SchemaManager schemaManager;
052    
053        /** A static instance of this normalizer */
054        public static final NameOrNumericIdNormalizer INSTANCE = new NameOrNumericIdNormalizer();
055    
056    
057        /**
058         * Creates a new instance of GeneralizedTimeNormalizer.
059         */
060        public NameOrNumericIdNormalizer()
061        {
062            super( SchemaConstants.NAME_OR_NUMERIC_ID_MATCH_OID );
063        }
064    
065    
066        /**
067         * {@inheritDoc} 
068         */
069        public Value<?> normalize( Value<?> value ) throws LdapException
070        {
071            if ( value == null )
072            {
073                return null;
074            }
075    
076            String strValue = value.getString();
077    
078            if ( strValue.length() == 0 )
079            {
080                return new StringValue( "" );
081            }
082    
083            // if value is a numeric id then return it as is
084            if ( checker.isValidSyntax( strValue ) )
085            {
086                return value;
087            }
088    
089            // if it is a name we need to do a lookup
090            String oid = schemaManager.getRegistries().getOid( strValue );
091    
092            if ( oid != null )
093            {
094                return new StringValue( oid );
095            }
096    
097            // if all else fails
098            throw new LdapOtherException( I18n.err( I18n.ERR_04225, value ) );
099        }
100    
101    
102        /**
103         * {@inheritDoc} 
104         */
105        public String normalize( String value ) throws LdapException
106        {
107            if ( value == null )
108            {
109                return null;
110            }
111    
112            if ( value.length() == 0 )
113            {
114                return value;
115            }
116    
117            // if value is a numeric id then return it as is
118            if ( checker.isValidSyntax( value ) )
119            {
120                return value;
121            }
122    
123            // if it is a name we need to do a lookup
124            String oid = schemaManager.getRegistries().getOid( value );
125    
126            if ( oid != null )
127            {
128                return oid;
129            }
130    
131            // if all else fails
132            throw new LdapOtherException( I18n.err( I18n.ERR_04226, value ) );
133        }
134    
135    
136        /**
137         * {@inheritDoc}
138         */
139        public void setSchemaManager( SchemaManager schemaManager )
140        {
141            this.schemaManager = schemaManager;
142        }
143    }