001 /* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2006-2008 Sun Microsystems, Inc. 026 */ 027 package org.opends.server.schema; 028 029 030 031 import java.util.List; 032 033 import org.opends.server.admin.std.server.SubstringMatchingRuleCfg; 034 import org.opends.server.api.SubstringMatchingRule; 035 import org.opends.server.config.ConfigException; 036 import org.opends.server.protocols.asn1.ASN1OctetString; 037 import org.opends.server.types.ByteString; 038 import org.opends.server.types.DirectoryException; 039 import org.opends.server.types.InitializationException; 040 041 import static org.opends.server.schema.SchemaConstants.*; 042 043 044 045 /** 046 * This class defines the caseExactSubstringsMatch matching rule defined in 047 * X.520 and referenced in RFC 2252. 048 */ 049 public class CaseExactSubstringMatchingRule 050 extends SubstringMatchingRule 051 { 052 /** 053 * Creates a new instance of this caseExactSubstringsMatch matching rule. 054 */ 055 public CaseExactSubstringMatchingRule() 056 { 057 super(); 058 } 059 060 061 062 /** 063 * {@inheritDoc} 064 */ 065 public void initializeMatchingRule(SubstringMatchingRuleCfg configuration) 066 throws ConfigException, InitializationException 067 { 068 // No initialization is required. 069 } 070 071 072 073 /** 074 * Retrieves the common name for this matching rule. 075 * 076 * @return The common name for this matching rule, or <CODE>null</CODE> if 077 * it does not have a name. 078 */ 079 public String getName() 080 { 081 return SMR_CASE_EXACT_NAME; 082 } 083 084 085 086 /** 087 * Retrieves the OID for this matching rule. 088 * 089 * @return The OID for this matching rule. 090 */ 091 public String getOID() 092 { 093 return SMR_CASE_EXACT_OID; 094 } 095 096 097 098 /** 099 * Retrieves the description for this matching rule. 100 * 101 * @return The description for this matching rule, or <CODE>null</CODE> if 102 * there is none. 103 */ 104 public String getDescription() 105 { 106 // There is no standard description for this matching rule. 107 return null; 108 } 109 110 111 112 /** 113 * Retrieves the OID of the syntax with which this matching rule is 114 * associated. 115 * 116 * @return The OID of the syntax with which this matching rule is associated. 117 */ 118 public String getSyntaxOID() 119 { 120 return SYNTAX_SUBSTRING_ASSERTION_OID; 121 } 122 123 124 125 /** 126 * Retrieves the normalized form of the provided value, which is best suited 127 * for efficiently performing matching operations on that value. 128 * 129 * @param value The value to be normalized. 130 * 131 * @return The normalized version of the provided value. 132 * 133 * @throws DirectoryException If the provided value is invalid according to 134 * the associated attribute syntax. 135 */ 136 public ByteString normalizeValue(ByteString value) 137 throws DirectoryException 138 { 139 StringBuilder buffer = new StringBuilder(); 140 buffer.append(value.stringValue().trim()); 141 142 int bufferLength = buffer.length(); 143 if (bufferLength == 0) 144 { 145 if (value.value().length > 0) 146 { 147 // This should only happen if the value is composed entirely of spaces. 148 // In that case, the normalized value is a single space. 149 return new ASN1OctetString(" "); 150 } 151 else 152 { 153 // The value is empty, so it is already normalized. 154 return new ASN1OctetString(); 155 } 156 } 157 158 159 // Replace any consecutive spaces with a single space. 160 for (int pos = bufferLength-1; pos > 0; pos--) 161 { 162 if (buffer.charAt(pos) == ' ') 163 { 164 if (buffer.charAt(pos-1) == ' ') 165 { 166 buffer.delete(pos, pos+1); 167 } 168 } 169 } 170 171 return new ASN1OctetString(buffer.toString()); 172 } 173 174 175 176 /** 177 * Normalizes the provided value fragment into a form that can be used to 178 * efficiently compare values. 179 * 180 * @param substring The value fragment to be normalized. 181 * 182 * @return The normalized form of the value fragment. 183 * 184 * @throws DirectoryException If the provided value fragment is not 185 * acceptable according to the associated syntax. 186 */ 187 public ByteString normalizeSubstring(ByteString substring) 188 throws DirectoryException 189 { 190 // In this case, the process for normalizing a substring is the same as 191 // normalizing a full value with the exception that it may include an 192 // opening or trailing space. 193 StringBuilder buffer = new StringBuilder(); 194 buffer.append(substring.stringValue()); 195 196 int bufferLength = buffer.length(); 197 if (bufferLength == 0) 198 { 199 if (substring.value().length > 0) 200 { 201 // This should only happen if the value is composed entirely of spaces. 202 // In that case, the normalized value is a single space. 203 return new ASN1OctetString(" "); 204 } 205 else 206 { 207 // The value is empty, so it is already normalized. 208 return substring; 209 } 210 } 211 212 213 // Replace any consecutive spaces with a single space. 214 for (int pos = bufferLength-1; pos > 0; pos--) 215 { 216 if (buffer.charAt(pos) == ' ') 217 { 218 if (buffer.charAt(pos-1) == ' ') 219 { 220 buffer.delete(pos, pos+1); 221 } 222 } 223 } 224 225 return new ASN1OctetString(buffer.toString()); 226 } 227 228 229 230 /** 231 * Determines whether the provided value matches the given substring filter 232 * components. Note that any of the substring filter components may be 233 * <CODE>null</CODE> but at least one of them must be non-<CODE>null</CODE>. 234 * 235 * @param value The normalized value against which to compare the 236 * substring components. 237 * @param subInitial The normalized substring value fragment that should 238 * appear at the beginning of the target value. 239 * @param subAnyElements The normalized substring value fragments that 240 * should appear in the middle of the target value. 241 * @param subFinal The normalized substring value fragment that should 242 * appear at the end of the target value. 243 * 244 * @return <CODE>true</CODE> if the provided value does match the given 245 * substring components, or <CODE>false</CODE> if not. 246 */ 247 public boolean valueMatchesSubstring(ByteString value, ByteString subInitial, 248 List<ByteString> subAnyElements, 249 ByteString subFinal) 250 { 251 byte[] valueBytes = value.value(); 252 int valueLength = valueBytes.length; 253 254 int pos = 0; 255 if (subInitial != null) 256 { 257 byte[] initialBytes = subInitial.value(); 258 int initialLength = initialBytes.length; 259 if (initialLength > valueLength) 260 { 261 return false; 262 } 263 264 for (; pos < initialLength; pos++) 265 { 266 if (initialBytes[pos] != valueBytes[pos]) 267 { 268 return false; 269 } 270 } 271 } 272 273 274 if ((subAnyElements != null) && (! subAnyElements.isEmpty())) 275 { 276 for (ByteString element : subAnyElements) 277 { 278 byte[] anyBytes = element.value(); 279 int anyLength = anyBytes.length; 280 281 int end = valueLength - anyLength; 282 boolean match = false; 283 for (; pos <= end; pos++) 284 { 285 if (anyBytes[0] == valueBytes[pos]) 286 { 287 boolean subMatch = true; 288 for (int i=1; i < anyLength; i++) 289 { 290 if (anyBytes[i] != valueBytes[pos+i]) 291 { 292 subMatch = false; 293 break; 294 } 295 } 296 297 if (subMatch) 298 { 299 match = subMatch; 300 break; 301 } 302 } 303 } 304 305 if (match) 306 { 307 pos += anyLength; 308 } 309 else 310 { 311 return false; 312 } 313 } 314 } 315 316 317 if (subFinal != null) 318 { 319 byte[] finalBytes = subFinal.value(); 320 int finalLength = finalBytes.length; 321 322 if ((valueLength - finalLength) < pos) 323 { 324 return false; 325 } 326 327 pos = valueLength - finalLength; 328 for (int i=0; i < finalLength; i++,pos++) 329 { 330 if (finalBytes[i] != valueBytes[pos]) 331 { 332 return false; 333 } 334 } 335 } 336 337 338 return true; 339 } 340 } 341