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.shared.ldap.model.schema.syntaxCheckers; 021 022 023import java.util.regex.Matcher; 024import java.util.regex.Pattern; 025 026import org.apache.directory.shared.ldap.model.constants.SchemaConstants; 027import org.apache.directory.shared.ldap.model.schema.SyntaxChecker; 028import org.apache.directory.shared.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 * <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 */ 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 * Creates a new instance of ObjectNameSyntaxChecker. 061 */ 062 public ObjectNameSyntaxChecker() 063 { 064 super( SchemaConstants.OBJECT_NAME_SYNTAX ); 065 } 066 067 068 /** 069 * {@inheritDoc} 070 */ 071 public boolean isValidSyntax( Object value ) 072 { 073 String strValue = null; 074 075 if ( value == null ) 076 { 077 LOG.debug( "Syntax invalid for 'null'" ); 078 return false; 079 } 080 081 if ( value instanceof String ) 082 { 083 strValue = ( String ) value; 084 } 085 else if ( value instanceof byte[] ) 086 { 087 strValue = Strings.utf8ToString((byte[]) value); 088 } 089 else 090 { 091 strValue = value.toString(); 092 } 093 094 if ( strValue.length() == 0 ) 095 { 096 LOG.debug( "Syntax invalid for '{}'", value ); 097 return false; 098 } 099 100 // Search for the '$' separator 101 Matcher match = PATTERN.matcher ( strValue ); 102 103 boolean result = match.matches(); 104 105 if ( result ) 106 { 107 LOG.debug( "Syntax valid for '{}'", value ); 108 } 109 else 110 { 111 LOG.debug( "Syntax invalid for '{}'", value ); 112 } 113 114 return result; 115 } 116}