1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
39
40
41
42 @SuppressWarnings("serial")
43 public class NameOrNumericIdNormalizer extends Normalizer
44 {
45 private NumericOidSyntaxChecker checker = NumericOidSyntaxChecker.INSTANCE;
46
47
48 private SchemaManager schemaManager;
49
50
51 public static final NameOrNumericIdNormalizer INSTANCE = new NameOrNumericIdNormalizer();
52
53
54
55
56
57 public NameOrNumericIdNormalizer()
58 {
59 super( SchemaConstants.NAME_OR_NUMERIC_ID_MATCH_OID );
60 }
61
62
63
64
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
82 if ( checker.isValidSyntax( strValue ) )
83 {
84 return value;
85 }
86
87
88 String oid = schemaManager.getRegistries().getOid( strValue );
89
90 if ( oid != null )
91 {
92 return new StringValue( oid );
93 }
94
95
96 throw new LdapOtherException( I18n.err( I18n.ERR_04225, value ) );
97 }
98
99
100
101
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
117 if ( checker.isValidSyntax( value ) )
118 {
119 return value;
120 }
121
122
123 String oid = schemaManager.getRegistries().getOid( value );
124
125 if ( oid != null )
126 {
127 return oid;
128 }
129
130
131 throw new LdapOtherException( I18n.err( I18n.ERR_04226, value ) );
132 }
133
134
135
136
137
138 @Override
139 public void setSchemaManager( SchemaManager schemaManager )
140 {
141 this.schemaManager = schemaManager;
142 }
143 }