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 */
020package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.api.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.name.Dn;
025import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
026import org.apache.directory.api.util.Strings;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * A SyntaxChecker which verifies that a value is a valid Name and Optional UID.
033 * 
034 * This element is a composition of two parts : a Dn and an optional UID :
035 * NameAndOptionalUID = distinguishedName [ SHARP BitString ]
036 * 
037 * Both part already have their syntax checkers, so we will just call them
038 * after having splitted the element in two ( if necessary)
039 * 
040 * We just check that the Dn is valid, we don't need to verify each of the Rdn
041 * syntax.
042 * 
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045@SuppressWarnings("serial")
046public class NameAndOptionalUIDSyntaxChecker extends SyntaxChecker
047{
048    /** A logger for this class */
049    private static final Logger LOG = LoggerFactory.getLogger( NameAndOptionalUIDSyntaxChecker.class );
050
051
052    /**
053     * Creates a new instance of NameAndOptionalUIDSyntaxChecker.
054     */
055    public NameAndOptionalUIDSyntaxChecker()
056    {
057        super( SchemaConstants.NAME_AND_OPTIONAL_UID_SYNTAX );
058    }
059
060
061    /**
062     * {@inheritDoc}
063     */
064    public boolean isValidSyntax( Object value )
065    {
066        String strValue = null;
067
068        if ( value == null )
069        {
070            LOG.debug( "Syntax invalid for 'null'" );
071            return false;
072        }
073
074        if ( value instanceof String )
075        {
076            strValue = ( String ) value;
077        }
078        else if ( value instanceof byte[] )
079        {
080            strValue = Strings.utf8ToString( ( byte[] ) value );
081        }
082        else
083        {
084            strValue = value.toString();
085        }
086
087        if ( strValue.length() == 0 )
088        {
089            LOG.debug( "Syntax invalid for '{}'", value );
090            return false;
091        }
092
093        // Let's see if we have an UID part
094        int sharpPos = strValue.lastIndexOf( '#' );
095
096        if ( sharpPos != -1 )
097        {
098            // Now, check that we don't have another '#'
099            if ( strValue.indexOf( '#' ) != sharpPos )
100            {
101                // Yes, we have one : this is not allowed, it should have been
102                // escaped.
103                LOG.debug( "Syntax invalid for '{}'", value );
104                return false;
105            }
106
107            // This is an UID if the '#' is immediatly
108            // followed by a BitString, except if the '#' is
109            // on the last position
110            // We shoould not find a
111            if ( BitStringSyntaxChecker.isValid( strValue.substring( sharpPos + 1 ) )
112                && ( sharpPos < strValue.length() ) )
113            {
114                // Ok, we have a BitString, now check the Dn,
115                // except if the '#' is in first position
116                if ( sharpPos > 0 )
117                {
118                    boolean result = Dn.isValid( strValue.substring( 0, sharpPos ) );
119
120                    if ( result )
121                    {
122                        LOG.debug( "Syntax valid for '{}'", value );
123                    }
124                    else
125                    {
126                        LOG.debug( "Syntax invalid for '{}'", value );
127                    }
128
129                    return result;
130
131                }
132                else
133                {
134                    // The Dn must not be null ?
135                    LOG.debug( "Syntax invalid for '{}'", value );
136                    return false;
137                }
138            }
139            else
140            {
141                // We have found a '#' but no UID part.
142                LOG.debug( "Syntax invalid for '{}'", value );
143                return false;
144            }
145        }
146        else
147        {
148            // No UID, the strValue is a Dn
149            // Check that the value is a valid Dn
150            boolean result = Dn.isValid( strValue );
151
152            if ( result )
153            {
154                LOG.debug( "Syntax valid for '{}'", value );
155            }
156            else
157            {
158                LOG.debug( "Syntax invalid for '{}'", value );
159            }
160
161            return result;
162        }
163    }
164}