001/*
002 * Copyright 2009-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2018 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.ldap.sdk.migrate.ldapjdk;
022
023
024
025import java.io.Serializable;
026import java.util.Arrays;
027import java.util.Enumeration;
028import java.util.Set;
029
030import com.unboundid.ldap.sdk.Attribute;
031import com.unboundid.util.Mutable;
032import com.unboundid.util.NotExtensible;
033import com.unboundid.util.StaticUtils;
034import com.unboundid.util.ThreadSafety;
035import com.unboundid.util.ThreadSafetyLevel;
036
037
038
039/**
040 * This class provides a data structure that holds information about an LDAP
041 * attribute, including an attribute description (a base name or OID and
042 * optional set of options) and zero or more values.
043 * <BR><BR>
044 * This class is primarily intended to be used in the process of updating
045 * applications which use the Netscape Directory SDK for Java to switch to or
046 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
047 * using the Netscape Directory SDK for Java, the {@link Attribute} class should
048 * be used instead.
049 */
050@NotExtensible()
051@Mutable()
052@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
053public class LDAPAttribute
054       implements Serializable
055{
056  /**
057   * The serial version UID for this serializable attribute.
058   */
059  private static final long serialVersionUID = 839217229050750570L;
060
061
062
063  // The Attribute object wrapped by this LDAPAttribute.
064  private Attribute attribute;
065
066
067
068  /**
069   * Creates a new LDAP attribute from the provided {@link Attribute} object.
070   *
071   * @param  attr  The LDAP attribute to use to create this attribute.
072   */
073  public LDAPAttribute(final Attribute attr)
074  {
075    attribute = attr;
076  }
077
078
079
080  /**
081   * Creates a new LDAP attribute that is a duplicate of the provided attribute.
082   *
083   * @param  attr  The LDAP attribute to use to create this attribute.
084   */
085  public LDAPAttribute(final LDAPAttribute attr)
086  {
087    attribute = attr.attribute;
088  }
089
090
091
092  /**
093   * Creates a new LDAP attribute with the specified name and no values.
094   *
095   * @param  attrName  The name for this attribute.
096   */
097  public LDAPAttribute(final String attrName)
098  {
099    attribute = new Attribute(attrName);
100  }
101
102
103
104  /**
105   * Creates a new LDAP attribute with the specified name and value.
106   *
107   * @param  attrName   The name for this attribute.
108   * @param  attrBytes  The value for this attribute.
109   */
110  public LDAPAttribute(final String attrName, final byte[] attrBytes)
111  {
112    attribute = new Attribute(attrName, attrBytes);
113  }
114
115
116
117  /**
118   * Creates a new LDAP attribute with the specified name and value.
119   *
120   * @param  attrName    The name for this attribute.
121   * @param  attrString  The value for this attribute.
122   */
123  public LDAPAttribute(final String attrName, final String attrString)
124  {
125    attribute = new Attribute(attrName, attrString);
126  }
127
128
129
130  /**
131   * Creates a new LDAP attribute with the specified name and values.
132   *
133   * @param  attrName     The name for this attribute.
134   * @param  attrStrings  The values for this attribute.
135   */
136  public LDAPAttribute(final String attrName, final String[] attrStrings)
137  {
138    attribute = new Attribute(attrName, attrStrings);
139  }
140
141
142
143  /**
144   * Retrieves the name for this attribute.
145   *
146   * @return  The name for this attribute.
147   */
148  public String getName()
149  {
150    return attribute.getName();
151  }
152
153
154
155  /**
156   * Retrieves the base name for this attribute, without any options.
157   *
158   * @return  The base name for this attribute.
159   */
160  public String getBaseName()
161  {
162    return attribute.getBaseName();
163  }
164
165
166
167  /**
168   * Retrieves the base name for the attribute with the provided name.
169   *
170   * @param  attrName  The attribute name for which to retrieve the base name.
171   *
172   * @return  The base name for the attribute with the provided name.
173   */
174  public static String getBaseName(final String attrName)
175  {
176    return Attribute.getBaseName(attrName);
177  }
178
179
180
181  /**
182   * Retrieves the subtypes (i.e., attribute options) contained in the name for
183   * this attribute.
184   *
185   * @return  The subtypes contained in the name for this attribute, or
186   *          {@code null} if there are none.
187   */
188  public String[] getSubtypes()
189  {
190    final Set<String> optionSet = attribute.getOptions();
191    if (optionSet.isEmpty())
192    {
193      return null;
194    }
195
196    final String[] options = new String[optionSet.size()];
197    return optionSet.toArray(options);
198  }
199
200
201
202  /**
203   * Retrieves the subtypes (i.e., attribute options) contained in the provided
204   * attribute name.
205   *
206   * @param  attrName  The attribute name from which to extract the subtypes.
207   *
208   * @return  The subtypes contained in the provided attribute name, or
209   *          {@code null} if there are none.
210   */
211  public static String[] getSubtypes(final String attrName)
212  {
213    return new LDAPAttribute(attrName).getSubtypes();
214  }
215
216
217
218  /**
219   * Retrieves the language subtype (i.e., the attribute option which begins
220   * with "lang-") for this attribute, if present.
221   *
222   * @return  The language subtype for this attribute, or {@code null} if there
223   *          is none.
224   */
225  public String getLangSubtype()
226  {
227    for (final String s : attribute.getOptions())
228    {
229      final String lowerName = StaticUtils.toLowerCase(s);
230      if (lowerName.startsWith("lang-"))
231      {
232        return s;
233      }
234    }
235
236    return null;
237  }
238
239
240
241  /**
242   * Indicates whether this attribute contains the specified subtype.
243   *
244   * @param  subtype  The subtype for which to make the determination.
245   *
246   * @return  {@code true} if this option has the specified subtype, or
247   *          {@code false} if not.
248   */
249  public boolean hasSubtype(final String subtype)
250  {
251    return attribute.hasOption(subtype);
252  }
253
254
255
256  /**
257   * Indicates whether this attribute contains all of the specified subtypes.
258   *
259   * @param  subtypes  The subtypes for which to make the determination.
260   *
261   * @return  {@code true} if this option has all of the specified subtypes, or
262   *          {@code false} if not.
263   */
264  public boolean hasSubtypes(final String[] subtypes)
265  {
266    for (final String s : subtypes)
267    {
268      if (! attribute.hasOption(s))
269      {
270        return false;
271      }
272    }
273
274    return true;
275  }
276
277
278
279  /**
280   * Retrieves an enumeration over the string values for this attribute.
281   *
282   * @return  An enumeration over the string values for this attribute.
283   */
284  public Enumeration<String> getStringValues()
285  {
286    return new IterableEnumeration<>(Arrays.asList(attribute.getValues()));
287  }
288
289
290
291  /**
292   * Retrieves an array of the values for this attribute.
293   *
294   * @return  An array of the values for this attribute.
295   */
296  public String[] getStringValueArray()
297  {
298    return attribute.getValues();
299  }
300
301
302
303  /**
304   * Retrieves an enumeration over the binary values for this attribute.
305   *
306   * @return  An enumeration over the binary values for this attribute.
307   */
308  public Enumeration<byte[]> getByteValues()
309  {
310    return new IterableEnumeration<>(
311         Arrays.asList(attribute.getValueByteArrays()));
312  }
313
314
315
316  /**
317   * Retrieves an array of the values for this attribute.
318   *
319   * @return  An array of the values for this attribute.
320   */
321  public byte[][] getByteValueArray()
322  {
323    return attribute.getValueByteArrays();
324  }
325
326
327
328  /**
329   * Adds the provided value to the set of values for this attribute.
330   *
331   * @param  attrString  The value to add to this attribute.
332   */
333  public void addValue(final String attrString)
334  {
335    attribute = Attribute.mergeAttributes(attribute,
336         new Attribute(attribute.getName(), attrString));
337  }
338
339
340
341  /**
342   * Adds the provided value to the set of values for this attribute.
343   *
344   * @param  attrBytes  The value to add to this attribute.
345   */
346  public void addValue(final byte[] attrBytes)
347  {
348    attribute = Attribute.mergeAttributes(attribute,
349         new Attribute(attribute.getName(), attrBytes));
350  }
351
352
353
354  /**
355   * Removes the provided value from this attribute.
356   *
357   * @param  attrValue  The value to remove.
358   */
359  public void removeValue(final String attrValue)
360  {
361    attribute = Attribute.removeValues(attribute,
362         new Attribute(attribute.getName(), attrValue));
363  }
364
365
366
367  /**
368   * Removes the provided value from this attribute.
369   *
370   * @param  attrValue  The value to remove.
371   */
372  public void removeValue(final byte[] attrValue)
373  {
374    attribute = Attribute.removeValues(attribute,
375         new Attribute(attribute.getName(), attrValue));
376  }
377
378
379
380  /**
381   * Retrieves the number of values for this attribute.
382   *
383   * @return  The number of values for this attribute.
384   */
385  public int size()
386  {
387    return attribute.size();
388  }
389
390
391
392  /**
393   * Converts this LDAP attribute to an {@link Attribute} object.
394   *
395   * @return  The {@code Attribute} object which corresponds to this LDAP
396   *          attribute.
397   */
398  public final Attribute toAttribute()
399  {
400    return attribute;
401  }
402
403
404
405  /**
406   * Retrieves a string representation of this LDAP attribute.
407   *
408   * @return  A string representation of this LDAP attribute.
409   */
410  @Override()
411  public String toString()
412  {
413    return attribute.toString();
414  }
415}