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