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