001/*
002 * Copyright 2009-2017 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2017 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 */
021package com.unboundid.ldap.sdk.migrate.ldapjdk;
022
023
024
025import com.unboundid.ldap.sdk.DN;
026import com.unboundid.ldap.sdk.RDN;
027import com.unboundid.util.NotMutable;
028import com.unboundid.util.ThreadSafety;
029import com.unboundid.util.ThreadSafetyLevel;
030
031import static com.unboundid.util.Debug.*;
032import static com.unboundid.util.StaticUtils.*;
033
034
035
036/**
037 * This class provides a set of utility methods for working with LDAP DNs.
038 * <BR><BR>
039 * This class is primarily intended to be used in the process of updating
040 * applications which use the Netscape Directory SDK for Java to switch to or
041 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
042 * using the Netscape Directory SDK for Java, the {@link DN} class should be
043 * used instead.
044 */
045@NotMutable()
046@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
047public final class LDAPDN
048{
049  /**
050   * Prevent this class from being instantiated.
051   */
052  private LDAPDN()
053  {
054    // No implementation required.
055  }
056
057
058
059  /**
060   * Retrieves a normalized representation of the provided DN.  If the provided
061   * string does not represent a valid distinguished name, then the value
062   * returned by this method will not be reliable.
063   *
064   * @param  dn  The string representation of the DN to be normalized.
065   *
066   * @return  A normalized representation of the provided DN.
067   */
068  public static String normalize(final String dn)
069  {
070    try
071    {
072      return DN.normalize(dn);
073    }
074    catch (Exception e)
075    {
076      debugException(e);
077      return toLowerCase(dn.trim());
078    }
079  }
080
081
082
083  /**
084   * Explodes the provided DN into individual RDN components.  If the provided
085   * string does not represent a valid distinguished name, then the value
086   * returned by this method will not be reliable.
087   *
088   * @param  dn       The DN to be exploded into its RDN components.
089   * @param  noTypes  Indicates whether to exclude the attribute names and
090   *                  equal signs and only include the values of the RDN
091   *                  components.
092   *
093   * @return  An exploded representation of the provided DN.
094   */
095  public static String[] explodeDN(final String dn, final boolean noTypes)
096  {
097    try
098    {
099      final RDN[] rdns = new DN(dn).getRDNs();
100      final String[] rdnStrings = new String[rdns.length];
101      for (int i=0; i < rdns.length; i++)
102      {
103        if (noTypes)
104        {
105          final StringBuilder buffer = new StringBuilder();
106          for (final String s : rdns[i].getAttributeValues())
107          {
108            if (buffer.length() > 0)
109            {
110              buffer.append('+');
111            }
112            buffer.append(s);
113          }
114          rdnStrings[i] = buffer.toString();
115        }
116        else
117        {
118          rdnStrings[i] = rdns[i].toString();
119        }
120      }
121      return rdnStrings;
122    }
123    catch (Exception e)
124    {
125      debugException(e);
126      return new String[] { dn };
127    }
128  }
129
130
131
132  /**
133   * Explodes the provided RDN into individual name-value pairs.  If the
134   * provided string does not represent a valid relative distinguished name,
135   * then the value returned by this method will not be reliable.
136   *
137   * @param  rdn      The RDN to be exploded into its name-value pairs.
138   * @param  noTypes  Indicates whether to exclude the attribute names and
139   *                  equal signs and only include the values of the components.
140   *
141   * @return  An exploded representation of the provided DN.
142   */
143  public static String[] explodeRDN(final String rdn, final boolean noTypes)
144  {
145    try
146    {
147      final RDN      rdnObject  = new RDN(rdn);
148
149      final String[] values = rdnObject.getAttributeValues();
150      if (noTypes)
151      {
152        return values;
153      }
154
155      final String[] names      = rdnObject.getAttributeNames();
156      final String[] returnStrs = new String[names.length];
157
158      for (int i=0; i < names.length; i++)
159      {
160        returnStrs[i] = names[i] + '=' + values[i];
161      }
162
163      return returnStrs;
164    }
165    catch (Exception e)
166    {
167      debugException(e);
168      return new String[] { rdn };
169    }
170  }
171
172
173
174  /**
175   * Indicates whether the provided strings represent the same distinguished
176   * name.
177   *
178   * @param  dn1  The first DN to be compared.
179   * @param  dn2  The second DN to be compared.
180   *
181   * @return  {@code true} if the provided strings represent the same
182   *          distinguished name, or {@code false} if not or if either of the
183   *          values cannot be parsed as a valid DN.
184   */
185  public static boolean equals(final String dn1, final String dn2)
186  {
187    try
188    {
189      return DN.equals(dn1, dn2);
190    }
191    catch (Exception e)
192    {
193      debugException(e);
194      return false;
195    }
196  }
197}