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 import org.opends.messages.Message; 029 030 031 032 import java.util.Arrays; 033 034 import org.opends.server.admin.std.server.EqualityMatchingRuleCfg; 035 import org.opends.server.api.EqualityMatchingRule; 036 import org.opends.server.config.ConfigException; 037 import org.opends.server.core.DirectoryServer; 038 import org.opends.server.protocols.asn1.ASN1OctetString; 039 import org.opends.server.types.ByteString; 040 import org.opends.server.types.DirectoryException; 041 import org.opends.server.types.InitializationException; 042 import org.opends.server.types.ResultCode; 043 044 import static org.opends.messages.SchemaMessages.*; 045 import static org.opends.server.schema.SchemaConstants.*; 046 import static org.opends.server.util.StaticUtils.*; 047 import org.opends.server.loggers.ErrorLogger; 048 049 050 /** 051 * This class implements the caseIgnoreIA5Match matching rule defined in RFC 052 * 2252. 053 */ 054 public class CaseIgnoreIA5EqualityMatchingRule 055 extends EqualityMatchingRule 056 { 057 /** 058 * Creates a new instance of this caseIgnoreIA5Match matching rule. 059 */ 060 public CaseIgnoreIA5EqualityMatchingRule() 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_CASE_IGNORE_IA5_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_CASE_IGNORE_IA5_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_IA5_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, and watch out for 165 // non-ASCII characters. 166 boolean logged = false; 167 for (int pos = bufferLength-1; pos > 0; pos--) 168 { 169 char c = buffer.charAt(pos); 170 if (c == ' ') 171 { 172 if (buffer.charAt(pos-1) == ' ') 173 { 174 buffer.delete(pos, pos+1); 175 } 176 } 177 else if ((c & 0x7F) != c) 178 { 179 // This is not a valid character for an IA5 string. If strict syntax 180 // enforcement is enabled, then we'll throw an exception. Otherwise, 181 // we'll get rid of the character. 182 183 Message message = WARN_ATTR_SYNTAX_IA5_ILLEGAL_CHARACTER.get( 184 value.stringValue(), String.valueOf(c)); 185 186 switch (DirectoryServer.getSyntaxEnforcementPolicy()) 187 { 188 case REJECT: 189 throw new DirectoryException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, 190 message); 191 case WARN: 192 if (! logged) 193 { 194 ErrorLogger.logError(message); 195 logged = true; 196 } 197 198 buffer.delete(pos, pos+1); 199 break; 200 201 default: 202 buffer.delete(pos, pos+1); 203 break; 204 } 205 } 206 } 207 208 return new ASN1OctetString(buffer.toString()); 209 } 210 211 212 213 /** 214 * Indicates whether the two provided normalized values are equal to each 215 * other. 216 * 217 * @param value1 The normalized form of the first value to compare. 218 * @param value2 The normalized form of the second value to compare. 219 * 220 * @return <CODE>true</CODE> if the provided values are equal, or 221 * <CODE>false</CODE> if not. 222 */ 223 public boolean areEqual(ByteString value1, ByteString value2) 224 { 225 // Since the values are already normalized, we just need to compare the 226 // associated byte arrays. 227 return Arrays.equals(value1.value(), value2.value()); 228 } 229 } 230