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 = NumericOidSyntaxChecker.INSTANCE;
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      @Override
67      public Value<?> normalize( Value<?> value ) throws LdapException
68      {
69          if ( value == null )
70          {
71              return null;
72          }
73  
74          String strValue = value.getString();
75  
76          if ( strValue.length() == 0 )
77          {
78              return new StringValue( "" );
79          }
80  
81          // if value is a numeric id then return it as is
82          if ( checker.isValidSyntax( strValue ) )
83          {
84              return value;
85          }
86  
87          // if it is a name we need to do a lookup
88          String oid = schemaManager.getRegistries().getOid( strValue );
89  
90          if ( oid != null )
91          {
92              return new StringValue( oid );
93          }
94  
95          // if all else fails
96          throw new LdapOtherException( I18n.err( I18n.ERR_04225, value ) );
97      }
98  
99  
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 }