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.exception.LdapException;
026import org.apache.directory.api.ldap.model.exception.LdapOtherException;
027import org.apache.directory.api.ldap.model.schema.Normalizer;
028import org.apache.directory.api.ldap.model.schema.PrepareString;
029import org.apache.directory.api.ldap.model.schema.SchemaManager;
030import org.apache.directory.api.ldap.model.schema.syntaxCheckers.NumericOidSyntaxChecker;
031import org.apache.directory.api.util.Strings;
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 * setSchemaManager(SchemaManager) method is exposed.
038 * 
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042@SuppressWarnings("serial")
043public class NameOrNumericIdNormalizer extends Normalizer
044{
045    private NumericOidSyntaxChecker checker = NumericOidSyntaxChecker.INSTANCE;
046
047    /** A reference to the schema manager used to normalize the Name */
048    private transient SchemaManager schemaManager;
049
050    /** A static instance of this normalizer */
051    public static final NameOrNumericIdNormalizer INSTANCE = new NameOrNumericIdNormalizer();
052
053
054    /**
055     * Creates a new instance of GeneralizedTimeNormalizer.
056     */
057    public NameOrNumericIdNormalizer()
058    {
059        super( SchemaConstants.NAME_OR_NUMERIC_ID_MATCH_OID );
060    }
061
062
063    /**
064     * {@inheritDoc} 
065     */
066    @Override
067    public String normalize( String value ) throws LdapException
068    {
069        return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
070    }
071
072
073    /**
074     * {@inheritDoc} 
075     */
076    @Override
077    public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
078    {
079        if ( Strings.isEmpty( value ) )
080        {
081            return value;
082        }
083
084        // if value is a numeric id then return it as is
085        if ( checker.isValidSyntax( value ) )
086        {
087            return value;
088        }
089
090        // if it is a name we need to do a lookup
091        String oid = schemaManager.getRegistries().getOid( value );
092
093        if ( oid != null )
094        {
095            return oid;
096        }
097
098        // if all else fails
099        throw new LdapOtherException( I18n.err( I18n.ERR_13725_CANNOT_HANDLE_NAME_AND_OPTIONAL_UID_NORM, value ) );
100    }
101
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public void setSchemaManager( SchemaManager schemaManager )
108    {
109        this.schemaManager = schemaManager;
110    }
111}