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 java.util.regex.Matcher;
024import java.util.regex.Pattern;
025
026import org.apache.directory.api.ldap.model.constants.SchemaConstants;
027import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
028import org.apache.directory.api.util.Strings;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032
033/**
034 * A SyntaxChecker which verifies that a name is valid for an ObjectClass
035 * or an AttributeType<br/><br/>
036 * 
037 * &lt;m-name&gt; = &lt;keystring&gt; <br/>
038 * &lt;keystring&gt; = &lt;leadkeychar&gt; *&lt;keychar&gt;<br/>
039 * &lt;leadkeychar&gt; = &lt;ALPHA&gt;<br/>
040 * &lt;keychar&gt; = &lt;ALPHA&gt; / &lt;DIGIT&gt; / &lt;HYPHEN&gt; / &lt;SEMI&gt;<br/>
041 * &lt;ALPHA&gt;   = %x41-5A / %x61-7A   ; "A"-"Z" / "a"-"z"<br/>
042 * &lt;DIGIT&gt;   = %x30 / &lt;LDIGIT       ; "0"-"9"<br/>
043 * &lt;LDIGIT&gt;  = %x31-39             ; "1"-"9"<br/>
044 * &lt;HYPHEN&gt;  = %x2D ; hyphen ("-")<br/>
045 * &lt;SEMI&gt;    = %x3B ; semicolon (";")<br/>
046 * 
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049@SuppressWarnings("serial")
050public class ObjectNameSyntaxChecker extends SyntaxChecker
051{
052    /** A logger for this class */
053    private static final Logger LOG = LoggerFactory.getLogger( ObjectNameSyntaxChecker.class );
054
055    private static final String REGEXP = "^([a-zA-Z][a-zA-Z0-9-;]*)$";
056
057    private static final Pattern PATTERN = Pattern.compile( REGEXP );
058
059
060    /**
061     * Creates a new instance of ObjectNameSyntaxChecker.
062     */
063    public ObjectNameSyntaxChecker()
064    {
065        super( SchemaConstants.OBJECT_NAME_SYNTAX );
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public boolean isValidSyntax( Object value )
073    {
074        String strValue = null;
075
076        if ( value == null )
077        {
078            LOG.debug( "Syntax invalid for 'null'" );
079            return false;
080        }
081
082        if ( value instanceof String )
083        {
084            strValue = ( String ) value;
085        }
086        else if ( value instanceof byte[] )
087        {
088            strValue = Strings.utf8ToString( ( byte[] ) value );
089        }
090        else
091        {
092            strValue = value.toString();
093        }
094
095        if ( strValue.length() == 0 )
096        {
097            LOG.debug( "Syntax invalid for '{}'", value );
098            return false;
099        }
100
101        // Search for the '$' separator
102        Matcher match = PATTERN.matcher( strValue );
103
104        boolean result = match.matches();
105
106        if ( result )
107        {
108            LOG.debug( "Syntax valid for '{}'", value );
109        }
110        else
111        {
112            LOG.debug( "Syntax invalid for '{}'", value );
113        }
114
115        return result;
116    }
117}