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 org.apache.directory.shared.ldap.constants.SchemaConstants;
024    import org.apache.directory.shared.ldap.schema.SyntaxChecker;
025    import org.apache.directory.shared.ldap.util.StringTools;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    
030    /**
031     * A SyntaxChecker which verifies that a value is a PostalAddress according to 
032     * RFC 4517 :
033     * 
034     * <postal-address> = <dstring> <dstring-list>
035     * <dstring-list> = "$" <dstring> <dstring-list> | e
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$
039     */
040    public class PostalAddressSyntaxChecker extends SyntaxChecker
041    {
042        /** A logger for this class */
043        private static final Logger LOG = LoggerFactory.getLogger( PostalAddressSyntaxChecker.class );
044    
045        /** The serialVersionUID */
046        private static final long serialVersionUID = 1L;
047    
048        /**
049         * Creates a new instance of PostalAddressSyntaxChecker.
050         */
051        public PostalAddressSyntaxChecker()
052        {
053            super( SchemaConstants.POSTAL_ADDRESS_SYNTAX );
054        }
055        
056        
057        /**
058         * {@inheritDoc}
059         */
060        public boolean isValidSyntax( Object value )
061        {
062            String strValue = null;
063    
064            if ( value == null )
065            {
066                LOG.debug( "Syntax invalid for '{}'", value );
067                return false;
068            }
069            
070            if ( value instanceof String )
071            {
072                strValue = ( String ) value;
073            }
074            else if ( value instanceof byte[] )
075            {
076                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
077            }
078            else
079            {
080                strValue = value.toString();
081            }
082    
083            if ( strValue.length() == 0 )
084            {
085                LOG.debug( "Syntax invalid for '{}'", value );
086                return false;
087            }
088    
089            // Search for the '$' separator
090            int dollar = strValue.indexOf( '$' );
091            
092            if ( dollar == -1 )
093            {
094                // No '$' => only a dstring
095                LOG.debug( "Syntax valid for '{}'", value );
096                return true;
097            }
098    
099            int pos = 0;
100            do
101            {
102                // check that the element between each '$' is not empty
103                String address = strValue.substring( pos, dollar );
104                
105                if ( StringTools.isEmpty( address ) )
106                {
107                    LOG.debug( "Syntax invalid for '{}'", value );
108                    return false;
109                }
110                
111                pos = dollar + 1;
112                
113                if ( pos == strValue.length() )
114                {
115                    // we should not have a '$' at the end
116                    LOG.debug( "Syntax invalid for '{}'", value );
117                    return false;
118                }
119                
120                dollar = strValue.indexOf( '$', pos );
121            } while ( dollar > -1 );
122            
123            return true;
124        }
125    }