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.HashSet;
24 import java.util.Set;
25
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.Chars;
29 import org.apache.directory.api.util.Strings;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 @SuppressWarnings("serial")
53 public class DeliveryMethodSyntaxChecker extends SyntaxChecker
54 {
55
56 private static final Logger LOG = LoggerFactory.getLogger( DeliveryMethodSyntaxChecker.class );
57
58 private static final String[] PDMS =
59 {
60 "any", "mhs", "physical", "telex", "teletex",
61 "g3fax", "g4fax", "ia5", "videotex", "telephone"
62 };
63
64
65 private static final Set<String> DELIVERY_METHODS = new HashSet<String>();
66
67
68 static
69 {
70 for ( String country : PDMS )
71 {
72 DELIVERY_METHODS.add( country );
73 }
74 }
75
76
77
78
79
80
81
82 public DeliveryMethodSyntaxChecker()
83 {
84 super( SchemaConstants.DELIVERY_METHOD_SYNTAX );
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99 private int isPdm( String strValue, int start, Set<String> pdms )
100 {
101 int pos = start;
102
103 while ( Chars.isAlphaDigit( strValue, pos ) )
104 {
105 pos++;
106 }
107
108
109 if ( pos == start )
110 {
111 return -1;
112 }
113
114 String pdm = strValue.substring( start, pos );
115
116 if ( !DELIVERY_METHODS.contains( pdm ) )
117 {
118
119 return -1;
120 }
121 else
122 {
123 if ( pdms.contains( pdm ) )
124 {
125
126 return -1;
127 }
128 else
129 {
130 pdms.add( pdm );
131 return pos;
132 }
133 }
134 }
135
136
137
138
139
140 public boolean isValidSyntax( Object value )
141 {
142 String strValue = null;
143
144 if ( value == null )
145 {
146 LOG.debug( "Syntax invalid for 'null'" );
147 return false;
148 }
149
150 if ( value instanceof String )
151 {
152 strValue = ( String ) value;
153 }
154 else if ( value instanceof byte[] )
155 {
156 strValue = Strings.utf8ToString( ( byte[] ) value );
157 }
158 else
159 {
160 strValue = value.toString();
161 }
162
163 if ( strValue.length() == 0 )
164 {
165 LOG.debug( "Syntax invalid for '{}'", value );
166 return false;
167 }
168
169
170 int length = strValue.length();
171 int pos = 0;
172 Set<String> pmds = new HashSet<String>();
173
174 if ( ( pos = isPdm( strValue, pos, pmds ) ) == -1 )
175 {
176 LOG.debug( "Syntax invalid for '{}'", value );
177 return false;
178 }
179
180
181
182
183 while ( pos < length )
184 {
185
186 while ( Strings.isCharASCII( strValue, pos, ' ' ) )
187 {
188 pos++;
189 }
190
191 if ( !Strings.isCharASCII( strValue, pos, '$' ) )
192 {
193
194 LOG.debug( "Syntax invalid for '{}'", value );
195 return false;
196 }
197 else
198 {
199 pos++;
200 }
201
202
203 while ( Strings.isCharASCII( strValue, pos, ' ' ) )
204 {
205 pos++;
206 }
207
208 if ( ( pos = isPdm( strValue, pos, pmds ) ) == -1 )
209 {
210 LOG.debug( "Syntax invalid for '{}'", value );
211 return false;
212 }
213 }
214
215 LOG.debug( "Syntax valid for '{}'", value );
216 return true;
217 }
218 }