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.syntaxCheckers;
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.SyntaxChecker;
028    import org.apache.directory.shared.ldap.util.StringTools;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    
033    /**
034     * A SyntaxChecker which verifies that a value is a country according to RFC 4517.
035     * 
036     * From RFC 4517 :
037     * 
038     * A value of the Country String syntax is one of the two-character
039     * codes from ISO 3166 [ISO3166] for representing a country.
040     * 
041     *
042     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043     * @version $Rev$
044     */
045    public class CountrySyntaxChecker extends SyntaxChecker
046    {
047        /** A logger for this class */
048        private static final Logger LOG = LoggerFactory.getLogger( CountrySyntaxChecker.class );
049    
050        /** The serialVersionUID */
051        private static final long serialVersionUID = 1L;
052    
053        /** The ISO 3166 list of countries, as of 2006 */
054        private static final String[] COUNTRY_ISO_3166 =
055            {
056            "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ",
057            "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ",
058            "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CX", "CY", "CZ",
059            "DE", "DJ", "DK", "DM", "DO", "DZ",
060            "EC", "EE", "EG", "EH", "ER", "ES", "ET",
061            "FI", "FJ", "FK", "FM", "FO", "FR",
062            "GA", "GB", "GD", "GE", "GG", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY",
063            "HK", "HM", "HN", "HR", "HT", "HU",
064            "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT",
065            "JE", "JM", "JO", "JP",
066            "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ",
067            "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY",
068            "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", 
069            "MY", "MZ",
070            "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ",
071            "OM",
072            "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
073            "QA",
074            "RE", "RO", "RS", "RU", "RW", 
075            "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ",
076            "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ",
077            "UA", "UG", "UM", "US", "UY", "UZ",
078            "VA", "VC", "VE", "VG", "VI", "VN", "VU",
079            "WF", "WS",
080            "YE", "YT",
081            "ZA", "ZM", "ZW"
082            };
083        
084        /** The Set which contains the countries */
085        private static final Set<String> COUNTRIES = new HashSet<String>();
086        
087        /** Initialization of the country set */
088        static
089        {
090            for ( String country:COUNTRY_ISO_3166 )
091            {
092                COUNTRIES.add( country );
093            }
094        }
095        
096        /**
097         * 
098         * Creates a new instance of CountrySyntaxChecker.
099         *
100         */
101        public CountrySyntaxChecker()
102        {
103            super( SchemaConstants.COUNTRY_STRING_SYNTAX );
104        }
105        
106        
107        /**
108         * {@inheritDoc}
109         */
110        public boolean isValidSyntax( Object value )
111        {
112            String strValue = null;
113    
114            if ( value == null )
115            {
116                LOG.debug( "Syntax invalid for '{}'", value );
117                return false;
118            }
119            
120            if ( value instanceof String )
121            {
122                strValue = ( String ) value;
123            }
124            else if ( value instanceof byte[] )
125            {
126                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
127            }
128            else
129            {
130                strValue = value.toString();
131            }
132    
133            if ( strValue.length() == 0 )
134            {
135                LOG.debug( "Syntax invalid for '{}'", value );
136                return false;
137            }
138            
139            boolean result = COUNTRIES.contains( StringTools.toUpperCase( strValue ) );
140            
141            if ( result )
142            {
143                LOG.debug( "Syntax valid for '{}'", value );
144            }
145            else
146            {
147                LOG.debug( "Syntax invalid for '{}'", value );
148            }
149        
150        return result;
151        }
152    }