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