001/*
002 * Copyright 2015-2017 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2017 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 */
021package com.unboundid.util.args;
022
023
024
025import java.util.regex.Pattern;
026
027import com.unboundid.util.Debug;
028import com.unboundid.util.NotMutable;
029import com.unboundid.util.ThreadSafety;
030import com.unboundid.util.ThreadSafetyLevel;
031
032import static com.unboundid.util.args.ArgsMessages.*;
033
034
035
036/**
037 * This class provides an implementation of an argument value validator that is
038 * expected to be used with a string argument and ensures that all values for
039 * the argument are valid regular expressions.  Note that it does not verify
040 * that values match a given regular expression, but that can already be
041 * accomplished with the {@link StringArgument#setValueRegex} method.
042 */
043@NotMutable()
044@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
045public final class RegularExpressionArgumentValueValidator
046       extends ArgumentValueValidator
047{
048  /**
049   * Creates a new instance of this regular expression argument value validator.
050   */
051  public RegularExpressionArgumentValueValidator()
052  {
053    // No implementation is required.
054  }
055
056
057
058  /**
059   * {@inheritDoc}
060   */
061  @Override()
062  public void validateArgumentValue(final Argument argument,
063                                    final String valueString)
064         throws ArgumentException
065  {
066    try
067    {
068      Pattern.compile(valueString);
069    }
070    catch (final Exception e)
071    {
072      Debug.debugException(e);
073      throw new ArgumentException(
074           ERR_REGEX_VALIDATOR_VALUE_NOT_REGEX.get(valueString,
075                argument.getIdentifierString()));
076    }
077  }
078
079
080
081  /**
082   * Retrieves a string representation of this argument value validator.
083   *
084   * @return  A string representation of this argument value validator.
085   */
086  @Override()
087  public String toString()
088  {
089    final StringBuilder buffer = new StringBuilder();
090    toString(buffer);
091    return buffer.toString();
092  }
093
094
095
096  /**
097   * Appends a string representation of this argument value validator to the
098   * provided buffer.
099   *
100   * @param  buffer  The buffer to which the string representation should be
101   *                 appended.
102   */
103  public void toString(final StringBuilder buffer)
104  {
105    buffer.append("RegularExpressionArgumentValueValidator()");
106  }
107}