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 org.opends.server.admin.std.server.AttributeSyntaxCfg; 032 import org.opends.server.api.ApproximateMatchingRule; 033 import org.opends.server.api.AttributeSyntax; 034 import org.opends.server.api.EqualityMatchingRule; 035 import org.opends.server.api.OrderingMatchingRule; 036 import org.opends.server.api.SubstringMatchingRule; 037 import org.opends.server.config.ConfigException; 038 import org.opends.server.core.DirectoryServer; 039 import org.opends.server.types.ByteString; 040 041 042 043 import static org.opends.server.loggers.ErrorLogger.*; 044 import static org.opends.messages.SchemaMessages.*; 045 import org.opends.messages.MessageBuilder; 046 import static org.opends.server.schema.SchemaConstants.*; 047 048 049 /** 050 * This class defines the substring assertion attribute syntax, which contains 051 * one or more substring components, as used in a substring search filter. For 052 * the purposes of matching, it will be treated like a Directory String syntax 053 * except that approximate matching will not be allowed. 054 */ 055 public class SubstringAssertionSyntax 056 extends AttributeSyntax<AttributeSyntaxCfg> 057 { 058 // The default equality matching rule for this syntax. 059 private EqualityMatchingRule defaultEqualityMatchingRule; 060 061 // The default ordering matching rule for this syntax. 062 private OrderingMatchingRule defaultOrderingMatchingRule; 063 064 // The default substring matching rule for this syntax. 065 private SubstringMatchingRule defaultSubstringMatchingRule; 066 067 068 069 /** 070 * Creates a new instance of this syntax. Note that the only thing that 071 * should be done here is to invoke the default constructor for the 072 * superclass. All initialization should be performed in the 073 * <CODE>initializeSyntax</CODE> method. 074 */ 075 public SubstringAssertionSyntax() 076 { 077 super(); 078 } 079 080 081 082 /** 083 * {@inheritDoc} 084 */ 085 public void initializeSyntax(AttributeSyntaxCfg configuration) 086 throws ConfigException 087 { 088 defaultEqualityMatchingRule = 089 DirectoryServer.getEqualityMatchingRule(EMR_CASE_IGNORE_OID); 090 if (defaultEqualityMatchingRule == null) 091 { 092 logError(ERR_ATTR_SYNTAX_UNKNOWN_EQUALITY_MATCHING_RULE.get( 093 EMR_CASE_IGNORE_OID, SYNTAX_SUBSTRING_ASSERTION_NAME)); 094 } 095 096 defaultOrderingMatchingRule = 097 DirectoryServer.getOrderingMatchingRule(OMR_CASE_IGNORE_OID); 098 if (defaultOrderingMatchingRule == null) 099 { 100 logError(ERR_ATTR_SYNTAX_UNKNOWN_ORDERING_MATCHING_RULE.get( 101 OMR_CASE_IGNORE_OID, SYNTAX_SUBSTRING_ASSERTION_NAME)); 102 } 103 104 defaultSubstringMatchingRule = 105 DirectoryServer.getSubstringMatchingRule(SMR_CASE_IGNORE_OID); 106 if (defaultSubstringMatchingRule == null) 107 { 108 logError(ERR_ATTR_SYNTAX_UNKNOWN_SUBSTRING_MATCHING_RULE.get( 109 SMR_CASE_IGNORE_OID, SYNTAX_SUBSTRING_ASSERTION_NAME)); 110 } 111 } 112 113 114 115 /** 116 * Retrieves the common name for this attribute syntax. 117 * 118 * @return The common name for this attribute syntax. 119 */ 120 public String getSyntaxName() 121 { 122 return SYNTAX_SUBSTRING_ASSERTION_NAME; 123 } 124 125 126 127 /** 128 * Retrieves the OID for this attribute syntax. 129 * 130 * @return The OID for this attribute syntax. 131 */ 132 public String getOID() 133 { 134 return SYNTAX_SUBSTRING_ASSERTION_OID; 135 } 136 137 138 139 /** 140 * Retrieves a description for this attribute syntax. 141 * 142 * @return A description for this attribute syntax. 143 */ 144 public String getDescription() 145 { 146 return SYNTAX_SUBSTRING_ASSERTION_DESCRIPTION; 147 } 148 149 150 151 /** 152 * Retrieves the default equality matching rule that will be used for 153 * attributes with this syntax. 154 * 155 * @return The default equality matching rule that will be used for 156 * attributes with this syntax, or <CODE>null</CODE> if equality 157 * matches will not be allowed for this type by default. 158 */ 159 public EqualityMatchingRule getEqualityMatchingRule() 160 { 161 return defaultEqualityMatchingRule; 162 } 163 164 165 166 /** 167 * Retrieves the default ordering matching rule that will be used for 168 * attributes with this syntax. 169 * 170 * @return The default ordering matching rule that will be used for 171 * attributes with this syntax, or <CODE>null</CODE> if ordering 172 * matches will not be allowed for this type by default. 173 */ 174 public OrderingMatchingRule getOrderingMatchingRule() 175 { 176 return defaultOrderingMatchingRule; 177 } 178 179 180 181 /** 182 * Retrieves the default substring matching rule that will be used for 183 * attributes with this syntax. 184 * 185 * @return The default substring matching rule that will be used for 186 * attributes with this syntax, or <CODE>null</CODE> if substring 187 * matches will not be allowed for this type by default. 188 */ 189 public SubstringMatchingRule getSubstringMatchingRule() 190 { 191 return defaultSubstringMatchingRule; 192 } 193 194 195 196 /** 197 * Retrieves the default approximate matching rule that will be used for 198 * attributes with this syntax. 199 * 200 * @return The default approximate matching rule that will be used for 201 * attributes with this syntax, or <CODE>null</CODE> if approximate 202 * matches will not be allowed for this type by default. 203 */ 204 public ApproximateMatchingRule getApproximateMatchingRule() 205 { 206 // Approximate matching will not be allowed by default. 207 return null; 208 } 209 210 211 212 /** 213 * Indicates whether the provided value is acceptable for use in an attribute 214 * with this syntax. If it is not, then the reason may be appended to the 215 * provided buffer. 216 * 217 * @param value The value for which to make the determination. 218 * @param invalidReason The buffer to which the invalid reason should be 219 * appended. 220 * 221 * @return <CODE>true</CODE> if the provided value is acceptable for use with 222 * this syntax, or <CODE>false</CODE> if not. 223 */ 224 public boolean valueIsAcceptable(ByteString value, 225 MessageBuilder invalidReason) 226 { 227 // Get the string representation of the value and check its length. A 228 // zero-length value is acceptable. A one-length value is acceptable as 229 // long as it is not an asterisk. For all other lengths, just ensure that 230 // there are no consecutive wildcards. 231 String valueString = value.stringValue(); 232 int valueLength = valueString.length(); 233 if (valueLength == 0) 234 { 235 return true; 236 } 237 else if (valueLength == 1) 238 { 239 if (valueString.charAt(0) == '*') 240 { 241 invalidReason.append(WARN_ATTR_SYNTAX_SUBSTRING_ONLY_WILDCARD.get()); 242 243 return false; 244 } 245 else 246 { 247 return true; 248 } 249 } 250 else 251 { 252 for (int i=1; i < valueLength; i++) 253 { 254 if ((valueString.charAt(i) == '*') && (valueString.charAt(i-1) == '*')) 255 { 256 invalidReason.append( 257 WARN_ATTR_SYNTAX_SUBSTRING_CONSECUTIVE_WILDCARDS.get( 258 valueString, i)); 259 return false; 260 } 261 } 262 263 return true; 264 } 265 } 266 } 267