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.asn1.util.Oid;
024import org.apache.directory.api.i18n.I18n;
025import org.apache.directory.api.ldap.model.constants.SchemaConstants;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
028import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
029import org.apache.directory.api.ldap.model.schema.Normalizer;
030import org.apache.directory.api.ldap.model.schema.PrepareString;
031import org.apache.directory.api.ldap.model.schema.SchemaManager;
032import org.apache.directory.api.util.Strings;
033
034
035/**
036 * A normalizer for the objectIdentifierMatch matching rule.
037 * 
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040@SuppressWarnings("serial")
041public class ObjectIdentifierNormalizer extends Normalizer
042{
043    /** A reference to the schema manager used to normalize the Name */
044    private transient SchemaManager schemaManager;
045
046    /**
047     * Creates a new instance of ObjectIdentifierNormalizer.
048     */
049    public ObjectIdentifierNormalizer()
050    {
051        super( SchemaConstants.OBJECT_IDENTIFIER_MATCH_MR_OID );
052    }
053
054
055    /**
056     * {@inheritDoc}
057     */
058    @Override
059    public void setSchemaManager( SchemaManager schemaManager )
060    {
061        this.schemaManager = schemaManager;
062    }
063
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public String normalize( String value ) throws LdapException
070    {
071        if ( Strings.isEmpty( value ) )
072        {
073            return "";
074        }
075        
076        String trimmedValue = value.trim();
077        
078        if ( Strings.isEmpty( trimmedValue ) )
079        {
080            return "";
081        }
082
083        String oid = schemaManager.getRegistries().getOid( trimmedValue );
084        
085        if ( oid == null )
086        {
087            // Not found in the schemaManager : keep it as is
088            if ( Oid.isOid( trimmedValue ) )
089            {
090                // It's an numericOid
091                oid = trimmedValue;
092            }
093            else
094            {
095                // It's a descr : ALPHA ( ALPHA | DIGIT | '-' )*
096                for ( int i = 0; i < trimmedValue.length(); i++ )
097                {
098                    char c = trimmedValue.charAt( i );
099                    
100                    if ( i == 0 )
101                    {
102                        if ( !Character.isLetter( c ) )
103                        {
104                            throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
105                                I18n.ERR_13724_INVALID_VALUE, value ) );
106                        }
107                    }
108                    else
109                    {
110                        if ( !( Character.isDigit( c ) || Character.isLetter( c ) || ( c == '-'  ) || ( c == '_' ) ) )
111                            {
112                            throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
113                                I18n.ERR_13724_INVALID_VALUE, value ) );
114                            }
115                    }
116                }
117                
118                oid = trimmedValue;
119            }
120        }
121        
122        return oid;
123    }
124
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
131    {
132        return normalize( value );
133    }
134}