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.HashSet; 032 import java.util.StringTokenizer; 033 034 import org.opends.server.admin.std.server.AttributeSyntaxCfg; 035 import org.opends.server.api.ApproximateMatchingRule; 036 import org.opends.server.api.AttributeSyntax; 037 import org.opends.server.api.EqualityMatchingRule; 038 import org.opends.server.api.OrderingMatchingRule; 039 import org.opends.server.api.SubstringMatchingRule; 040 import org.opends.server.config.ConfigException; 041 import org.opends.server.core.DirectoryServer; 042 import org.opends.server.types.ByteString; 043 044 045 046 import static org.opends.server.loggers.ErrorLogger.*; 047 import static org.opends.messages.SchemaMessages.*; 048 import org.opends.messages.MessageBuilder; 049 import static org.opends.server.schema.SchemaConstants.*; 050 import static org.opends.server.util.StaticUtils.*; 051 052 053 054 /** 055 * This class defines the delivery method attribute syntax. This contains one 056 * or more of a fixed set of values. If there are multiple values, then they 057 * are separated by spaces with a dollar sign between them. The allowed values 058 * include: 059 * 060 * <UL> 061 * <LI>any</LI> 062 * <LI>mhs</LI> 063 * <LI>physical</LI> 064 * <LI>telex</LI> 065 * <LI>teletex</LI> 066 * <LI>g3fax</LI> 067 * <LI>g4fax</LI> 068 * <LI>ia5</LI> 069 * <LI>videotex</LI> 070 * <LI>telephone</LI> 071 * </UL> 072 */ 073 public class DeliveryMethodSyntax 074 extends AttributeSyntax<AttributeSyntaxCfg> 075 { 076 /** 077 * The set of values that may be used as delivery methods. 078 */ 079 private static final String[] ALLOWED_VALUES = 080 { 081 "any", 082 "mhs", 083 "physical", 084 "telex", 085 "teletex", 086 "g3fax", 087 "g4fax", 088 "ia5", 089 "videotex", 090 "telephone" 091 }; 092 093 094 095 // The hash set containing the allowed values. 096 private HashSet<String> allowedValues; 097 098 // The default approximate matching rule for this syntax. 099 private ApproximateMatchingRule defaultApproximateMatchingRule; 100 101 // The default equality matching rule for this syntax. 102 private EqualityMatchingRule defaultEqualityMatchingRule; 103 104 // The default ordering matching rule for this syntax. 105 private OrderingMatchingRule defaultOrderingMatchingRule; 106 107 // The default substring matching rule for this syntax. 108 private SubstringMatchingRule defaultSubstringMatchingRule; 109 110 111 112 /** 113 * Creates a new instance of this syntax. Note that the only thing that 114 * should be done here is to invoke the default constructor for the 115 * superclass. All initialization should be performed in the 116 * <CODE>initializeSyntax</CODE> method. 117 */ 118 public DeliveryMethodSyntax() 119 { 120 super(); 121 } 122 123 124 125 /** 126 * {@inheritDoc} 127 */ 128 public void initializeSyntax(AttributeSyntaxCfg configuration) 129 throws ConfigException 130 { 131 allowedValues = new HashSet<String>(ALLOWED_VALUES.length); 132 for (String s : ALLOWED_VALUES) 133 { 134 allowedValues.add(s); 135 } 136 137 defaultApproximateMatchingRule = 138 DirectoryServer.getApproximateMatchingRule(AMR_DOUBLE_METAPHONE_OID); 139 if (defaultApproximateMatchingRule == null) 140 { 141 logError(ERR_ATTR_SYNTAX_UNKNOWN_APPROXIMATE_MATCHING_RULE.get( 142 AMR_DOUBLE_METAPHONE_OID, SYNTAX_DELIVERY_METHOD_NAME)); 143 } 144 145 defaultEqualityMatchingRule = 146 DirectoryServer.getEqualityMatchingRule(EMR_CASE_IGNORE_OID); 147 if (defaultEqualityMatchingRule == null) 148 { 149 logError(ERR_ATTR_SYNTAX_UNKNOWN_EQUALITY_MATCHING_RULE.get( 150 EMR_CASE_IGNORE_OID, SYNTAX_DELIVERY_METHOD_NAME)); 151 } 152 153 defaultOrderingMatchingRule = 154 DirectoryServer.getOrderingMatchingRule(OMR_CASE_IGNORE_OID); 155 if (defaultOrderingMatchingRule == null) 156 { 157 logError(ERR_ATTR_SYNTAX_UNKNOWN_ORDERING_MATCHING_RULE.get( 158 OMR_CASE_IGNORE_OID, SYNTAX_DELIVERY_METHOD_NAME)); 159 } 160 161 defaultSubstringMatchingRule = 162 DirectoryServer.getSubstringMatchingRule(SMR_CASE_IGNORE_OID); 163 if (defaultSubstringMatchingRule == null) 164 { 165 logError(ERR_ATTR_SYNTAX_UNKNOWN_SUBSTRING_MATCHING_RULE.get( 166 SMR_CASE_IGNORE_OID, SYNTAX_DELIVERY_METHOD_NAME)); 167 } 168 } 169 170 171 172 /** 173 * Retrieves the common name for this attribute syntax. 174 * 175 * @return The common name for this attribute syntax. 176 */ 177 public String getSyntaxName() 178 { 179 return SYNTAX_DELIVERY_METHOD_NAME; 180 } 181 182 183 184 /** 185 * Retrieves the OID for this attribute syntax. 186 * 187 * @return The OID for this attribute syntax. 188 */ 189 public String getOID() 190 { 191 return SYNTAX_DELIVERY_METHOD_OID; 192 } 193 194 195 196 /** 197 * Retrieves a description for this attribute syntax. 198 * 199 * @return A description for this attribute syntax. 200 */ 201 public String getDescription() 202 { 203 return SYNTAX_DELIVERY_METHOD_DESCRIPTION; 204 } 205 206 207 208 /** 209 * Retrieves the default equality matching rule that will be used for 210 * attributes with this syntax. 211 * 212 * @return The default equality matching rule that will be used for 213 * attributes with this syntax, or <CODE>null</CODE> if equality 214 * matches will not be allowed for this type by default. 215 */ 216 public EqualityMatchingRule getEqualityMatchingRule() 217 { 218 return defaultEqualityMatchingRule; 219 } 220 221 222 223 /** 224 * Retrieves the default ordering matching rule that will be used for 225 * attributes with this syntax. 226 * 227 * @return The default ordering matching rule that will be used for 228 * attributes with this syntax, or <CODE>null</CODE> if ordering 229 * matches will not be allowed for this type by default. 230 */ 231 public OrderingMatchingRule getOrderingMatchingRule() 232 { 233 return defaultOrderingMatchingRule; 234 } 235 236 237 238 /** 239 * Retrieves the default substring matching rule that will be used for 240 * attributes with this syntax. 241 * 242 * @return The default substring matching rule that will be used for 243 * attributes with this syntax, or <CODE>null</CODE> if substring 244 * matches will not be allowed for this type by default. 245 */ 246 public SubstringMatchingRule getSubstringMatchingRule() 247 { 248 return defaultSubstringMatchingRule; 249 } 250 251 252 253 /** 254 * Retrieves the default approximate matching rule that will be used for 255 * attributes with this syntax. 256 * 257 * @return The default approximate matching rule that will be used for 258 * attributes with this syntax, or <CODE>null</CODE> if approximate 259 * matches will not be allowed for this type by default. 260 */ 261 public ApproximateMatchingRule getApproximateMatchingRule() 262 { 263 return defaultApproximateMatchingRule; 264 } 265 266 267 268 /** 269 * Indicates whether the provided value is acceptable for use in an attribute 270 * with this syntax. If it is not, then the reason may be appended to the 271 * provided buffer. 272 * 273 * @param value The value for which to make the determination. 274 * @param invalidReason The buffer to which the invalid reason should be 275 * appended. 276 * 277 * @return <CODE>true</CODE> if the provided value is acceptable for use with 278 * this syntax, or <CODE>false</CODE> if not. 279 */ 280 public boolean valueIsAcceptable(ByteString value, 281 MessageBuilder invalidReason) 282 { 283 String stringValue = toLowerCase(value.stringValue()); 284 StringTokenizer tokenizer = new StringTokenizer(stringValue, " $"); 285 if (! tokenizer.hasMoreTokens()) 286 { 287 invalidReason.append(ERR_ATTR_SYNTAX_DELIVERY_METHOD_NO_ELEMENTS.get( 288 value.stringValue())); 289 return false; 290 } 291 292 while (tokenizer.hasMoreTokens()) 293 { 294 String token = tokenizer.nextToken(); 295 if (! allowedValues.contains(token)) 296 { 297 invalidReason.append(ERR_ATTR_SYNTAX_DELIVERY_METHOD_INVALID_ELEMENT 298 .get(value.stringValue(), token)); 299 return false; 300 } 301 } 302 303 return true; 304 } 305 } 306