1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.bootstrap;
21
22
23 import javax.naming.NamingException;
24
25 import org.apache.directory.server.schema.registries.OidRegistry;
26 import org.apache.directory.server.schema.registries.Registries;
27 import org.apache.directory.shared.ldap.exception.LdapNamingException;
28 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
29 import org.apache.directory.shared.ldap.schema.Normalizer;
30 import org.apache.directory.shared.ldap.schema.syntax.NumericOidSyntaxChecker;
31 import org.apache.directory.shared.ldap.util.StringTools;
32
33
34
35
36
37
38
39
40
41
42
43 public class NameOrNumericIdNormalizer implements Normalizer
44 {
45 private static final long serialVersionUID = 1L;
46 private NumericOidSyntaxChecker checker = new NumericOidSyntaxChecker();
47 private transient OidRegistry registry;
48
49
50 public NameOrNumericIdNormalizer( OidRegistry registry )
51 {
52 this.registry = registry;
53 }
54
55
56 public NameOrNumericIdNormalizer()
57 {
58 }
59
60
61
62
63
64 public Object normalize( Object value ) throws NamingException
65 {
66 String strValue;
67
68 if ( value == null )
69 {
70 return null;
71 }
72
73 if ( value instanceof String )
74 {
75 strValue = ( String ) value;
76 }
77 else if ( value instanceof byte[] )
78 {
79 strValue = StringTools.utf8ToString( ( byte[] ) value );
80 }
81 else
82 {
83 strValue = value.toString();
84 }
85
86 if ( strValue.length() == 0 )
87 {
88 return "";
89 }
90
91
92 if ( checker.isValidSyntax( strValue ) )
93 {
94 return value;
95 }
96
97
98 if ( registry.hasOid( strValue ) )
99 {
100 return registry.getOid( strValue );
101 }
102
103
104 throw new LdapNamingException( "Encountered name based id of " + value
105 + " which was not found in the OID registry" , ResultCodeEnum.OTHER );
106 }
107
108
109 public void setRegistries( Registries registries )
110 {
111 this.registry = registries.getOidRegistry();
112 }
113 }