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