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.Arrays;
032    
033    import org.opends.server.admin.std.server.EqualityMatchingRuleCfg;
034    import org.opends.server.api.EqualityMatchingRule;
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    import static org.opends.server.util.StaticUtils.*;
043    
044    
045    
046    /**
047     * This class implements the telephoneNumberMatch matching rule defined in X.520
048     * and referenced in RFC 2252.  Note that although the specification calls for a
049     * very rigorous format, this is widely ignored so this matching will compare
050     * only numeric digits and strip out everything else.
051     */
052    public class TelephoneNumberEqualityMatchingRule
053           extends EqualityMatchingRule
054    {
055      /**
056       * Creates a new instance of this telephoneNumberMatch matching rule.
057       */
058      public TelephoneNumberEqualityMatchingRule()
059      {
060        super();
061      }
062    
063    
064    
065      /**
066       * {@inheritDoc}
067       */
068      public void initializeMatchingRule(EqualityMatchingRuleCfg configuration)
069             throws ConfigException, InitializationException
070      {
071        // No initialization is required.
072      }
073    
074    
075    
076      /**
077       * Retrieves the common name for this matching rule.
078       *
079       * @return  The common name for this matching rule, or <CODE>null</CODE> if
080       * it does not have a name.
081       */
082      public String getName()
083      {
084        return EMR_TELEPHONE_NAME;
085      }
086    
087    
088    
089      /**
090       * Retrieves the OID for this matching rule.
091       *
092       * @return  The OID for this matching rule.
093       */
094      public String getOID()
095      {
096        return EMR_TELEPHONE_OID;
097      }
098    
099    
100    
101      /**
102       * Retrieves the description for this matching rule.
103       *
104       * @return  The description for this matching rule, or <CODE>null</CODE> if
105       *          there is none.
106       */
107      public String getDescription()
108      {
109        // There is no standard description for this matching rule.
110        return null;
111      }
112    
113    
114    
115      /**
116       * Retrieves the OID of the syntax with which this matching rule is
117       * associated.
118       *
119       * @return  The OID of the syntax with which this matching rule is associated.
120       */
121      public String getSyntaxOID()
122      {
123        return SYNTAX_TELEPHONE_OID;
124      }
125    
126    
127    
128      /**
129       * Retrieves the normalized form of the provided value, which is best suited
130       * for efficiently performing matching operations on that value.
131       *
132       * @param  value  The value to be normalized.
133       *
134       * @return  The normalized version of the provided value.
135       *
136       * @throws  DirectoryException  If the provided value is invalid according to
137       *                              the associated attribute syntax.
138       */
139      public ByteString normalizeValue(ByteString value)
140             throws DirectoryException
141      {
142        String valueString = value.stringValue();
143        int    valueLength = valueString.length();
144        StringBuilder buffer = new StringBuilder(valueLength);
145    
146    
147        // Iterate through the characters in the value and filter out everything
148        // that isn't a digit.
149        for (int i=0; i < valueLength; i++)
150        {
151          char c = valueString.charAt(i);
152          if (isDigit(c))
153          {
154            buffer.append(c);
155          }
156        }
157    
158    
159        return new ASN1OctetString(buffer.toString());
160      }
161    
162    
163    
164      /**
165       * Indicates whether the two provided normalized values are equal to each
166       * other.
167       *
168       * @param  value1  The normalized form of the first value to compare.
169       * @param  value2  The normalized form of the second value to compare.
170       *
171       * @return  <CODE>true</CODE> if the provided values are equal, or
172       *          <CODE>false</CODE> if not.
173       */
174      public boolean areEqual(ByteString value1, ByteString value2)
175      {
176        // Since the values are already normalized, we just need to compare the
177        // associated byte arrays.
178        return Arrays.equals(value1.value(), value2.value());
179      }
180    }
181