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