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 org.opends.server.loggers.ErrorLogger;
047    
048    
049    /**
050     * This class defines the booleanMatch matching rule defined in X.520 and
051     * referenced in RFC 4519.
052     */
053    public class BooleanEqualityMatchingRule
054           extends EqualityMatchingRule
055    {
056      /**
057       * Creates a new instance of this booleanMatch matching rule.
058       */
059      public BooleanEqualityMatchingRule()
060      {
061        super();
062      }
063    
064    
065    
066      /**
067       * {@inheritDoc}
068       */
069      public void initializeMatchingRule(EqualityMatchingRuleCfg configuration)
070             throws ConfigException, InitializationException
071      {
072        // No initialization is required.
073      }
074    
075    
076    
077      /**
078       * Retrieves the common name for this matching rule.
079       *
080       * @return  The common name for this matching rule, or <CODE>null</CODE> if
081       * it does not have a name.
082       */
083      public String getName()
084      {
085        return EMR_BOOLEAN_NAME;
086      }
087    
088    
089    
090      /**
091       * Retrieves the OID for this matching rule.
092       *
093       * @return  The OID for this matching rule.
094       */
095      public String getOID()
096      {
097        return EMR_BOOLEAN_OID;
098      }
099    
100    
101    
102      /**
103       * Retrieves the description for this matching rule.
104       *
105       * @return  The description for this matching rule, or <CODE>null</CODE> if
106       *          there is none.
107       */
108      public String getDescription()
109      {
110        // There is no standard description for this matching rule.
111        return null;
112      }
113    
114    
115    
116      /**
117       * Retrieves the OID of the syntax with which this matching rule is
118       * associated.
119       *
120       * @return  The OID of the syntax with which this matching rule is associated.
121       */
122      public String getSyntaxOID()
123      {
124        return SYNTAX_BOOLEAN_OID;
125      }
126    
127    
128    
129      /**
130       * Retrieves the normalized form of the provided value, which is best suited
131       * for efficiently performing matching operations on that value.
132       *
133       * @param  value  The value to be normalized.
134       *
135       * @return  The normalized version of the provided value.
136       *
137       * @throws  DirectoryException  If the provided value is invalid according to
138       *                              the associated attribute syntax.
139       */
140      public ByteString normalizeValue(ByteString value)
141             throws DirectoryException
142      {
143        String valueString = value.stringValue().toUpperCase();
144        if (valueString.equals("TRUE") || valueString.equals("YES") ||
145            valueString.equals("ON") || valueString.equals("1"))
146        {
147          return new ASN1OctetString("TRUE");
148        }
149        else if (valueString.equals("FALSE") || valueString.equals("NO") ||
150                 valueString.equals("OFF") || valueString.equals("0"))
151        {
152          return new ASN1OctetString("FALSE");
153        }
154        else
155        {
156          Message message = WARN_ATTR_SYNTAX_ILLEGAL_BOOLEAN.get(
157                  value.stringValue());
158    
159          switch (DirectoryServer.getSyntaxEnforcementPolicy())
160          {
161            case REJECT:
162              throw new DirectoryException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
163                                           message);
164            case WARN:
165              ErrorLogger.logError(message);
166              return new ASN1OctetString(valueString);
167            default:
168              return new ASN1OctetString(valueString);
169          }
170        }
171      }
172    
173    
174    
175      /**
176       * Indicates whether the two provided normalized values are equal to each
177       * other.
178       *
179       * @param  value1  The normalized form of the first value to compare.
180       * @param  value2  The normalized form of the second value to compare.
181       *
182       * @return  <CODE>true</CODE> if the provided values are equal, or
183       *          <CODE>false</CODE> if not.
184       */
185      public boolean areEqual(ByteString value1, ByteString value2)
186      {
187        // Since the values are already normalized, we just need to compare the
188        // associated byte arrays.
189        return Arrays.equals(value1.value(), value2.value());
190      }
191    }
192