001/*
002 * Copyright 2017-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-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) 2017-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.util.args;
037
038
039
040import java.io.Serializable;
041import java.text.ParseException;
042
043import com.unboundid.util.Debug;
044import com.unboundid.util.NotMutable;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.OID;
047import com.unboundid.util.ThreadSafety;
048import com.unboundid.util.ThreadSafetyLevel;
049
050import static com.unboundid.util.args.ArgsMessages.*;
051
052
053
054/**
055 * This class provides an implementation of an argument value validator that
056 * ensures that values can be parsed as valid object identifiers.
057 */
058@NotMutable()
059@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
060public final class OIDArgumentValueValidator
061       extends ArgumentValueValidator
062       implements Serializable
063{
064  /**
065   * The serial version UID for this serializable class.
066   */
067  private static final long serialVersionUID = 2195078137238476902L;
068
069
070
071  // Indicates whether to perform strict validation.
072  private final boolean isStrict;
073
074
075
076  /**
077   * Creates a new OID address argument value validator that will only accept
078   * strictly valid numeric OIDs.
079   */
080  public OIDArgumentValueValidator()
081  {
082    this(true);
083  }
084
085
086
087  /**
088   * Creates a new OID address argument value validator that will only accept
089   * valid numeric OIDs.
090   *
091   * @param  isStrict  Indicates whether to perform strict validation.  If this
092   *                   is {@code false}, then the validator will only sure that
093   *                   each value is a dotted list of digits that does not start
094   *                   or end with a period and does not contain two consecutive
095   *                   periods.  If this is {@code true}, then it will also
096   *                   ensure that it contains at least two components, that the
097   *                   value of the first component is not greater than two,
098   *                   and that the value of the second component is not greater
099   *                   than 39 if the value of the first component is zero or
100   *                   one.
101   */
102  public OIDArgumentValueValidator(final boolean isStrict)
103  {
104    this.isStrict = isStrict;
105  }
106
107
108
109  /**
110   * Indicates whether this validator is configured to operate in strict mode.
111   * If it not operating in strict mode, then it will only ensure that each
112   * value is is a dotted list of digits that does not start or end with a
113   * period and does not contain two consecutive periods.  If it is strict, then
114   * it will also ensure that it contains at least two components, that the
115   * value of the first component is not greater than two, and that the value of
116   * the second component is not greater than 39 if the value of the first
117   * component is zero or one.
118   *
119   * @return  {@code true} if this validator is configured to operate in strict
120   *          mode, or {@code false} if not.
121   */
122  public boolean isStrict()
123  {
124    return isStrict;
125  }
126
127
128
129  /**
130   * {@inheritDoc}
131   */
132  @Override()
133  public void validateArgumentValue(@NotNull final Argument argument,
134                                    @NotNull final String valueString)
135         throws ArgumentException
136  {
137    try
138    {
139      OID.parseNumericOID(valueString, isStrict);
140    }
141    catch (final ParseException e)
142    {
143      Debug.debugException(e);
144      throw new ArgumentException(
145           ERR_OID_VALIDATOR_INVALID_VALUE.get(argument.getIdentifierString(),
146                e.getMessage()),
147           e);
148    }
149  }
150
151
152
153  /**
154   * Retrieves a string representation of this argument value validator.
155   *
156   * @return  A string representation of this argument value validator.
157   */
158  @Override()
159  @NotNull()
160  public String toString()
161  {
162    final StringBuilder buffer = new StringBuilder();
163    toString(buffer);
164    return buffer.toString();
165  }
166
167
168
169  /**
170   * Appends a string representation of this argument value validator to the
171   * provided buffer.
172   *
173   * @param  buffer  The buffer to which the string representation should be
174   *                 appended.
175   */
176  public void toString(@NotNull final StringBuilder buffer)
177  {
178    buffer.append("OIDArgumentValueValidator(isStrict=");
179    buffer.append(isStrict);
180    buffer.append(')');
181  }
182}