001/*
002 * Copyright 2008-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2008-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.matchingrules;
037
038
039
040import com.unboundid.asn1.ASN1OctetString;
041import com.unboundid.ldap.sdk.LDAPException;
042import com.unboundid.ldap.sdk.ResultCode;
043import com.unboundid.util.NotNull;
044import com.unboundid.util.StaticUtils;
045import com.unboundid.util.ThreadSafety;
046import com.unboundid.util.ThreadSafetyLevel;
047
048import static com.unboundid.ldap.matchingrules.MatchingRuleMessages.*;
049
050
051
052/**
053 * This class provides an implementation of a matching rule that allows strings
054 * consisting of numeric digits and spaces.  Spaces will be considered
055 * insignificant for matching purposes.
056 */
057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
058public final class NumericStringMatchingRule
059       extends SimpleMatchingRule
060{
061  /**
062   * The singleton instance that will be returned from the {@code getInstance}
063   * method.
064   */
065  @NotNull private static final NumericStringMatchingRule INSTANCE =
066       new NumericStringMatchingRule();
067
068
069
070  /**
071   * The name for the numericStringMatch equality matching rule.
072   */
073  @NotNull public static final String EQUALITY_RULE_NAME =
074       "numericStringMatch";
075
076
077
078  /**
079   * The name for the numericStringMatch equality matching rule, formatted in
080   * all lowercase characters.
081   */
082  @NotNull static final String LOWER_EQUALITY_RULE_NAME =
083       StaticUtils.toLowerCase(EQUALITY_RULE_NAME);
084
085
086
087  /**
088   * The OID for the numericStringMatch equality matching rule.
089   */
090  @NotNull public static final String EQUALITY_RULE_OID = "2.5.13.8";
091
092
093
094  /**
095   * The name for the numericStringOrderingMatch ordering matching rule.
096   */
097  @NotNull public static final String ORDERING_RULE_NAME =
098       "numericStringOrderingMatch";
099
100
101
102  /**
103   * The name for the numericStringOrderingMatch ordering matching rule,
104   * formatted in all lowercase characters.
105   */
106  @NotNull static final String LOWER_ORDERING_RULE_NAME =
107       StaticUtils.toLowerCase(ORDERING_RULE_NAME);
108
109
110
111  /**
112   * The OID for the numericStringOrderingMatch ordering matching rule.
113   */
114  @NotNull public static final String ORDERING_RULE_OID = "2.5.13.9";
115
116
117
118  /**
119   * The name for the numericStringSubstringsMatch substring matching rule.
120   */
121  @NotNull public static final String SUBSTRING_RULE_NAME =
122       "numericStringSubstringsMatch";
123
124
125
126  /**
127   * The name for the numericStringSubstringsMatch substring matching rule,
128   * formatted in all lowercase characters.
129   */
130  @NotNull static final String LOWER_SUBSTRING_RULE_NAME =
131       StaticUtils.toLowerCase(SUBSTRING_RULE_NAME);
132
133
134
135  /**
136   * The OID for the numericStringSubstringsMatch substring matching rule.
137   */
138  @NotNull public static final String SUBSTRING_RULE_OID = "2.5.13.10";
139
140
141
142  /**
143   * The serial version UID for this serializable class.
144   */
145  private static final long serialVersionUID = -898484312052746321L;
146
147
148
149  /**
150   * Creates a new instance of this numeric string matching rule.
151   */
152  public NumericStringMatchingRule()
153  {
154    // No implementation is required.
155  }
156
157
158
159  /**
160   * Retrieves a singleton instance of this matching rule.
161   *
162   * @return  A singleton instance of this matching rule.
163   */
164  @NotNull()
165  public static NumericStringMatchingRule getInstance()
166  {
167    return INSTANCE;
168  }
169
170
171
172  /**
173   * {@inheritDoc}
174   */
175  @Override()
176  @NotNull()
177  public String getEqualityMatchingRuleName()
178  {
179    return EQUALITY_RULE_NAME;
180  }
181
182
183
184  /**
185   * {@inheritDoc}
186   */
187  @Override()
188  @NotNull()
189  public String getEqualityMatchingRuleOID()
190  {
191    return EQUALITY_RULE_OID;
192  }
193
194
195
196  /**
197   * {@inheritDoc}
198   */
199  @Override()
200  @NotNull()
201  public String getOrderingMatchingRuleName()
202  {
203    return ORDERING_RULE_NAME;
204  }
205
206
207
208  /**
209   * {@inheritDoc}
210   */
211  @Override()
212  @NotNull()
213  public String getOrderingMatchingRuleOID()
214  {
215    return ORDERING_RULE_OID;
216  }
217
218
219
220  /**
221   * {@inheritDoc}
222   */
223  @Override()
224  @NotNull()
225  public String getSubstringMatchingRuleName()
226  {
227    return SUBSTRING_RULE_NAME;
228  }
229
230
231
232  /**
233   * {@inheritDoc}
234   */
235  @Override()
236  @NotNull()
237  public String getSubstringMatchingRuleOID()
238  {
239    return SUBSTRING_RULE_OID;
240  }
241
242
243
244  /**
245   * {@inheritDoc}
246   */
247  @Override()
248  @NotNull()
249  public ASN1OctetString normalize(@NotNull final ASN1OctetString value)
250         throws LDAPException
251  {
252    // The value may already be normalized, so optimize behavior for that
253    // possibility.
254    int numSpaces = 0;
255    final byte[] valueBytes = value.getValue();
256    for (int i=0; i < valueBytes.length; i++)
257    {
258      if (valueBytes[i] == ' ')
259      {
260        numSpaces++;
261      }
262      else if ((valueBytes[i] < '0') || (valueBytes[i] > '9'))
263      {
264        throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
265                                ERR_NUMERIC_STRING_INVALID_CHARACTER.get(i));
266      }
267    }
268
269    if (numSpaces == 0)
270    {
271      return value;
272    }
273
274    int pos = 0;
275    final byte[] returnBytes = new byte[valueBytes.length-numSpaces];
276    for (final byte b : valueBytes)
277    {
278      if (b != ' ')
279      {
280        returnBytes[pos++] = b;
281      }
282    }
283
284    return new ASN1OctetString(returnBytes);
285  }
286
287
288
289  /**
290   * {@inheritDoc}
291   */
292  @Override()
293  @NotNull()
294  public ASN1OctetString normalizeSubstring(
295                              @NotNull final ASN1OctetString value,
296                              final byte substringType)
297         throws LDAPException
298  {
299    return normalize(value);
300  }
301}