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.name.DN;
025    import org.apache.directory.shared.ldap.schema.SyntaxChecker;
026    import org.apache.directory.shared.ldap.util.StringTools;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    
031    /**
032     * A SyntaxChecker which verifies that a value is a valid DN. We just check
033     * that the DN is valid, we don't need to verify each of the RDN syntax.
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev$
037     */
038    public class DNSyntaxChecker extends SyntaxChecker
039    {
040        /** A logger for this class */
041        private static final Logger LOG = LoggerFactory.getLogger( DNSyntaxChecker.class );
042    
043        /** The serialVersionUID */
044        private static final long serialVersionUID = 1L;
045    
046        /**
047         * Creates a new instance of DNSyntaxChecker.
048         */
049        public DNSyntaxChecker()
050        {
051            super( SchemaConstants.DN_SYNTAX );
052        }
053    
054        
055        /**
056         * {@inheritDoc}
057         */
058        public boolean isValidSyntax( Object value )
059        {
060            String strValue = null;
061    
062            if ( value == null )
063            {
064                LOG.debug( "Syntax invalid for '{}'", value );
065                return false;
066            }
067            
068            if ( value instanceof String )
069            {
070                strValue = ( String ) value;
071            }
072            else if ( value instanceof byte[] )
073            {
074                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
075            }
076            else
077            {
078                strValue = value.toString();
079            }
080    
081            if ( strValue.length() == 0 )
082            {
083                // TODO: this should be a false, but for 
084                // some reason, the principal is empty in 
085                // some cases.
086                LOG.debug( "Syntax valid for '{}'", value );
087                return true;
088            }
089            
090            // Check that the value is a valid DN
091            boolean result = DN.isValid( strValue );
092            
093            if ( result )
094            {
095                LOG.debug( "Syntax valid for '{}'", value );
096            }
097            else
098            {
099                LOG.debug( "Syntax invalid for '{}'", value );
100            }
101            
102            return result;
103        }
104    }