jdk/src/share/classes/javax/naming/ldap/LdapName.java
author alanb
Mon, 10 Jun 2013 12:58:32 +0100
changeset 18156 edb590d448c5
parent 10324 e28265130e4f
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
8016217: More javadoc warnings Reviewed-by: lancea, chegar, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
     2
 * Copyright (c) 2003, 2011, 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
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   107
    private transient List<Rdn> rdns;   // parsed name components
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private transient String unparsed;  // if non-null, the DN in unparsed form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final long serialVersionUID = -1595520034788997356L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Constructs an LDAP name from the given distinguished name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param name  This is a non-null distinguished name formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * according to the rules defined in
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
   116
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws InvalidNameException if a syntax violation is detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @see Rdn#escapeValue(Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public LdapName(String name) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        unparsed = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Constructs an LDAP name given its parsed RDN components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The indexing of RDNs in the list follows the numbering of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * RDNs described in the class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param rdns The non-null list of <tt>Rdn</tt>s forming this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public LdapName(List<Rdn> rdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // if (rdns instanceof ArrayList<Rdn>) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        //      this.rdns = rdns.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // } else if (rdns instanceof List<Rdn>) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        //      this.rdns = new ArrayList<Rdn>(rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        //      throw IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        //              "Invalid entries, list entries must be of type Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   145
        this.rdns = new ArrayList<>(rdns.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for (int i = 0; i < rdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Object obj = rdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throw new IllegalArgumentException("Entry:" + obj +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        "  not a valid type;list entries must be of type Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   152
            this.rdns.add((Rdn)obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Constructs an LDAP name given its parsed components (the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * of "rdns" in the range [beg,end)) and, optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * (if "name" is not null), the unparsed DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   162
    private LdapName(String name, List<Rdn> rdns, int beg, int end) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        unparsed = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // this.rdns = rdns.subList(beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   166
        List<Rdn> sList = rdns.subList(beg, end);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   167
        this.rdns = new ArrayList<>(sList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Retrieves the number of components in this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return The non-negative number of components in this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Determines whether this LDAP name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * An empty name is one with zero components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return true if this LDAP name is empty, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return rdns.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Retrieves the components of this name as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * of strings. The effect of updates to this name on this enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * is undefined. If the name has zero components, an empty (non-null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * enumeration is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The order of the components returned by the enumeration is same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * the order in which the components are numbered as described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return A non-null enumeration of the components of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Each element of the enumeration is of class String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public Enumeration<String> getAll() {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   200
        final Iterator<Rdn> iter = rdns.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return new Enumeration<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                return iter.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            public String nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                return iter.next().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Retrieves a component of this LDAP name as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param  posn The 0-based index of the component to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *              Must be in the range [0,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return The non-null component at index posn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @exception IndexOutOfBoundsException if posn is outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *          specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public String get(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return rdns.get(posn).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Retrieves an RDN of this LDAP name as an Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param   posn The 0-based index of the RDN to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *          Must be in the range [0,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return The non-null RDN at index posn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception IndexOutOfBoundsException if posn is outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *            specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public Rdn getRdn(int posn) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   233
        return rdns.get(posn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Creates a name whose components consist of a prefix of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * components of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Subsequent changes to this name will not affect the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * that is returned and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param  posn     The 0-based index of the component at which to stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return  An instance of <tt>LdapName</tt> consisting of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *          components at indexes in the range [0,posn).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *          If posn is zero, an empty LDAP name is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @exception   IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *              If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public Name getPrefix(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return new LdapName(null, rdns, 0, posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                "Posn: " + posn + ", Size: "+ rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Creates a name whose components consist of a suffix of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * components in this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Subsequent changes to this name do not affect the name that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * returned and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param  posn     The 0-based index of the component at which to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return  An instance of <tt>LdapName</tt> consisting of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          components at indexes in the range [posn,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          If posn is equal to size(), an empty LDAP name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @exception IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *          If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public Name getSuffix(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return new LdapName(null, rdns, posn, rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                "Posn: " + posn + ", Size: "+ rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Determines whether this LDAP name starts with a specified LDAP name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * A name <tt>n</tt> is a prefix if it is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <tt>getPrefix(n.size())</tt>--in other words this LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * name starts with 'n'. If n is null or not a RFC2253 formatted name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * as described in the class description, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param n The LDAP name to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return  true if <tt>n</tt> is a prefix of this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @see #getPrefix(int posn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public boolean startsWith(Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int len1 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int len2 = n.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                matches(0, len2, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Determines whether the specified RDN sequence forms a prefix of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * LDAP name.  Returns true if this LdapName is at least as long as rdns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * and for every position p in the range [0, rdns.size()) the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * getRdn(p) matches rdns.get(p). Returns false otherwise. If rdns is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * null, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param rdns The sequence of <tt>Rdn</tt>s to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return  true if <tt>rdns</tt> form a prefix of this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *          false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public boolean startsWith(List<Rdn> rdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (rdns == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int len1 = this.rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int len2 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                doesListMatch(0, len2, rdns));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Determines whether this LDAP name ends with a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * LDAP name suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * A name <tt>n</tt> is a suffix if it is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <tt>getSuffix(size()-n.size())</tt>--in other words this LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * name ends with 'n'. If n is null or not a RFC2253 formatted name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * as described in the class description, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param n The LDAP name to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return true if <tt>n</tt> is a suffix of this name, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see #getSuffix(int posn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public boolean endsWith(Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int len1 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int len2 = n.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                matches(len1 - len2, len1, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Determines whether the specified RDN sequence forms a suffix of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * LDAP name.  Returns true if this LdapName is at least as long as rdns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * and for every position p in the range [size() - rdns.size(), size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * the component getRdn(p) matches rdns.get(p). Returns false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * If rdns is null, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param rdns The sequence of <tt>Rdn</tt>s to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return  true if <tt>rdns</tt> form a suffix of this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *          false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public boolean endsWith(List<Rdn> rdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (rdns == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        int len1 = this.rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int len2 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                doesListMatch(len1 - len2, len1, rdns));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   369
    private boolean doesListMatch(int beg, int end, List<Rdn> rdns) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (!this.rdns.get(i).equals(rdns.get(i - beg))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Helper method for startsWith() and endsWith().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Returns true if components [beg,end) match the components of "n".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * If "n" is not an LdapName, each of its components is parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * the string form of an RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * The following must hold:  end - beg == n.size().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private boolean matches(int beg, int end, Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (n instanceof LdapName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            LdapName ln = (LdapName) n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return doesListMatch(beg, end, ln.rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                Rdn rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                String rdnString = n.get(i - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    rdn = (new Rfc2253Parser(rdnString)).parseRdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                if (!rdn.equals(rdns.get(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return true;
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
     * Adds the components of a name -- in order -- to the end of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param   suffix The non-null components to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return  The updated name (not a new instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @throws  InvalidNameException if <tt>suffix</tt> is not a valid LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *          name, or if the addition of the components would violate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *          syntax rules of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public Name addAll(Name suffix) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         return addAll(size(), suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Adds the RDNs of a name -- in order -- to the end of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param   suffixRdns The non-null suffix <tt>Rdn</tt>s to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @return  The updated name (not a new instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public Name addAll(List<Rdn> suffixRdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return addAll(size(), suffixRdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Adds the components of a name -- in order -- at a specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * within this name. Components of this LDAP name at or after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * index (if any) of the first new component are shifted up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * (away from index 0) to accomodate the new components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param suffix    The non-null components to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param posn      The index at which to add the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return  The updated name (not a new instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @throws  InvalidNameException if <tt>suffix</tt> is not a valid LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *          name, or if the addition of the components would violate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *          syntax rules of this LDAP name.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 10324
diff changeset
   446
     * @throws  IndexOutOfBoundsException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *          If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public Name addAll(int posn, Name suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (suffix instanceof LdapName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            LdapName s = (LdapName) suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            rdns.addAll(posn, s.rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        } else {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   456
            Enumeration<String> comps = suffix.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            while (comps.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                rdns.add(posn++,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   459
                    (new Rfc2253Parser(comps.nextElement()).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    parseRdn()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Adds the RDNs of a name -- in order -- at a specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * within this name. RDNs of this LDAP name at or after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * index (if any) of the first new RDN are shifted up (away from index 0) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * accomodate the new RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param suffixRdns        The non-null suffix <tt>Rdn</tt>s to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param posn              The index at which to add the suffix RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *                          Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @return  The updated name (not a new instance).
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 10324
diff changeset
   477
     * @throws  IndexOutOfBoundsException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *          If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public Name addAll(int posn, List<Rdn> suffixRdns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        unparsed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        for (int i = 0; i < suffixRdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            Object obj = suffixRdns.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (!(obj instanceof Rdn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                throw new IllegalArgumentException("Entry:" + obj +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                "  not a valid type;suffix list entries must be of type Rdn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   488
            rdns.add(i + posn, (Rdn)obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Adds a single component to the end of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param comp      The non-null component to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *                  Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @exception       InvalidNameException If adding comp at end of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *                  would violate the name's syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public Name add(String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return add(size(), comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Adds a single RDN to the end of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param comp      The non-null RDN to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *                  Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public Name add(Rdn comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return add(size(), comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Adds a single component at a specified position within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Components of this LDAP name at or after the index (if any) of the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * component are shifted up by one (away from index 0) to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param  comp     The non-null component to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param  posn     The index at which to add the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *                  Cannot be null.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 10324
diff changeset
   530
     * @exception       IndexOutOfBoundsException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *                  If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @exception       InvalidNameException If adding comp at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *                  specified position would violate the name's syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public Name add(int posn, String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        Rdn rdn = (new Rfc2253Parser(comp)).parseRdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        rdns.add(posn, rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Adds a single RDN at a specified position within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * RDNs of this LDAP name at or after the index (if any) of the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * RDN are shifted up by one (away from index 0) to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * the new RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param  comp     The non-null RDN to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param  posn     The index at which to add the new RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *                  Must be in the range [0,size()].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @return          The updated LdapName, not a new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *                  Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @exception       IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *                  If posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public Name add(int posn, Rdn comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            throw new NullPointerException("Cannot set comp to null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        rdns.add(posn, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Removes a component from this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * The component of this name at the specified position is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Components with indexes greater than this position (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * are shifted down (toward index 0) by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param posn      The index of the component to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *                  Must be in the range [0,size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @return          The component removed (a String).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @throws          IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *                  if posn is outside the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @throws          InvalidNameException if deleting the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *                  would violate the syntax rules of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public Object remove(int posn) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        unparsed = null;        // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return rdns.remove(posn).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Retrieves the list of relative distinguished names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * The contents of the list are unmodifiable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * The indexing of RDNs in the returned list follows the numbering of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * RDNs as described in the class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * If the name has zero components, an empty list is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return  The name as a list of RDNs which are instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *          the class {@link Rdn Rdn}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public List<Rdn> getRdns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return Collections.unmodifiableList(rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Generates a new copy of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Subsequent changes to the components of this name will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * affect the new copy, and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return A copy of the this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return new LdapName(unparsed, rdns, 0, rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * 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
   613
     * defined by <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * and described in the class description. If the name has zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * components an empty string is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @return The string representation of the LdapName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (unparsed != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return unparsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        StringBuilder builder = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        int size = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if ((size - 1) >= 0) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   626
            builder.append(rdns.get(size - 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        for (int next = size - 2; next >= 0; next--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            builder.append(',');
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   630
            builder.append(rdns.get(next));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        unparsed = builder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return unparsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Determines whether two LDAP names are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * If obj is null or not an LDAP name, false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Two LDAP names are equal if each RDN in one is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * to the corresponding RDN in the other. This implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * both have the same number of RDNs, and each RDN's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * equals() test against the corresponding RDN in the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * name returns true. See {@link Rdn#equals(Object obj)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * for a definition of RDN equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param  obj      The possibly null object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @return          true if obj is equal to this LDAP name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *                  false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @see #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        // check possible shortcuts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (!(obj instanceof LdapName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        LdapName that = (LdapName) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (rdns.size() != that.rdns.size()) {
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
        if (unparsed != null && unparsed.equalsIgnoreCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                that.unparsed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        // Compare RDNs one by one for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        for (int i = 0; i < rdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            // Compare a single pair of RDNs.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   671
            Rdn rdn1 = rdns.get(i);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   672
            Rdn rdn2 = that.rdns.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            if (!rdn1.equals(rdn2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Compares this LdapName with the specified Object for order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Returns a negative integer, zero, or a positive integer as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Name is less than, equal to, or greater than the given Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * If obj is null or not an instance of LdapName, ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Ordering of LDAP names follows the lexicographical rules for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * string comparison, with the extension that this applies to all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * the RDNs in the LDAP name. All the RDNs are lined up in their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * specified order and compared lexicographically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * See {@link Rdn#compareTo(Object obj) Rdn.compareTo(Object obj)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * for RDN comparison rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * If this LDAP name is lexicographically lesser than obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * a negative number is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * If this LDAP name is lexicographically greater than obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * a positive number is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @param obj The non-null LdapName instance to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @return  A negative integer, zero, or a positive integer as this Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *          is less than, equal to, or greater than the given obj.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @exception ClassCastException if obj is null or not a LdapName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (!(obj instanceof LdapName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            throw new ClassCastException("The obj is not a LdapName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        // check possible shortcuts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        LdapName that = (LdapName) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (unparsed != null && unparsed.equalsIgnoreCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                        that.unparsed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        // Compare RDNs one by one, lexicographically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        int minSize = Math.min(rdns.size(), that.rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        for (int i = 0; i < minSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            // Compare a single pair of RDNs.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   726
            Rdn rdn1 = rdns.get(i);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   727
            Rdn rdn2 = that.rdns.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            int diff = rdn1.compareTo(rdn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        return (rdns.size() - that.rdns.size());        // longer DN wins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Computes the hash code of this LDAP name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * The hash code is the sum of the hash codes of individual RDNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * of this  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @return An int representing the hash code of this name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @see #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        // Sum up the hash codes of the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        // For each RDN...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        for (int i = 0; i < rdns.size(); i++) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   751
            Rdn rdn = rdns.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            hash += rdn.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Serializes only the unparsed DN, for compactness and to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * any implementation dependency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @serialData      The DN string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        s.writeObject(toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        unparsed = (String)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    "Invalid name: " + unparsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    private void parse() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // rdns = (ArrayList<Rdn>) (new RFC2253Parser(unparsed)).getDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   785
        rdns = new Rfc2253Parser(unparsed).parseDn();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
}