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