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