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