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.EqualityMatchingRuleCfg;
032 import org.opends.server.api.EqualityMatchingRule;
033 import org.opends.server.config.ConfigException;
034 import org.opends.server.protocols.asn1.ASN1OctetString;
035 import org.opends.server.types.AttributeValue;
036 import org.opends.server.types.ByteString;
037 import org.opends.server.types.DirectoryException;
038 import org.opends.server.types.InitializationException;
039
040 import static org.opends.server.schema.SchemaConstants.*;
041 import static org.opends.server.util.StaticUtils.*;
042
043
044
045 /**
046 * This class implements the directoryStringFirstComponentMatch matching rule
047 * defined in X.520 and referenced in RFC 2252. This rule is intended for use
048 * with attributes whose values contain a set of parentheses enclosing a
049 * space-delimited set of names and/or name-value pairs (like attribute type or
050 * objectclass descriptions) in which the "first component" is the first item
051 * after the opening parenthesis.
052 */
053 public class DirectoryStringFirstComponentEqualityMatchingRule
054 extends EqualityMatchingRule
055 {
056 /**
057 * Creates a new instance of this directoryStringFirstComponentMatch matching
058 * rule.
059 */
060 public DirectoryStringFirstComponentEqualityMatchingRule()
061 {
062 super();
063 }
064
065
066
067 /**
068 * {@inheritDoc}
069 */
070 public void initializeMatchingRule(EqualityMatchingRuleCfg configuration)
071 throws ConfigException, InitializationException
072 {
073 // No initialization is required.
074 }
075
076
077
078 /**
079 * Retrieves the common name for this matching rule.
080 *
081 * @return The common name for this matching rule, or <CODE>null</CODE> if
082 * it does not have a name.
083 */
084 public String getName()
085 {
086 return EMR_DIRECTORY_STRING_FIRST_COMPONENT_NAME;
087 }
088
089
090
091 /**
092 * Retrieves the OID for this matching rule.
093 *
094 * @return The OID for this matching rule.
095 */
096 public String getOID()
097 {
098 return EMR_DIRECTORY_STRING_FIRST_COMPONENT_OID;
099 }
100
101
102
103 /**
104 * Retrieves the description for this matching rule.
105 *
106 * @return The description for this matching rule, or <CODE>null</CODE> if
107 * there is none.
108 */
109 public String getDescription()
110 {
111 // There is no standard description for this matching rule.
112 return null;
113 }
114
115
116
117 /**
118 * Retrieves the OID of the syntax with which this matching rule is
119 * associated.
120 *
121 * @return The OID of the syntax with which this matching rule is associated.
122 */
123 public String getSyntaxOID()
124 {
125 return SYNTAX_DIRECTORY_STRING_OID;
126 }
127
128
129
130 /**
131 * Retrieves the normalized form of the provided value, which is best suited
132 * for efficiently performing matching operations on that value.
133 *
134 * @param value The value to be normalized.
135 *
136 * @return The normalized version of the provided value.
137 *
138 * @throws DirectoryException If the provided value is invalid according to
139 * the associated attribute syntax.
140 */
141 public ByteString normalizeValue(ByteString value)
142 throws DirectoryException
143 {
144 StringBuilder buffer = new StringBuilder();
145 toLowerCase(value.value(), buffer, true);
146
147 int bufferLength = buffer.length();
148 if (bufferLength == 0)
149 {
150 if (value.value().length > 0)
151 {
152 // This should only happen if the value is composed entirely of spaces.
153 // In that case, the normalized value is a single space.
154 return new ASN1OctetString(" ");
155 }
156 else
157 {
158 // The value is empty, so it is already normalized.
159 return new ASN1OctetString();
160 }
161 }
162
163
164 // Replace any consecutive spaces with a single space.
165 for (int pos = bufferLength-1; pos > 0; pos--)
166 {
167 if (buffer.charAt(pos) == ' ')
168 {
169 if (buffer.charAt(pos-1) == ' ')
170 {
171 buffer.delete(pos, pos+1);
172 }
173 }
174 }
175
176 return new ASN1OctetString(buffer.toString());
177 }
178
179
180
181 /**
182 * Indicates whether the two provided normalized values are equal to each
183 * other.
184 *
185 * @param value1 The normalized form of the first value to compare.
186 * @param value2 The normalized form of the second value to compare.
187 *
188 * @return <CODE>true</CODE> if the provided values are equal, or
189 * <CODE>false</CODE> if not.
190 */
191 public boolean areEqual(ByteString value1, ByteString value2)
192 {
193 // For this purpose, the first value will be considered the attribute value,
194 // and the second the assertion value. The attribute value must start with
195 // an open parenthesis, followed by one or more spaces.
196 String value1String = value1.stringValue();
197 int value1Length = value1String.length();
198
199 if ((value1Length == 0) || (value1String.charAt(0) != '('))
200 {
201 // They cannot be equal if the attribute value is empty or doesn't start
202 // with an open parenthesis.
203 return false;
204 }
205
206 char c;
207 int pos = 1;
208 while ((pos < value1Length) && ((c = value1String.charAt(pos)) == ' '))
209 {
210 pos++;
211 }
212
213 if (pos >= value1Length)
214 {
215 // We hit the end of the value before finding a non-space character.
216 return false;
217 }
218
219
220 // The current position must be the start position for the value. Keep
221 // reading until we find the next space.
222 int startPos = pos++;
223 while ((pos < value1Length) && ((c = value1String.charAt(pos)) != ' '))
224 {
225 pos++;
226 }
227
228 if (pos >= value1Length)
229 {
230 // We hit the end of the value before finding the next space.
231 return false;
232 }
233
234
235 // Grab the substring between the start pos and the current pos and compare
236 // it with the assertion value.
237 String compareStr = value1String.substring(startPos, pos);
238 String value2String = value2.stringValue();
239 return value2String.equals(compareStr);
240 }
241
242
243
244 /**
245 * Generates a hash code for the provided attribute value. This version of
246 * the method will simply create a hash code from the normalized form of the
247 * attribute value. For matching rules explicitly designed to work in cases
248 * where byte-for-byte comparisons of normalized values is not sufficient for
249 * determining equality (e.g., if the associated attribute syntax is based on
250 * hashed or encrypted values), then this method must be overridden to provide
251 * an appropriate implementation for that case.
252 *
253 * @param attributeValue The attribute value for which to generate the hash
254 * code.
255 *
256 * @return The hash code generated for the provided attribute value.*/
257 public int generateHashCode(AttributeValue attributeValue)
258 {
259 // In this case, we'll always return the same value because the matching
260 // isn't based on the entire value.
261 return 1;
262 }
263 }
264