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.HashSet; 024import java.util.Set; 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.Chars; 029import org.apache.directory.api.util.Strings; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033 034/** 035 * A SyntaxChecker which verifies that a value is a DSEType according to 036 * http://tools.ietf.org/id/draft-ietf-asid-ldapv3-attributes-03.txt, par 6.2.1.5 : 037 * 038 * <DSEType> ::= '(' <sp>* <DSEBit> <sp>* <DSEBitList> ')' 039 * <DSEBitList> ::= '$' <sp>* <DSEBit> <sp>* <DSEBitList> | e 040 * <DSEBit> ::= 'root' | 'glue' | 'cp' | 'entry' | 'alias' | 'subr' | 041 * 'nssr' | 'supr' | 'xr' | 'admPoint' | 'subentry' | 042 * 'shadow' | 'zombie' | 'immSupr' | 'rhob' | 'sa' 043 * 044 * 045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 046 */ 047@SuppressWarnings("serial") 048public class DseTypeSyntaxChecker extends SyntaxChecker 049{ 050 /** A logger for this class */ 051 private static final Logger LOG = LoggerFactory.getLogger( DseTypeSyntaxChecker.class ); 052 053 /** The DSE BITS keywords */ 054 private static final String[] DSE_BITS_STRINGS = 055 { 056 "root", "glue", "cp", "entry", "alias", "subr", 057 "nssr", "supr", "xr", "admPoint", "subentry", 058 "shadow", "zombie", "immSupr", "rhob", "sa" 059 }; 060 061 /** The Set which contains the DESBits */ 062 private static final Set<String> DSE_BITS = new HashSet<String>(); 063 064 /** Initialization of the country set */ 065 static 066 { 067 for ( String country : DSE_BITS_STRINGS ) 068 { 069 DSE_BITS.add( country ); 070 } 071 } 072 073 074 /** 075 * 076 * Creates a new instance of DSETypeSyntaxChecker. 077 * 078 */ 079 public DseTypeSyntaxChecker() 080 { 081 super( SchemaConstants.DSE_TYPE_SYNTAX ); 082 } 083 084 085 /** 086 * {@inheritDoc} 087 */ 088 public boolean isValidSyntax( Object value ) 089 { 090 String strValue = null; 091 092 if ( value == null ) 093 { 094 LOG.debug( "Syntax invalid for 'null'" ); 095 return false; 096 } 097 098 if ( value instanceof String ) 099 { 100 strValue = ( String ) value; 101 } 102 else if ( value instanceof byte[] ) 103 { 104 strValue = Strings.utf8ToString( ( byte[] ) value ); 105 } 106 else 107 { 108 strValue = value.toString(); 109 } 110 111 // We must have at least '(cp)', '(xr)' or '(ca)' 112 if ( strValue.length() < 4 ) 113 { 114 LOG.debug( "Syntax invalid for '{}'", value ); 115 return false; 116 } 117 118 // Check the opening and closing parenthesis 119 if ( ( strValue.charAt( 0 ) != '(' ) 120 || ( strValue.charAt( strValue.length() - 1 ) != ')' ) ) 121 { 122 LOG.debug( "Syntax invalid for '{}'", value ); 123 return false; 124 } 125 126 Set<String> keywords = new HashSet<String>(); 127 int len = strValue.length() - 1; 128 boolean needKeyword = true; 129 130 // 131 for ( int i = 1; i < len; /* */) 132 { 133 // Skip spaces 134 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) ) 135 { 136 i++; 137 } 138 139 int pos = i; 140 141 // Search for a keyword 142 while ( ( i < len ) && Chars.isAlphaASCII( strValue, pos ) ) 143 { 144 pos++; 145 } 146 147 if ( pos == i ) 148 { 149 // No keyword : error 150 LOG.debug( "Syntax invalid for '{}'", value ); 151 return false; 152 } 153 154 String keyword = strValue.substring( i, pos ); 155 i = pos; 156 157 if ( !DSE_BITS.contains( keyword ) ) 158 { 159 // Unknown keyword 160 LOG.debug( "Syntax invalid for '{}'", value ); 161 return false; 162 } 163 164 // Check that the keyword has not been met 165 if ( keywords.contains( keyword ) ) 166 { 167 LOG.debug( "Syntax invalid for '{}'", value ); 168 return false; 169 } 170 171 keywords.add( keyword ); 172 needKeyword = false; 173 174 // Skip spaces 175 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) ) 176 { 177 i++; 178 } 179 180 // Do we have another keyword ? 181 if ( ( i < len ) && ( strValue.charAt( i ) == '$' ) ) 182 { 183 // yes 184 i++; 185 needKeyword = true; 186 continue; 187 } 188 } 189 190 // We are done 191 if ( needKeyword ) 192 { 193 LOG.debug( "Syntax invalid for '{}'", value ); 194 } 195 else 196 { 197 LOG.debug( "Syntax valid for '{}'", value ); 198 } 199 200 return !needKeyword; 201 } 202}