001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.schema.syntaxes;
021
022
023 import java.util.HashSet;
024 import java.util.Set;
025
026 import org.apache.directory.shared.ldap.constants.SchemaConstants;
027 import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
028 import org.apache.directory.shared.ldap.util.StringTools;
029
030
031 /**
032 * A SyntaxChecker which verifies that a value is a country according to RFC 4517.
033 *
034 * From RFC 4517 :
035 *
036 * A value of the Country String syntax is one of the two-character
037 * codes from ISO 3166 [ISO3166] for representing a country.
038 *
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 * @version $Rev$
042 */
043 public class CountrySyntaxChecker extends AbstractSyntaxChecker
044 {
045 /** The ISO 3166 list of countries, as of 2006 */
046 private static final String[] COUNTRY_ISO_3166 =
047 {
048 "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ",
049 "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ",
050 "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CX", "CY", "CZ",
051 "DE", "DJ", "DK", "DM", "DO", "DZ",
052 "EC", "EE", "EG", "EH", "ER", "ES", "ET",
053 "FI", "FJ", "FK", "FM", "FO", "FR",
054 "GA", "GB", "GD", "GE", "GG", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY",
055 "HK", "HM", "HN", "HR", "HT", "HU",
056 "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT",
057 "JE", "JM", "JO", "JP",
058 "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ",
059 "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY",
060 "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX",
061 "MY", "MZ",
062 "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ",
063 "OM",
064 "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
065 "QA",
066 "RE", "RO", "RS", "RU", "RW",
067 "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ",
068 "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ",
069 "UA", "UG", "UM", "US", "UY", "UZ",
070 "VA", "VC", "VE", "VG", "VI", "VN", "VU",
071 "WF", "WS",
072 "YE", "YT",
073 "ZA", "ZM", "ZW"
074 };
075
076 /** The Set which contains the countries */
077 private static final Set<String> COUNTRIES = new HashSet<String>();
078
079 /** Initialization of the country set */
080 static
081 {
082 for ( String country:COUNTRY_ISO_3166 )
083 {
084 COUNTRIES.add( country );
085 }
086 }
087
088 /**
089 *
090 * Creates a new instance of CountrySyntaxChecker.
091 *
092 */
093 public CountrySyntaxChecker()
094 {
095 super( SchemaConstants.COUNTRY_STRING_SYNTAX );
096 }
097
098
099 /**
100 *
101 * Creates a new instance of CountrySyntaxChecker.
102 *
103 * @param oid the oid to associate with this new SyntaxChecker
104 *
105 */
106 protected CountrySyntaxChecker( String oid )
107 {
108 super( oid );
109 }
110
111 /* (non-Javadoc)
112 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
113 */
114 public boolean isValidSyntax( Object value )
115 {
116 String strValue = null;
117
118 if ( value == null )
119 {
120 return false;
121 }
122
123 if ( value instanceof String )
124 {
125 strValue = ( String ) value;
126 }
127 else if ( value instanceof byte[] )
128 {
129 strValue = StringTools.utf8ToString( ( byte[] ) value );
130 }
131 else
132 {
133 strValue = value.toString();
134 }
135
136 if ( strValue.length() == 0 )
137 {
138 return false;
139 }
140
141 return COUNTRIES.contains( StringTools.toUpperCase( strValue ) );
142 }
143 }