jdk/src/share/classes/com/sun/jndi/ldap/LdapName.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 10369 e9d2e59e53f0
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
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: 5506
diff changeset
     2
 * Copyright (c) 1999, 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 com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
    31
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.directory.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.directory.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.naming.directory.BasicAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>LdapName</code> implements compound names for LDAP v3 as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * specified by RFC 2253.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * RFC 2253 has a few ambiguities and outright inconsistencies.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * are resolved as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <li> RFC 2253 leaves the term "whitespace" undefined.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      definition of "optional-space" given in RFC 1779 is used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      its place:  either a space character or a carriage return ("\r").
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li> Whitespace is allowed on either side of ',', ';', '=', and '+'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      Such whitespace is accepted but not generated by this code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      and is ignored when comparing names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <li> AttributeValue strings containing '=' or non-leading '#'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      characters (unescaped) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * String names passed to <code>LdapName</code> or returned by it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * use the full 16-bit Unicode character set.  They may also contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * characters encoded into UTF-8 with each octet represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * three-character substring such as "\\B4".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * They may not, however, contain characters encoded into UTF-8 with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * each octet represented by a single character in the string:  the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * meaning would be ambiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>LdapName</code> will properly parse all valid names, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * does not attempt to detect all possible violations when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * invalid names.  It's "generous".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * When names are tested for equality, attribute types and binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * values are case-insensitive, and string values are by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * String values with different but equivalent usage of quoting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * escaping, or UTF8-hex-encoding are considered equal.  The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * components in multi-valued RDNs (such as "ou=Sales+cn=Bob") is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public final class LdapName implements Name {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private transient String unparsed;  // if non-null, the DN in unparsed form
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    82
    private transient Vector<Rdn> rdns;      // parsed name components
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private transient boolean valuesCaseSensitive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Constructs an LDAP name from the given DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param name      An LDAP DN.  To JNDI, a compound name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @throws InvalidNameException if a syntax violation is detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public LdapName(String name) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        unparsed = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Constructs an LDAP name given its parsed components and, optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * (if "name" is not null), the unparsed DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   101
    @SuppressWarnings("unchecked") // clone()
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   102
    private LdapName(String name, Vector<Rdn> rdns) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        unparsed = name;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   104
        this.rdns = (Vector<Rdn>)rdns.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Constructs an LDAP name given its parsed components (the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * of "rdns" in the range [beg,end)) and, optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * (if "name" is not null), the unparsed DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   112
    private LdapName(String name, Vector<Rdn> rdns, int beg, int end) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        unparsed = name;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   114
        this.rdns = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            this.rdns.addElement(rdns.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return new LdapName(unparsed, rdns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (unparsed != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return unparsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        for (int i = rdns.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (i < rdns.size() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                buf.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   135
            Rdn rdn = rdns.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            buf.append(rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        unparsed = new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return unparsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return ((obj instanceof LdapName) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                (compareTo(obj) == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        LdapName that = (LdapName)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if ((obj == this) ||                    // check possible shortcuts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            (unparsed != null && unparsed.equals(that.unparsed))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Compare RDNs one by one, lexicographically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        int minSize = Math.min(rdns.size(), that.rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (int i = 0 ; i < minSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // Compare a single pair of RDNs.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   160
            Rdn rdn1 = rdns.elementAt(i);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   161
            Rdn rdn2 = that.rdns.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            int diff = rdn1.compareTo(rdn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return (rdns.size() - that.rdns.size());        // longer DN wins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Sum up the hash codes of the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // For each RDN...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int i = 0; i < rdns.size(); i++) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   177
            Rdn rdn = rdns.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            hash += rdn.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
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
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   191
    public Enumeration<String> getAll() {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   192
        final Enumeration<Rdn> enum_ = rdns.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   194
        return new Enumeration<String>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return enum_.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   198
            public String nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return enum_.nextElement().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public String get(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return rdns.elementAt(pos).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public Name getPrefix(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return new LdapName(null, rdns, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public Name getSuffix(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return new LdapName(null, rdns, pos, rdns.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public boolean startsWith(Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int len1 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int len2 = n.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                matches(0, len2, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public boolean endsWith(Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int len1 = rdns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int len2 = n.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return (len1 >= len2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                matches(len1 - len2, len1, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Controls whether string-values are treated as case-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * when the string values within names are compared.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * behavior is case-insensitive comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     public void setValuesCaseSensitive(boolean caseSensitive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         rdns = null;   // clear any cached information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
             parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
             // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
             throw new IllegalStateException("Cannot parse name: " + unparsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         valuesCaseSensitive = caseSensitive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Helper method for startsWith() and endsWith().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Returns true if components [beg,end) match the components of "n".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * If "n" is not an LdapName, each of its components is parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * the string form of an RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * The following must hold:  end - beg == n.size().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private boolean matches(int beg, int end, Name n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            Rdn rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (n instanceof LdapName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                LdapName ln = (LdapName)n;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   259
                rdn = ln.rdns.elementAt(i - beg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                String rdnString = n.get(i - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    rdn = (new DnParser(rdnString, valuesCaseSensitive)).getRdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (!rdn.equals(rdns.elementAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public Name addAll(Name suffix) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return addAll(size(), suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * If "suffix" is not an LdapName, each of its components is parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * the string form of an RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public Name addAll(int pos, Name suffix) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (suffix instanceof LdapName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            LdapName s = (LdapName)suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            for (int i = 0; i < s.rdns.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                rdns.insertElementAt(s.rdns.elementAt(i), pos++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } else {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   291
            Enumeration<String> comps = suffix.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            while (comps.hasMoreElements()) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   293
                DnParser p = new DnParser(comps.nextElement(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    valuesCaseSensitive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                rdns.insertElementAt(p.getRdn(), pos++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        unparsed = null;                                // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public Name add(String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return add(size(), comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public Name add(int pos, String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Rdn rdn = (new DnParser(comp, valuesCaseSensitive)).getRdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        rdns.insertElementAt(rdn, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        unparsed = null;                                // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public Object remove(int pos) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        String comp = get(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        rdns.removeElementAt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        unparsed = null;                                // no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private void parse() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        rdns = (new DnParser(unparsed, valuesCaseSensitive)).getDn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Best guess as to what RFC 2253 means by "whitespace".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private static boolean isWhitespace(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return (c == ' ' || c == '\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Given the value of an attribute, returns a string suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * for inclusion in a DN.  If the value is a string, this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * accomplished by using backslash (\) to escape the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * characters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *<li>leading and trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *<li><pre>, = + < > # ; " \</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * If the value is a byte array, it is converted to hex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * notation (such as "#CEB1DF80").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public static String escapeAttributeValue(Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return TypeAndValue.escapeValue(val);
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
     * Given an attribute value formated according to RFC 2253,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * returns the unformated value.  Returns a string value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * a string, and a binary value as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public static Object unescapeAttributeValue(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return TypeAndValue.unescapeValue(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Serializes only the unparsed DN, for compactness and to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * any implementation dependency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @serialdata      The DN string and a boolean indicating whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * the values are case sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        s.writeObject(toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        s.writeBoolean(valuesCaseSensitive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        unparsed = (String)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        valuesCaseSensitive = s.readBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    "Invalid name: " + unparsed);
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
    static final long serialVersionUID = -1595520034788997356L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * DnParser implements a recursive descent parser for a single DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    static class DnParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        private final String name;      // DN being parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        private final char[] chars;     // characters in LDAP name being parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        private final int len;          // length of "chars"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        private int cur = 0;            // index of first unconsumed char in "chars"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        private boolean valuesCaseSensitive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * Given an LDAP DN in string form, returns a parser for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        DnParser(String name, boolean valuesCaseSensitive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            len = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            chars = name.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            this.valuesCaseSensitive = valuesCaseSensitive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * Parses the DN, returning a Vector of its RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   411
        Vector<Rdn> getDn() throws InvalidNameException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            cur = 0;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   413
            Vector<Rdn> rdns = new Vector<>(len / 3 + 10);  // leave room for growth
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                return rdns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            rdns.addElement(parseRdn());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            while (cur < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (chars[cur] == ',' || chars[cur] == ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    ++cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    rdns.insertElementAt(parseRdn(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    throw new InvalidNameException("Invalid name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return rdns;
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
         * Parses the DN, if it is known to contain a single RDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        Rdn getRdn() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            Rdn rdn = parseRdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (cur < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                throw new InvalidNameException("Invalid RDN: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * Parses the next RDN and returns it.  Throws an exception if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * none is found.  Leading and trailing whitespace is consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        private Rdn parseRdn() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Rdn rdn = new Rdn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            while (cur < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                consumeWhitespace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                String attrType = parseAttrType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                consumeWhitespace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                if (cur >= len || chars[cur] != '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    throw new InvalidNameException("Invalid name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                ++cur;          // consume '='
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                consumeWhitespace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                String value = parseAttrValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                consumeWhitespace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                rdn.add(new TypeAndValue(attrType, value, valuesCaseSensitive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (cur >= len || chars[cur] != '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                ++cur;          // consume '+'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return rdn;
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
         * Returns the attribute type that begins at the next unconsumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * char.  No leading whitespace is expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         * This routine is more generous than RFC 2253.  It accepts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * attribute types composed of any nonempty combination of Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * letters, Unicode digits, '.', '-', and internal space characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        private String parseAttrType() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            final int beg = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            while (cur < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                char c = chars[cur];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                if (Character.isLetterOrDigit(c) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                      c == '.' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                      c == '-' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                      c == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    ++cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // Back out any trailing spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            while ((cur > beg) && (chars[cur - 1] == ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                --cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            if (beg == cur) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                throw new InvalidNameException("Invalid name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return new String(chars, beg, cur - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * Returns the attribute value that begins at the next unconsumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * char.  No leading whitespace is expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        private String parseAttrValue() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (cur < len && chars[cur] == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return parseBinaryAttrValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            } else if (cur < len && chars[cur] == '"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                return parseQuotedAttrValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return parseStringAttrValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        private String parseBinaryAttrValue() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            final int beg = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            ++cur;                      // consume '#'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            while (cur < len &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                   Character.isLetterOrDigit(chars[cur])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                ++cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return new String(chars, beg, cur - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        private String parseQuotedAttrValue() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            final int beg = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            ++cur;                      // consume '"'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            while ((cur < len) && chars[cur] != '"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (chars[cur] == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    ++cur;              // consume backslash, then what follows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                ++cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (cur >= len) {   // no closing quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                throw new InvalidNameException("Invalid name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            ++cur       ;       // consume closing quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            return new String(chars, beg, cur - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        private String parseStringAttrValue() throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            final int beg = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            int esc = -1;       // index of the most recently escaped character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            while ((cur < len) && !atTerminator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                if (chars[cur] == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    ++cur;              // consume backslash, then what follows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    esc = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                ++cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (cur > len) {            // 'twas backslash followed by nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                throw new InvalidNameException("Invalid name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            // Trim off (unescaped) trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            int end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            for (end = cur; end > beg; end--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                if (!isWhitespace(chars[end - 1]) || (esc == end - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            return new String(chars, beg, end - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        private void consumeWhitespace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            while ((cur < len) && isWhitespace(chars[cur])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                ++cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * Returns true if next unconsumed character is one that terminates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * a string attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        private boolean atTerminator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return (cur < len &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    (chars[cur] == ',' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                     chars[cur] == ';' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                     chars[cur] == '+'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Class Rdn represents a set of TypeAndValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    static class Rdn {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * A vector of the TypeAndValue elements of this Rdn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * It is sorted to facilitate set operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   600
        private final Vector<TypeAndValue> tvs = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        void add(TypeAndValue tv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            // Set i to index of first element greater than tv, or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            // tvs.size() if there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            for (i = 0; i < tvs.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                int diff = tv.compareTo(tvs.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                if (diff == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    return;             // tv is a duplicate:  ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                } else if (diff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    break;
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
            tvs.insertElementAt(tv, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
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
            StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            for (int i = 0; i < tvs.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    buf.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                buf.append(tvs.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            return new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return ((obj instanceof Rdn) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    (compareTo(obj) == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // Compare TypeAndValue components one by one, lexicographically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            Rdn that = (Rdn)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            int minSize = Math.min(tvs.size(), that.tvs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            for (int i = 0; i < minSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                // Compare a single pair of type/value pairs.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   641
                TypeAndValue tv = tvs.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                int diff = tv.compareTo(that.tvs.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            return (tvs.size() - that.tvs.size());      // longer RDN wins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            // Sum up the hash codes of the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            // For each type/value pair...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            for (int i = 0; i < tvs.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                hash += tvs.elementAt(i).hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        Attributes toAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            Attributes attrs = new BasicAttributes(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            TypeAndValue tv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            for (int i = 0; i < tvs.size(); i++) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   667
                tv = tvs.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                if ((attr = attrs.get(tv.getType())) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    attrs.put(tv.getType(), tv.getUnescapedValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    attr.add(tv.getUnescapedValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Class TypeAndValue represents an attribute type and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * corresponding value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    static class TypeAndValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        private final String value;             // value, escaped or quoted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        private final boolean binary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        private final boolean valueCaseSensitive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        // If non-null, a canonical represention of the value suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        // for comparison using String.compareTo().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        private String comparable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        TypeAndValue(String type, String value, boolean valueCaseSensitive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            binary = value.startsWith("#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            this.valueCaseSensitive = valueCaseSensitive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return (type + "=" + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
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
            // NB: Any change here affecting equality must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            //     reflected in hashCode().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            TypeAndValue that = (TypeAndValue)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   711
            int diff = type.compareToIgnoreCase(that.type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (diff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (value.equals(that.value)) {     // try shortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            return getValueComparable().compareTo(that.getValueComparable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            // NB:  Any change here must be reflected in hashCode().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            if (!(obj instanceof TypeAndValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            TypeAndValue that = (TypeAndValue)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return (type.equalsIgnoreCase(that.type) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    (value.equals(that.value) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                     getValueComparable().equals(that.getValueComparable())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            // If two objects are equal, their hash codes must match.
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   734
            return (type.toUpperCase(Locale.ENGLISH).hashCode() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    getValueComparable().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         * Returns the type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         * Returns the unescaped value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        Object getUnescapedValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            return unescapeValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * Returns a canonical representation of "value" suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * comparison using String.compareTo().  If "value" is a string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * it is returned with escapes and quotes stripped away, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         * hex-encoded UTF-8 converted to 16-bit Unicode chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         * If value's case is to be ignored, it is returned in uppercase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * If "value" is binary, it is returned in uppercase but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * otherwise unmodified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        private String getValueComparable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (comparable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                return comparable;      // return cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // cache result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            if (binary) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   768
                comparable = value.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                comparable = (String)unescapeValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                if (!valueCaseSensitive) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   772
                    // ignore case
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   773
                    comparable = comparable.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return comparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * Given the value of an attribute, returns a string suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * for inclusion in a DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        static String escapeValue(Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return (val instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                ? escapeBinaryValue((byte[])val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                : escapeStringValue((String)val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * Given the value of a string-valued attribute, returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         * string suitable for inclusion in a DN.  This is accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
         * using backslash (\) to escape the following characters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         *      leading and trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         *      , = + < > # ; " \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        private static String escapeStringValue(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            final String escapees = ",=+<>#;\"\\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            char[] chars = val.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            StringBuffer buf = new StringBuffer(2 * val.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            // Find leading and trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            int lead;   // index of first char that is not leading whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            for (lead = 0; lead < chars.length; lead++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                if (!isWhitespace(chars[lead])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            int trail;  // index of last char that is not trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            for (trail = chars.length - 1; trail >= 0; trail--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                if (!isWhitespace(chars[trail])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            for (int i = 0; i < chars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                char c = chars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                if ((i < lead) || (i > trail) || (escapees.indexOf(c) >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    buf.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                buf.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * Given the value of a binary attribute, returns a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         * suitable for inclusion in a DN (such as "#CEB1DF80").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        private static String escapeBinaryValue(byte[] val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            StringBuffer buf = new StringBuffer(1 + 2 * val.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            buf.append("#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            for (int i = 0; i < val.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                byte b = val[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                buf.append(Character.forDigit(0xF & (b >>> 4), 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                buf.append(Character.forDigit(0xF & b, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   841
            return (new String(buf)).toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * Given an attribute value formated according to RFC 2253,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         * returns the unformated value.  Escapes and quotes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
         * stripped away, and hex-encoded UTF-8 is converted to 16-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * Unicode chars.  Returns a string value as a String, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         * binary value as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        static Object unescapeValue(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            char[] chars = val.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            int beg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            int end = chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            // Trim off leading and trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            while ((beg < end) && isWhitespace(chars[beg])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                ++beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            while ((beg < end) && isWhitespace(chars[end - 1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   865
            // Add back the trailing whitespace with a preceding '\'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            // (escaped or unescaped) that was taken off in the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            // loop. Whether or not to retain this whitespace is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            // decided below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            if (end != chars.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    (beg < end) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    chars[end - 1] == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if (beg >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            if (chars[beg] == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                // Value is binary (eg: "#CEB1DF80").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                return decodeHexPairs(chars, ++beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            // Trim off quotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            if ((chars[beg] == '\"') && (chars[end - 1] == '\"')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                ++beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            StringBuffer buf = new StringBuffer(end - beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            int esc = -1; // index of the last escaped character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                if ((chars[i] == '\\') && (i + 1 < end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    if (!Character.isLetterOrDigit(chars[i + 1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        ++i;                    // skip backslash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        buf.append(chars[i]);   // snarf escaped char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                        esc = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                        // Convert hex-encoded UTF-8 to 16-bit chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                        byte[] utf8 = getUtf8Octets(chars, i, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                        if (utf8.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                                buf.append(new String(utf8, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                            } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                                // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                            i += utf8.length * 3 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                                "Not a valid attribute string value:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                val +", improper usage of backslash");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    buf.append(chars[i]);       // snarf unescaped char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            // Get rid of the unescaped trailing whitespace with the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   921
            // preceding '\' character that was previously added back.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            int len = buf.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            if (isWhitespace(buf.charAt(len - 1)) && esc != (end - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                buf.setLength(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            return new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         * Given an array of chars (with starting and ending indexes into it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         * representing bytes encoded as hex-pairs (such as "CEB1DF80"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         * returns a byte array containing the decoded bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        private static byte[] decodeHexPairs(char[] chars, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            byte[] bytes = new byte[(end - beg) / 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            for (int i = 0; beg + 1 < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                int hi = Character.digit(chars[beg], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                int lo = Character.digit(chars[beg + 1], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                if (hi < 0 || lo < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                bytes[i] = (byte)((hi<<4) + lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                beg += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if (beg != end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        "Illegal attribute value: #" + new String(chars));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         * Given an array of chars (with starting and ending indexes into it),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * finds the largest prefix consisting of hex-encoded UTF-8 octets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         * and returns a byte array containing the corresponding UTF-8 octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
         * Hex-encoded UTF-8 octets look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
         *      \03\B1\DF\80
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        private static byte[] getUtf8Octets(char[] chars, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            byte[] utf8 = new byte[(end - beg) / 3];    // allow enough room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            int len = 0;        // index of first unused byte in utf8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            while ((beg + 2 < end) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                   (chars[beg++] == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                int hi = Character.digit(chars[beg++], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                int lo = Character.digit(chars[beg++], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                if (hi < 0 || lo < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                utf8[len++] = (byte)((hi<<4) + lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            if (len == utf8.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                return utf8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                byte[] res = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                System.arraycopy(utf8, 0, res, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * For testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (args.length == 1) {             // parse and print components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                LdapName n = new LdapName(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                Enumeration rdns = n.rdns.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                while (rdns.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    Rdn rdn = (Rdn)rdns.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    for (int i = 0; i < rdn.tvs.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                        System.out.print("[" + rdn.tvs.elementAt(i) + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            } else {                            // compare two names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                LdapName n1 = new LdapName(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                LdapName n2 = new LdapName(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                n1.unparsed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                n2.unparsed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                boolean eq = n1.equals(n2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                System.out.println("[" + n1 + (eq ? "] == [" : "] != [")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                                   + n2 + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
}