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.syntaxCheckers;
21
22
23 import java.util.regex.Pattern;
24
25 import org.apache.directory.api.i18n.I18n;
26 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28 import org.apache.directory.api.util.Strings;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @SuppressWarnings("serial")
49 public final class ObjectNameSyntaxChecker extends SyntaxChecker
50 {
51 private static final String REGEXP = "^([a-zA-Z][a-zA-Z0-9-;]*)$";
52
53 private static final Pattern PATTERN = Pattern.compile( REGEXP );
54
55
56
57
58 public static final ObjectNameSyntaxChecker INSTANCE =
59 new ObjectNameSyntaxChecker( SchemaConstants.OBJECT_NAME_SYNTAX );
60
61
62
63
64 public static final class Builder extends SCBuilder<ObjectNameSyntaxChecker>
65 {
66
67
68
69 private Builder()
70 {
71 super( SchemaConstants.OBJECT_NAME_SYNTAX );
72 }
73
74
75
76
77
78
79 @Override
80 public ObjectNameSyntaxChecker build()
81 {
82 return new ObjectNameSyntaxChecker( oid );
83 }
84 }
85
86
87
88
89
90
91
92 private ObjectNameSyntaxChecker( String oid )
93 {
94 super( oid );
95 }
96
97
98
99
100
101 public static Builder builder()
102 {
103 return new Builder();
104 }
105
106
107
108
109
110 @Override
111 public boolean isValidSyntax( Object value )
112 {
113 String strValue;
114
115 if ( value == null )
116 {
117 if ( LOG.isDebugEnabled() )
118 {
119 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
120 }
121
122 return false;
123 }
124
125 if ( value instanceof String )
126 {
127 strValue = ( String ) value;
128 }
129 else if ( value instanceof byte[] )
130 {
131 strValue = Strings.utf8ToString( ( byte[] ) value );
132 }
133 else
134 {
135 strValue = value.toString();
136 }
137
138 if ( strValue.length() == 0 )
139 {
140 if ( LOG.isDebugEnabled() )
141 {
142 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
143 }
144
145 return false;
146 }
147
148
149 boolean result = PATTERN.matcher( strValue ).matches();
150
151 if ( LOG.isDebugEnabled() )
152 {
153 if ( result )
154 {
155 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
156 }
157 else
158 {
159 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
160 }
161 }
162
163 return result;
164 }
165 }