001    /*
002     * Copyright 2008-2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2008-2016 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.matchingrules;
022    
023    
024    
025    import com.unboundid.asn1.ASN1OctetString;
026    import com.unboundid.ldap.sdk.LDAPException;
027    import com.unboundid.ldap.sdk.ResultCode;
028    
029    import static com.unboundid.ldap.matchingrules.MatchingRuleMessages.*;
030    import static com.unboundid.util.StaticUtils.*;
031    
032    
033    
034    /**
035     * This class provides an implementation of a matching rule that performs
036     * equality comparisons against Boolean values, which should be either "TRUE" or
037     * "FALSE".  Substring and ordering matching are not supported.
038     */
039    public final class BooleanMatchingRule
040           extends MatchingRule
041    {
042      /**
043       * The singleton instance that will be returned from the {@code getInstance}
044       * method.
045       */
046      private static final BooleanMatchingRule INSTANCE =
047           new BooleanMatchingRule();
048    
049    
050    
051      /**
052       * The pre-defined value that will be used as the normalized representation
053       * of a "TRUE" value.
054       */
055      private static final ASN1OctetString TRUE_VALUE = new ASN1OctetString("TRUE");
056    
057    
058    
059      /**
060       * The pre-defined value that will be used as the normalized representation
061       * of a "FALSE" value.
062       */
063      private static final ASN1OctetString FALSE_VALUE =
064           new ASN1OctetString("FALSE");
065    
066    
067    
068      /**
069       * The name for the booleanMatch equality matching rule.
070       */
071      public static final String EQUALITY_RULE_NAME = "booleanMatch";
072    
073    
074    
075      /**
076       * The name for the booleanMatch equality matching rule, formatted in all
077       * lowercase characters.
078       */
079      static final String LOWER_EQUALITY_RULE_NAME =
080           toLowerCase(EQUALITY_RULE_NAME);
081    
082    
083    
084      /**
085       * The OID for the booleanMatch equality matching rule.
086       */
087      public static final String EQUALITY_RULE_OID = "2.5.13.13";
088    
089    
090    
091      /**
092       * The serial version UID for this serializable class.
093       */
094      private static final long serialVersionUID = 5137725892611277972L;
095    
096    
097    
098      /**
099       * Creates a new instance of this Boolean matching rule.
100       */
101      public BooleanMatchingRule()
102      {
103        // No implementation is required.
104      }
105    
106    
107    
108      /**
109       * Retrieves a singleton instance of this matching rule.
110       *
111       * @return  A singleton instance of this matching rule.
112       */
113      public static BooleanMatchingRule getInstance()
114      {
115        return INSTANCE;
116      }
117    
118    
119    
120      /**
121       * {@inheritDoc}
122       */
123      @Override()
124      public String getEqualityMatchingRuleName()
125      {
126        return EQUALITY_RULE_NAME;
127      }
128    
129    
130    
131      /**
132       * {@inheritDoc}
133       */
134      @Override()
135      public String getEqualityMatchingRuleOID()
136      {
137        return EQUALITY_RULE_OID;
138      }
139    
140    
141    
142      /**
143       * {@inheritDoc}
144       */
145      @Override()
146      public String getOrderingMatchingRuleName()
147      {
148        return null;
149      }
150    
151    
152    
153      /**
154       * {@inheritDoc}
155       */
156      @Override()
157      public String getOrderingMatchingRuleOID()
158      {
159        return null;
160      }
161    
162    
163    
164      /**
165       * {@inheritDoc}
166       */
167      @Override()
168      public String getSubstringMatchingRuleName()
169      {
170        return null;
171      }
172    
173    
174    
175      /**
176       * {@inheritDoc}
177       */
178      @Override()
179      public String getSubstringMatchingRuleOID()
180      {
181        return null;
182      }
183    
184    
185    
186      /**
187       * {@inheritDoc}
188       */
189      @Override()
190      public boolean valuesMatch(final ASN1OctetString value1,
191                                 final ASN1OctetString value2)
192             throws LDAPException
193      {
194        return normalize(value1).equals(normalize(value2));
195      }
196    
197    
198    
199      /**
200       * {@inheritDoc}
201       */
202      @Override()
203      public boolean matchesSubstring(final ASN1OctetString value,
204                                      final ASN1OctetString subInitial,
205                                      final ASN1OctetString[] subAny,
206                                      final ASN1OctetString subFinal)
207             throws LDAPException
208      {
209        throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING,
210                                ERR_BOOLEAN_SUBSTRING_MATCHING_NOT_SUPPORTED.get());
211      }
212    
213    
214    
215      /**
216       * {@inheritDoc}
217       */
218      @Override()
219      public int compareValues(final ASN1OctetString value1,
220                               final ASN1OctetString value2)
221             throws LDAPException
222      {
223        throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING,
224                                ERR_BOOLEAN_ORDERING_MATCHING_NOT_SUPPORTED.get());
225      }
226    
227    
228    
229      /**
230       * {@inheritDoc}
231       */
232      @Override()
233      public ASN1OctetString normalize(final ASN1OctetString value)
234             throws LDAPException
235      {
236        final byte[] valueBytes = value.getValue();
237    
238        if ((valueBytes.length == 4) &&
239            ((valueBytes[0] == 'T') || (valueBytes[0] == 't')) &&
240            ((valueBytes[1] == 'R') || (valueBytes[1] == 'r')) &&
241            ((valueBytes[2] == 'U') || (valueBytes[2] == 'u')) &&
242            ((valueBytes[3] == 'E') || (valueBytes[3] == 'e')))
243        {
244          return TRUE_VALUE;
245        }
246        else if ((valueBytes.length == 5) &&
247                 ((valueBytes[0] == 'F') || (valueBytes[0] == 'f')) &&
248                 ((valueBytes[1] == 'A') || (valueBytes[1] == 'a')) &&
249                 ((valueBytes[2] == 'L') || (valueBytes[2] == 'l')) &&
250                 ((valueBytes[3] == 'S') || (valueBytes[3] == 's')) &&
251                 ((valueBytes[4] == 'E') || (valueBytes[4] == 'e')))
252        {
253          return FALSE_VALUE;
254        }
255        else
256        {
257          throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
258                                  ERR_BOOLEAN_INVALID_VALUE.get());
259        }
260      }
261    
262    
263    
264      /**
265       * {@inheritDoc}
266       */
267      @Override()
268      public ASN1OctetString normalizeSubstring(final ASN1OctetString value,
269                                                final byte substringType)
270             throws LDAPException
271      {
272        throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING,
273                                ERR_BOOLEAN_SUBSTRING_MATCHING_NOT_SUPPORTED.get());
274      }
275    }