View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.normalizers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.entry.StringValue;
26  import org.apache.directory.api.ldap.model.entry.Value;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.exception.LdapOtherException;
29  import org.apache.directory.api.ldap.model.schema.Normalizer;
30  import org.apache.directory.api.ldap.model.schema.SchemaManager;
31  import org.apache.directory.api.ldap.model.schema.syntaxCheckers.NumericOidSyntaxChecker;
32  
33  
34  /**
35   * A name or numeric id normalizer.  Needs an OID registry to operate properly.
36   * The OID registry is injected into this class after instantiation if a 
37   * setRegistries(Registries) method is exposed.
38   * 
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  @SuppressWarnings("serial")
43  public class NameOrNumericIdNormalizer extends Normalizer
44  {
45      private NumericOidSyntaxChecker checker = new NumericOidSyntaxChecker();
46  
47      /** A reference to the schema manager used to normalize the Name */
48      private SchemaManager schemaManager;
49  
50      /** A static instance of this normalizer */
51      public static final NameOrNumericIdNormalizer INSTANCE = new NameOrNumericIdNormalizer();
52  
53  
54      /**
55       * Creates a new instance of GeneralizedTimeNormalizer.
56       */
57      public NameOrNumericIdNormalizer()
58      {
59          super( SchemaConstants.NAME_OR_NUMERIC_ID_MATCH_OID );
60      }
61  
62  
63      /**
64       * {@inheritDoc} 
65       */
66      public Value<?> normalize( Value<?> value ) throws LdapException
67      {
68          if ( value == null )
69          {
70              return null;
71          }
72  
73          String strValue = value.getString();
74  
75          if ( strValue.length() == 0 )
76          {
77              return new StringValue( "" );
78          }
79  
80          // if value is a numeric id then return it as is
81          if ( checker.isValidSyntax( strValue ) )
82          {
83              return value;
84          }
85  
86          // if it is a name we need to do a lookup
87          String oid = schemaManager.getRegistries().getOid( strValue );
88  
89          if ( oid != null )
90          {
91              return new StringValue( oid );
92          }
93  
94          // if all else fails
95          throw new LdapOtherException( I18n.err( I18n.ERR_04225, value ) );
96      }
97  
98  
99      /**
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 }