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 = NumericOidSyntaxChecker.INSTANCE; 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 @Override 067 public Value<?> normalize( Value<?> value ) throws LdapException 068 { 069 if ( value == null ) 070 { 071 return null; 072 } 073 074 String strValue = value.getString(); 075 076 if ( strValue.length() == 0 ) 077 { 078 return new StringValue( "" ); 079 } 080 081 // if value is a numeric id then return it as is 082 if ( checker.isValidSyntax( strValue ) ) 083 { 084 return value; 085 } 086 087 // if it is a name we need to do a lookup 088 String oid = schemaManager.getRegistries().getOid( strValue ); 089 090 if ( oid != null ) 091 { 092 return new StringValue( oid ); 093 } 094 095 // if all else fails 096 throw new LdapOtherException( I18n.err( I18n.ERR_04225, value ) ); 097 } 098 099 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public String normalize( String value ) throws LdapException 105 { 106 if ( value == null ) 107 { 108 return null; 109 } 110 111 if ( value.length() == 0 ) 112 { 113 return value; 114 } 115 116 // if value is a numeric id then return it as is 117 if ( checker.isValidSyntax( value ) ) 118 { 119 return value; 120 } 121 122 // if it is a name we need to do a lookup 123 String oid = schemaManager.getRegistries().getOid( value ); 124 125 if ( oid != null ) 126 { 127 return oid; 128 } 129 130 // if all else fails 131 throw new LdapOtherException( I18n.err( I18n.ERR_04226, value ) ); 132 } 133 134 135 /** 136 * {@inheritDoc} 137 */ 138 @Override 139 public void setSchemaManager( SchemaManager schemaManager ) 140 { 141 this.schemaManager = schemaManager; 142 } 143}