jdk/src/share/classes/javax/naming/ldap/LdapName.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5820 4f5e99470724
child 10324 e28265130e4f
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5820
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.naming.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.naming.Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.naming.InvalidNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class represents a distinguished name as specified by
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
    45
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A distinguished name, or DN, is composed of an ordered list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * components called <em>relative distinguished name</em>s, or RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Details of a DN's syntax are described in RFC 2253.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class resolves a few ambiguities found in RFC 2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <li> RFC 2253 leaves the term "whitespace" undefined. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      ASCII space character 0x20 (" ") is used in its place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li> Whitespace is allowed on either side of ',', ';', '=', and '+'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      Such whitespace is accepted but not generated by this code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      and is ignored when comparing names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <li> AttributeValue strings containing '=' or non-leading '#'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *      characters (unescaped) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * String names passed to <code>LdapName</code> or returned by it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * use the full Unicode character set. They may also contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * characters encoded into UTF-8 with each octet represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * three-character substring such as "\\B4".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * They may not, however, contain characters encoded into UTF-8 with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * each octet represented by a single character in the string:  the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * meaning would be ambiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <code>LdapName</code> will properly parse all valid names, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * does not attempt to detect all possible violations when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * invalid names.  It is "generous" in accepting invalid names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The "validity" of a name is determined ultimately when it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * is supplied to an LDAP server, which may accept or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * reject the name based on factors such as its schema information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * and interoperability considerations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * When names are tested for equality, attribute types, both binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * and string values, are case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * String values with different but equivalent usage of quoting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * escaping, or UTF8-hex-encoding are considered equal.  The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * components in multi-valued RDNs (such as "ou=Sales+cn=Bob") is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * The components of a LDAP name, that is, RDNs, are numbered. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * indexes of a LDAP name with n RDNs range from 0 to n-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * This range may be written as [0,n).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * The right most RDN is at index 0, and the left most RDN is at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * index n-1. For example, the distinguished name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * "CN=Steve Kille, O=Isode Limited, C=GB" is numbered in the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * sequence ranging from 0 to 2: {C=GB, O=Isode Limited, CN=Steve Kille}. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * empty LDAP name is represented by an empty RDN list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Concurrent multithreaded read-only access of an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <tt>LdapName</tt> need not be synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Unless otherwise noted, the behavior of passing a null argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * to a constructor or method in this class will cause a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * NullPointerException to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
public class LdapName implements Name {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // private transient ArrayList<Rdn> rdns;   // parsed name components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private transient ArrayList rdns;   // parsed name components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private transient String unparsed;  // if non-null, the DN in unparsed form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final long serialVersionUID = -1595520034788997356L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Constructs an LDAP name from the given distinguished name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param name  This is a non-null distinguished name formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * according to the rules defined in
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   118
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @throws InvalidNameException if a syntax violation is detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see Rdn#escapeValue(Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public LdapName(String name) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        unparsed = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Constructs an LDAP name given its parsed RDN components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The indexing of RDNs in the list follows the numbering of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * RDNs described in the class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param rdns The non-null list of <tt>Rdn</tt>s forming this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public LdapName(List<Rdn> rdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // if (rdns instanceof ArrayList<Rdn>) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        //      this.rdns = rdns.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // } else if (rdns instanceof List<Rdn>) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        //      this.rdns = new ArrayList<Rdn>(rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //      throw IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        //              "Invalid entries, list entries must be of type Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        //  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.rdns = new ArrayList(rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (int i = 0; i < rdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            Object obj = rdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throw new IllegalArgumentException("Entry:" + obj +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        "  not a valid type;list entries must be of type Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            this.rdns.add(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Constructs an LDAP name given its parsed components (the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * of "rdns" in the range [beg,end)) and, optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * (if "name" is not null), the unparsed DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // private LdapName(String name, List<Rdn> rdns, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private LdapName(String name, ArrayList rdns, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        unparsed = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // this.rdns = rdns.subList(beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        List sList = rdns.subList(beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.rdns = new ArrayList(sList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Retrieves the number of components in this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return The non-negative number of components in this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Determines whether this LDAP name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * An empty name is one with zero components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return true if this LDAP name is empty, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return rdns.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Retrieves the components of this name as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * of strings. The effect of updates to this name on this enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * is undefined. If the name has zero components, an empty (non-null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * enumeration is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The order of the components returned by the enumeration is same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * the order in which the components are numbered as described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return A non-null enumeration of the components of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Each element of the enumeration is of class String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public Enumeration<String> getAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        final Iterator iter = rdns.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return new Enumeration<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return iter.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            public String nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                return iter.next().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Retrieves a component of this LDAP name as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param  posn The 0-based index of the component to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *              Must be in the range [0,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return The non-null component at index posn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @exception IndexOutOfBoundsException if posn is outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *          specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public String get(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return rdns.get(posn).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Retrieves an RDN of this LDAP name as an Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param   posn The 0-based index of the RDN to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *          Must be in the range [0,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return The non-null RDN at index posn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception IndexOutOfBoundsException if posn is outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *            specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public Rdn getRdn(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return (Rdn) rdns.get(posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Creates a name whose components consist of a prefix of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * components of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Subsequent changes to this name will not affect the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * that is returned and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param  posn     The 0-based index of the component at which to stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return  An instance of <tt>LdapName</tt> consisting of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *          components at indexes in the range [0,posn).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *          If posn is zero, an empty LDAP name is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception   IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *              If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public Name getPrefix(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return new LdapName(null, rdns, 0, posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                "Posn: " + posn + ", Size: "+ rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Creates a name whose components consist of a suffix of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * components in this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Subsequent changes to this name do not affect the name that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * returned and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param  posn     The 0-based index of the component at which to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return  An instance of <tt>LdapName</tt> consisting of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *          components at indexes in the range [posn,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          If posn is equal to size(), an empty LDAP name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public Name getSuffix(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return new LdapName(null, rdns, posn, rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                "Posn: " + posn + ", Size: "+ rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Determines whether this LDAP name starts with a specified LDAP name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * A name <tt>n</tt> is a prefix if it is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <tt>getPrefix(n.size())</tt>--in other words this LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * name starts with 'n'. If n is null or not a RFC2253 formatted name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * as described in the class description, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param n The LDAP name to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return  true if <tt>n</tt> is a prefix of this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #getPrefix(int posn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public boolean startsWith(Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int len1 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int len2 = n.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                matches(0, len2, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Determines whether the specified RDN sequence forms a prefix of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * LDAP name.  Returns true if this LdapName is at least as long as rdns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * and for every position p in the range [0, rdns.size()) the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * getRdn(p) matches rdns.get(p). Returns false otherwise. If rdns is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * null, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param rdns The sequence of <tt>Rdn</tt>s to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @return  true if <tt>rdns</tt> form a prefix of this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public boolean startsWith(List<Rdn> rdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (rdns == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int len1 = this.rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int len2 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                doesListMatch(0, len2, rdns));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Determines whether this LDAP name ends with a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * LDAP name suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * A name <tt>n</tt> is a suffix if it is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <tt>getSuffix(size()-n.size())</tt>--in other words this LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * name ends with 'n'. If n is null or not a RFC2253 formatted name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * as described in the class description, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param n The LDAP name to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return true if <tt>n</tt> is a suffix of this name, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @see #getSuffix(int posn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public boolean endsWith(Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int len1 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int len2 = n.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                matches(len1 - len2, len1, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Determines whether the specified RDN sequence forms a suffix of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * LDAP name.  Returns true if this LdapName is at least as long as rdns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * and for every position p in the range [size() - rdns.size(), size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * the component getRdn(p) matches rdns.get(p). Returns false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * If rdns is null, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param rdns The sequence of <tt>Rdn</tt>s to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @return  true if <tt>rdns</tt> form a suffix of this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public boolean endsWith(List<Rdn> rdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (rdns == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        int len1 = this.rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int len2 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                doesListMatch(len1 - len2, len1, rdns));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private boolean doesListMatch(int beg, int end, List rdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (!this.rdns.get(i).equals(rdns.get(i - beg))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Helper method for startsWith() and endsWith().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Returns true if components [beg,end) match the components of "n".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * If "n" is not an LdapName, each of its components is parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * the string form of an RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * The following must hold:  end - beg == n.size().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private boolean matches(int beg, int end, Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (n instanceof LdapName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            LdapName ln = (LdapName) n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return doesListMatch(beg, end, ln.rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                Rdn rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                String rdnString = n.get(i - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    rdn = (new Rfc2253Parser(rdnString)).parseRdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (!rdn.equals(rdns.get(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Adds the components of a name -- in order -- to the end of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param   suffix The non-null components to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @return  The updated name (not a new instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @throws  InvalidNameException if <tt>suffix</tt> is not a valid LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *          name, or if the addition of the components would violate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *          syntax rules of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public Name addAll(Name suffix) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         return addAll(size(), suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Adds the RDNs of a name -- in order -- to the end of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param   suffixRdns The non-null suffix <tt>Rdn</tt>s to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @return  The updated name (not a new instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public Name addAll(List<Rdn> suffixRdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return addAll(size(), suffixRdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Adds the components of a name -- in order -- at a specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * within this name. Components of this LDAP name at or after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * index (if any) of the first new component are shifted up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * (away from index 0) to accomodate the new components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param suffix    The non-null components to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param posn      The index at which to add the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @return  The updated name (not a new instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @throws  InvalidNameException if <tt>suffix</tt> is not a valid LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *          name, or if the addition of the components would violate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *          syntax rules of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @throws  IndexOutOfBoundsException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *          If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public Name addAll(int posn, Name suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (suffix instanceof LdapName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            LdapName s = (LdapName) suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            rdns.addAll(posn, s.rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            Enumeration comps = suffix.getAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            while (comps.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                rdns.add(posn++,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    (new Rfc2253Parser((String) comps.nextElement()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    parseRdn()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Adds the RDNs of a name -- in order -- at a specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * within this name. RDNs of this LDAP name at or after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * index (if any) of the first new RDN are shifted up (away from index 0) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * accomodate the new RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @param suffixRdns        The non-null suffix <tt>Rdn</tt>s to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param posn              The index at which to add the suffix RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *                          Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @return  The updated name (not a new instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @throws  IndexOutOfBoundsException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *          If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public Name addAll(int posn, List<Rdn> suffixRdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        unparsed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        for (int i = 0; i < suffixRdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            Object obj = suffixRdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                throw new IllegalArgumentException("Entry:" + obj +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                "  not a valid type;suffix list entries must be of type Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            rdns.add(i + posn, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Adds a single component to the end of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param comp      The non-null component to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *                  Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @exception       InvalidNameException If adding comp at end of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *                  would violate the name's syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public Name add(String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        return add(size(), comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Adds a single RDN to the end of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param comp      The non-null RDN to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *                  Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public Name add(Rdn comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return add(size(), comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Adds a single component at a specified position within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Components of this LDAP name at or after the index (if any) of the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * component are shifted up by one (away from index 0) to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param  comp     The non-null component to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @param  posn     The index at which to add the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *                  Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @exception       IndexOutOfBoundsException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *                  If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @exception       InvalidNameException If adding comp at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *                  specified position would violate the name's syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public Name add(int posn, String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        Rdn rdn = (new Rfc2253Parser(comp)).parseRdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        rdns.add(posn, rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Adds a single RDN at a specified position within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * RDNs of this LDAP name at or after the index (if any) of the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * RDN are shifted up by one (away from index 0) to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * the new RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param  comp     The non-null RDN to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param  posn     The index at which to add the new RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *                  Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @exception       IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *                  If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public Name add(int posn, Rdn comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            throw new NullPointerException("Cannot set comp to null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        rdns.add(posn, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Removes a component from this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * The component of this name at the specified position is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Components with indexes greater than this position (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * are shifted down (toward index 0) by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param posn      The index of the component to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *                  Must be in the range [0,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @return          The component removed (a String).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @throws          IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *                  if posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @throws          InvalidNameException if deleting the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *                  would violate the syntax rules of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public Object remove(int posn) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return rdns.remove(posn).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Retrieves the list of relative distinguished names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * The contents of the list are unmodifiable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * The indexing of RDNs in the returned list follows the numbering of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * RDNs as described in the class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * If the name has zero components, an empty list is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @return  The name as a list of RDNs which are instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *          the class {@link Rdn Rdn}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public List<Rdn> getRdns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return Collections.unmodifiableList(rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Generates a new copy of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Subsequent changes to the components of this name will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * affect the new copy, and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @return A copy of the this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return new LdapName(unparsed, rdns, 0, rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * Returns a string representation of this LDAP name in a format
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   617
     * defined by <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * and described in the class description. If the name has zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * components an empty string is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @return The string representation of the LdapName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (unparsed != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            return unparsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        StringBuilder builder = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        int size = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if ((size - 1) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            builder.append((Rdn) rdns.get(size - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        for (int next = size - 2; next >= 0; next--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            builder.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            builder.append((Rdn) rdns.get(next));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        unparsed = builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return unparsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Determines whether two LDAP names are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * If obj is null or not an LDAP name, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Two LDAP names are equal if each RDN in one is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * to the corresponding RDN in the other. This implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * both have the same number of RDNs, and each RDN's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * equals() test against the corresponding RDN in the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * name returns true. See {@link Rdn#equals(Object obj)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * for a definition of RDN equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param  obj      The possibly null object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @return          true if obj is equal to this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *                  false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @see #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        // check possible shortcuts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (!(obj instanceof LdapName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        LdapName that = (LdapName) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (rdns.size() != that.rdns.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (unparsed != null && unparsed.equalsIgnoreCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                that.unparsed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        // Compare RDNs one by one for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        for (int i = 0; i < rdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            // Compare a single pair of RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            Rdn rdn1 = (Rdn) rdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            Rdn rdn2 = (Rdn) that.rdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            if (!rdn1.equals(rdn2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Compares this LdapName with the specified Object for order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Returns a negative integer, zero, or a positive integer as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Name is less than, equal to, or greater than the given Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * If obj is null or not an instance of LdapName, ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Ordering of LDAP names follows the lexicographical rules for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * string comparison, with the extension that this applies to all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * the RDNs in the LDAP name. All the RDNs are lined up in their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * specified order and compared lexicographically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * See {@link Rdn#compareTo(Object obj) Rdn.compareTo(Object obj)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * for RDN comparison rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * If this LDAP name is lexicographically lesser than obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * a negative number is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * If this LDAP name is lexicographically greater than obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * a positive number is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @param obj The non-null LdapName instance to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @return  A negative integer, zero, or a positive integer as this Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *          is less than, equal to, or greater than the given obj.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @exception ClassCastException if obj is null or not a LdapName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (!(obj instanceof LdapName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            throw new ClassCastException("The obj is not a LdapName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        // check possible shortcuts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        LdapName that = (LdapName) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (unparsed != null && unparsed.equalsIgnoreCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        that.unparsed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        // Compare RDNs one by one, lexicographically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        int minSize = Math.min(rdns.size(), that.rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        for (int i = 0; i < minSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            // Compare a single pair of RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            Rdn rdn1 = (Rdn)rdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            Rdn rdn2 = (Rdn)that.rdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            int diff = rdn1.compareTo(rdn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        return (rdns.size() - that.rdns.size());        // longer DN wins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * Computes the hash code of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * The hash code is the sum of the hash codes of individual RDNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * of this  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @return An int representing the hash code of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @see #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        // Sum up the hash codes of the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // For each RDN...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        for (int i = 0; i < rdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            Rdn rdn = (Rdn) rdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            hash += rdn.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * Serializes only the unparsed DN, for compactness and to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * any implementation dependency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @serialData      The DN string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        s.writeObject(toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        unparsed = (String)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    "Invalid name: " + unparsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    private void parse() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        // rdns = (ArrayList<Rdn>) (new RFC2253Parser(unparsed)).getDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        rdns = (ArrayList) (new Rfc2253Parser(unparsed)).parseDn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
}