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.syntaxCheckers;
21  
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
28  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
29  import org.apache.directory.api.util.Strings;
30  
31  
32  /**
33   * A SyntaxChecker which verifies that a value is a country according to RFC 4517.
34   * 
35   * From RFC 4517 :
36   * 
37   * <pre>
38   * A value of the Country String syntax is one of the two-character
39   * codes from ISO 3166 [ISO3166] for representing a country.
40   * </pre>
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  @SuppressWarnings("serial")
45  public final class CountrySyntaxChecker extends SyntaxChecker
46  {
47      /** The ISO 3166 list of countries, as of 2006 */
48      private static final String[] COUNTRY_ISO_3166 =
49          {
50              "AD",
51              "AE",
52              "AF",
53              "AG",
54              "AI",
55              "AL",
56              "AM",
57              "AN",
58              "AO",
59              "AQ",
60              "AR",
61              "AS",
62              "AT",
63              "AU",
64              "AW",
65              "AX",
66              "AZ",
67              "BA",
68              "BB",
69              "BD",
70              "BE",
71              "BF",
72              "BG",
73              "BH",
74              "BI",
75              "BJ",
76              "BM",
77              "BN",
78              "BO",
79              "BR",
80              "BS",
81              "BT",
82              "BV",
83              "BW",
84              "BY",
85              "BZ",
86              "CA",
87              "CC",
88              "CD",
89              "CF",
90              "CG",
91              "CH",
92              "CI",
93              "CK",
94              "CL",
95              "CM",
96              "CN",
97              "CO",
98              "CR",
99              "CU",
100             "CV",
101             "CX",
102             "CY",
103             "CZ",
104             "DE",
105             "DJ",
106             "DK",
107             "DM",
108             "DO",
109             "DZ",
110             "EC",
111             "EE",
112             "EG",
113             "EH",
114             "ER",
115             "ES",
116             "ET",
117             "FI",
118             "FJ",
119             "FK",
120             "FM",
121             "FO",
122             "FR",
123             "GA",
124             "GB",
125             "GD",
126             "GE",
127             "GG",
128             "GF",
129             "GH",
130             "GI",
131             "GL",
132             "GM",
133             "GN",
134             "GP",
135             "GQ",
136             "GR",
137             "GS",
138             "GT",
139             "GU",
140             "GW",
141             "GY",
142             "HK",
143             "HM",
144             "HN",
145             "HR",
146             "HT",
147             "HU",
148             "ID",
149             "IE",
150             "IL",
151             "IM",
152             "IN",
153             "IO",
154             "IQ",
155             "IR",
156             "IS",
157             "IT",
158             "JE",
159             "JM",
160             "JO",
161             "JP",
162             "KE",
163             "KG",
164             "KH",
165             "KI",
166             "KM",
167             "KN",
168             "KP",
169             "KR",
170             "KW",
171             "KY",
172             "KZ",
173             "LA",
174             "LB",
175             "LC",
176             "LI",
177             "LK",
178             "LR",
179             "LS",
180             "LT",
181             "LU",
182             "LV",
183             "LY",
184             "MA",
185             "MC",
186             "MD",
187             "ME",
188             "MG",
189             "MH",
190             "MK",
191             "ML",
192             "MM",
193             "MN",
194             "MO",
195             "MP",
196             "MQ",
197             "MR",
198             "MS",
199             "MT",
200             "MU",
201             "MV",
202             "MW",
203             "MX",
204             "MY",
205             "MZ",
206             "NA",
207             "NC",
208             "NE",
209             "NF",
210             "NG",
211             "NI",
212             "NL",
213             "NO",
214             "NP",
215             "NR",
216             "NU",
217             "NZ",
218             "OM",
219             "PA",
220             "PE",
221             "PF",
222             "PG",
223             "PH",
224             "PK",
225             "PL",
226             "PM",
227             "PN",
228             "PR",
229             "PS",
230             "PT",
231             "PW",
232             "PY",
233             "QA",
234             "RE",
235             "RO",
236             "RS",
237             "RU",
238             "RW",
239             "SA",
240             "SB",
241             "SC",
242             "SD",
243             "SE",
244             "SG",
245             "SH",
246             "SI",
247             "SJ",
248             "SK",
249             "SL",
250             "SM",
251             "SN",
252             "SO",
253             "SR",
254             "ST",
255             "SV",
256             "SY",
257             "SZ",
258             "TC",
259             "TD",
260             "TF",
261             "TG",
262             "TH",
263             "TJ",
264             "TK",
265             "TL",
266             "TM",
267             "TN",
268             "TO",
269             "TR",
270             "TT",
271             "TV",
272             "TW",
273             "TZ",
274             "UA",
275             "UG",
276             "UM",
277             "US",
278             "UY",
279             "UZ",
280             "VA",
281             "VC",
282             "VE",
283             "VG",
284             "VI",
285             "VN",
286             "VU",
287             "WF",
288             "WS",
289             "YE",
290             "YT",
291             "ZA",
292             "ZM",
293             "ZW"
294     };
295 
296     /** The Set which contains the countries */
297     private static final Set<String> COUNTRIES = new HashSet<>();
298 
299     /** Initialization of the country set */
300     static
301     {
302         for ( String country : COUNTRY_ISO_3166 )
303         {
304             COUNTRIES.add( country );
305         }
306     }
307     
308     /**
309      * A static instance of CountrySyntaxChecker
310      */
311     public static final CountrySyntaxChecker INSTANCE = new CountrySyntaxChecker( SchemaConstants.COUNTRY_STRING_SYNTAX );
312 
313     /**
314      * A static Builder for this class
315      */
316     public static final class Builder extends SCBuilder<CountrySyntaxChecker>
317     {
318         /**
319          * The Builder constructor
320          */
321         private Builder()
322         {
323             super( SchemaConstants.COUNTRY_STRING_SYNTAX );
324         }
325         
326         
327         /**
328          * Create a new instance of CountrySyntaxChecker
329          * @return A new instance of CountrySyntaxChecker
330          */
331         @Override
332         public CountrySyntaxChecker build()
333         {
334             return new CountrySyntaxChecker( oid );
335         }
336     }
337 
338 
339     /**
340      * Creates a new instance of CountrySyntaxChecker.
341      *
342      * @param oid The OID to use for this SyntaxChecker
343      */
344     private CountrySyntaxChecker( String oid )
345     {
346         super( oid );
347     }
348 
349     
350     /**
351      * @return An instance of the Builder for this class
352      */
353     public static Builder builder()
354     {
355         return new Builder();
356     }
357 
358 
359     /**
360      * {@inheritDoc}
361      */
362     @Override
363     public boolean isValidSyntax( Object value )
364     {
365         String strValue;
366 
367         if ( value == null )
368         {
369             if ( LOG.isDebugEnabled() )
370             {
371                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
372             }
373             
374             return false;
375         }
376 
377         if ( value instanceof String )
378         {
379             strValue = ( String ) value;
380         }
381         else if ( value instanceof byte[] )
382         {
383             strValue = Strings.utf8ToString( ( byte[] ) value );
384         }
385         else
386         {
387             strValue = value.toString();
388         }
389 
390         if ( strValue.length() == 0 )
391         {
392             if ( LOG.isDebugEnabled() )
393             {
394                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
395             }
396             
397             return false;
398         }
399 
400         boolean result = COUNTRIES.contains( Strings.toUpperCaseAscii( strValue ) );
401 
402         if ( LOG.isDebugEnabled() )
403         {
404             if ( result )
405             {
406                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
407             }
408             else
409             {
410                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
411             }
412         }
413 
414         return result;
415     }
416 }