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;
026
027import com.unboundid.ldap.sdk.Attribute;
028import com.unboundid.ldap.sdk.persist.PersistUtils;
029import com.unboundid.ldap.sdk.schema.Schema;
030import com.unboundid.util.NotMutable;
031import com.unboundid.util.ThreadSafety;
032import com.unboundid.util.ThreadSafetyLevel;
033
034import static com.unboundid.util.args.ArgsMessages.*;
035
036
037
038/**
039 * This class provides an implementation of an argument value validator that is
040 * expected to be used with a string argument and ensures that all values for
041 * the argument are valid attribute type names (or numeric OIDs) or attribute
042 * descriptions (a name or OID with attribute options).  It can optionally use a
043 * provided schema to verify that the specified attribute type is defined.
044 */
045@NotMutable()
046@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
047public final class AttributeNameArgumentValueValidator
048       extends ArgumentValueValidator
049       implements Serializable
050{
051  /**
052   * The serial version UID for this serializable class.
053   */
054  private static final long serialVersionUID = 1781129993679474323L;
055
056
057
058  // Indicates whether to allow values to include attribute options.
059  private final boolean allowOptions;
060
061  // An optional schema to use to verify that the specified attribute type is
062  // defined.
063  private final Schema schema;
064
065
066
067  /**
068   * Creates a new instance of this attribute name argument value validator that
069   * will not permit attribute options and will not attempt to verify that the
070   * specified attribute type is defined in a schema.
071   */
072  public AttributeNameArgumentValueValidator()
073  {
074    this(false, null);
075  }
076
077
078
079  /**
080   * Creates a new instance of this attribute name argument value validator with
081   * the provided information.
082   *
083   * @param  allowOptions  Indicates whether to allow values that include one or
084   *                       more attribute options.
085   * @param  schema        An optional schema that can be used to verify that
086   *                       the specified attribute type is defined.
087   */
088  public AttributeNameArgumentValueValidator(final boolean allowOptions,
089                                             final Schema schema)
090  {
091    this.allowOptions = allowOptions;
092    this.schema       = schema;
093  }
094
095
096
097  /**
098   * Indicates whether to allow values that include one or more attribute
099   * options.
100   *
101   * @return  {@code true} if values will be allowed to include attribute
102   *          options, or {@code false} if not.
103   */
104  public boolean allowOptions()
105  {
106    return allowOptions;
107  }
108
109
110
111  /**
112   * Retrieves the schema that will be used to verify that attribute types
113   * specified in argument values are defined, if any.
114   *
115   * @return  The schema that will be used to verify that attribute types
116   *          specified in argument values are defined, or {@code null} if no
117   *          such validation will be performed.
118   */
119  public Schema getSchema()
120  {
121    return schema;
122  }
123
124
125
126  /**
127   * {@inheritDoc}
128   */
129  @Override()
130  public void validateArgumentValue(final Argument argument,
131                                    final String valueString)
132         throws ArgumentException
133  {
134    final StringBuilder errorMessage = new StringBuilder();
135    if (! PersistUtils.isValidLDAPName(valueString, allowOptions, errorMessage))
136    {
137      throw new ArgumentException(ERR_ATTR_NAME_VALIDATOR_INVALID_VALUE.get(
138           valueString, argument.getIdentifierString(),
139           String.valueOf(errorMessage)));
140    }
141
142    if (schema != null)
143    {
144      final String baseName = Attribute.getBaseName(valueString);
145      if (schema.getAttributeType(baseName) == null)
146      {
147        throw new ArgumentException(
148             ERR_ATTR_NAME_VALIDATOR_TYPE_NOT_DEFINED.get(valueString,
149                  argument.getIdentifierString(), baseName));
150      }
151    }
152  }
153
154
155
156  /**
157   * Retrieves a string representation of this argument value validator.
158   *
159   * @return  A string representation of this argument value validator.
160   */
161  @Override()
162  public String toString()
163  {
164    final StringBuilder buffer = new StringBuilder();
165    toString(buffer);
166    return buffer.toString();
167  }
168
169
170
171  /**
172   * Appends a string representation of this argument value validator to the
173   * provided buffer.
174   *
175   * @param  buffer  The buffer to which the string representation should be
176   *                 appended.
177   */
178  public void toString(final StringBuilder buffer)
179  {
180    buffer.append("AttributeNameArgumentValueValidator(allowOptions=");
181    buffer.append(allowOptions);
182    buffer.append(", hasSchema=");
183    buffer.append(schema != null);
184    buffer.append(')');
185  }
186}