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.entry.StringValue;
026import org.apache.directory.api.ldap.model.entry.Value;
027import org.apache.directory.api.ldap.model.exception.LdapException;
028import org.apache.directory.api.ldap.model.exception.LdapOtherException;
029import org.apache.directory.api.ldap.model.schema.Normalizer;
030import org.apache.directory.api.ldap.model.schema.SchemaManager;
031import org.apache.directory.api.ldap.model.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 */
042@SuppressWarnings("serial")
043public class NameOrNumericIdNormalizer extends Normalizer
044{
045    private NumericOidSyntaxChecker checker = new NumericOidSyntaxChecker();
046
047    /** A reference to the schema manager used to normalize the Name */
048    private 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    public Value<?> normalize( Value<?> value ) throws LdapException
067    {
068        if ( value == null )
069        {
070            return null;
071        }
072
073        String strValue = value.getString();
074
075        if ( strValue.length() == 0 )
076        {
077            return new StringValue( "" );
078        }
079
080        // if value is a numeric id then return it as is
081        if ( checker.isValidSyntax( strValue ) )
082        {
083            return value;
084        }
085
086        // if it is a name we need to do a lookup
087        String oid = schemaManager.getRegistries().getOid( strValue );
088
089        if ( oid != null )
090        {
091            return new StringValue( oid );
092        }
093
094        // if all else fails
095        throw new LdapOtherException( I18n.err( I18n.ERR_04225, value ) );
096    }
097
098
099    /**
100     * {@inheritDoc} 
101     */
102    public String normalize( String value ) throws LdapException
103    {
104        if ( value == null )
105        {
106            return null;
107        }
108
109        if ( value.length() == 0 )
110        {
111            return value;
112        }
113
114        // if value is a numeric id then return it as is
115        if ( checker.isValidSyntax( value ) )
116        {
117            return value;
118        }
119
120        // if it is a name we need to do a lookup
121        String oid = schemaManager.getRegistries().getOid( value );
122
123        if ( oid != null )
124        {
125            return oid;
126        }
127
128        // if all else fails
129        throw new LdapOtherException( I18n.err( I18n.ERR_04226, value ) );
130    }
131
132
133    /**
134     * {@inheritDoc}
135     */
136    public void setSchemaManager( SchemaManager schemaManager )
137    {
138        this.schemaManager = schemaManager;
139    }
140}