jdk/src/share/classes/javax/naming/NameImpl.java
author igerasim
Mon, 14 Apr 2014 16:15:10 +0400
changeset 23882 7dbf42ed83ef
parent 10369 e9d2e59e53f0
child 24969 afa6934dd8e8
permissions -rw-r--r--
8009637: Some error messages are missing a space Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.naming;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
    28
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * The implementation class for CompoundName and CompositeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  * This class is package private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * @author Aravindan Ranganathan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class NameImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final byte LEFT_TO_RIGHT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final byte RIGHT_TO_LEFT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final byte FLAT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    49
    private Vector<String> components;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte syntaxDirection = LEFT_TO_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String syntaxSeparator = "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private String syntaxSeparator2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean syntaxCaseInsensitive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean syntaxTrimBlanks = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private String syntaxEscape = "\\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private String syntaxBeginQuote1 = "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private String syntaxEndQuote1 = "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private String syntaxBeginQuote2 = "'";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private String syntaxEndQuote2 = "'";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private String syntaxAvaSeparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private String syntaxTypevalSeparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // escapingStyle gives the method used at creation time for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // quoting or escaping characters in the name.  It is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // first style of quote or escape encountered if and when the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // is parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final int STYLE_NONE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int STYLE_QUOTE1 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int STYLE_QUOTE2 = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int STYLE_ESCAPE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int escapingStyle = STYLE_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Returns true if "match" is not null, and n contains "match" at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // position i.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final boolean isA(String n, int i, String match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return (match != null && n.startsWith(match, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final boolean isMeta(String n, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return (isA(n, i, syntaxEscape) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                isA(n, i, syntaxBeginQuote1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                isA(n, i, syntaxBeginQuote2) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                isSeparator(n, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private final boolean isSeparator(String n, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return (isA(n, i, syntaxSeparator) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                isA(n, i, syntaxSeparator2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private final int skipSeparator(String name, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (isA(name, i, syntaxSeparator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            i += syntaxSeparator.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else if (isA(name, i, syntaxSeparator2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            i += syntaxSeparator2.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return (i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   101
    private final int extractComp(String name, int i, int len, Vector<String> comps)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String beginQuote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        String endQuote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        boolean start = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        boolean one = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        StringBuffer answer = new StringBuffer(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        while (i < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // handle quoted strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (start && ((one = isA(name, i, syntaxBeginQuote1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                          isA(name, i, syntaxBeginQuote2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                // record choice of quote chars being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                beginQuote = one ? syntaxBeginQuote1 : syntaxBeginQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                endQuote = one ? syntaxEndQuote1 : syntaxEndQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (escapingStyle == STYLE_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    escapingStyle = one ? STYLE_QUOTE1 : STYLE_QUOTE2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // consume string until matching quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                for (i += beginQuote.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                     ((i < len) && !name.startsWith(endQuote, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                     i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    // skip escape character if it is escaping ending quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    // otherwise leave as is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    if (isA(name, i, syntaxEscape) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        isA(name, i + syntaxEscape.length(), endQuote)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        i += syntaxEscape.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    answer.append(name.charAt(i));  // copy char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // no ending quote found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (i >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        new InvalidNameException(name + ": no close quote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
//                      new Exception("no close quote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                i += endQuote.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // verify that end-quote occurs at separator or end of string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (i == len || isSeparator(name, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
//              throw (new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw (new InvalidNameException(name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    ": close quote appears before end of component"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else if (isSeparator(name, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } else if (isA(name, i, syntaxEscape)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (isMeta(name, i + syntaxEscape.length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    // if escape precedes meta, consume escape and let
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // meta through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    i += syntaxEscape.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (escapingStyle == STYLE_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        escapingStyle = STYLE_ESCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                } else if (i + syntaxEscape.length() >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    throw (new InvalidNameException(name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        ": unescaped " + syntaxEscape + " at end of component"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } else if (isA(name, i, syntaxTypevalSeparator) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ((one = isA(name, i+syntaxTypevalSeparator.length(), syntaxBeginQuote1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            isA(name, i+syntaxTypevalSeparator.length(), syntaxBeginQuote2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // Handle quote occurring after typeval separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                beginQuote = one ? syntaxBeginQuote1 : syntaxBeginQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                endQuote = one ? syntaxEndQuote1 : syntaxEndQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                i += syntaxTypevalSeparator.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                answer.append(syntaxTypevalSeparator+beginQuote); // add back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                // consume string until matching quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                for (i += beginQuote.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                     ((i < len) && !name.startsWith(endQuote, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                     i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    // skip escape character if it is escaping ending quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    // otherwise leave as is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    if (isA(name, i, syntaxEscape) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        isA(name, i + syntaxEscape.length(), endQuote)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        i += syntaxEscape.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    answer.append(name.charAt(i));  // copy char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                // no ending quote found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (i >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        new InvalidNameException(name + ": typeval no close quote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                i += endQuote.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                answer.append(endQuote); // add back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // verify that end-quote occurs at separator or end of string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (i == len || isSeparator(name, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                throw (new InvalidNameException(name.substring(i) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    ": typeval close quote appears before end of component"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            answer.append(name.charAt(i++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            start = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (syntaxDirection == RIGHT_TO_LEFT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            comps.insertElementAt(answer.toString(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            comps.addElement(answer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private static boolean getBoolean(Properties p, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return toBoolean(p.getProperty(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static boolean toBoolean(String name) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   220
        return ((name != null) &&
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   221
            name.toLowerCase(Locale.ENGLISH).equals("true"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private final void recordNamingConvention(Properties p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        String syntaxDirectionStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            p.getProperty("jndi.syntax.direction", "flat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (syntaxDirectionStr.equals("left_to_right")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            syntaxDirection = LEFT_TO_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else if (syntaxDirectionStr.equals("right_to_left")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            syntaxDirection = RIGHT_TO_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else if (syntaxDirectionStr.equals("flat")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            syntaxDirection = FLAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new IllegalArgumentException(syntaxDirectionStr +
23882
7dbf42ed83ef 8009637: Some error messages are missing a space
igerasim
parents: 10369
diff changeset
   235
                " is not a valid value for the jndi.syntax.direction property");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (syntaxDirection != FLAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            syntaxSeparator = p.getProperty("jndi.syntax.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            syntaxSeparator2 = p.getProperty("jndi.syntax.separator2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (syntaxSeparator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    "jndi.syntax.separator property required for non-flat syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            syntaxSeparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        syntaxEscape = p.getProperty("jndi.syntax.escape");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        syntaxCaseInsensitive = getBoolean(p, "jndi.syntax.ignorecase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        syntaxTrimBlanks = getBoolean(p, "jndi.syntax.trimblanks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        syntaxBeginQuote1 = p.getProperty("jndi.syntax.beginquote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        syntaxEndQuote1 = p.getProperty("jndi.syntax.endquote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (syntaxEndQuote1 == null && syntaxBeginQuote1 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            syntaxEndQuote1 = syntaxBeginQuote1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        else if (syntaxBeginQuote1 == null && syntaxEndQuote1 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            syntaxBeginQuote1 = syntaxEndQuote1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        syntaxBeginQuote2 = p.getProperty("jndi.syntax.beginquote2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        syntaxEndQuote2 = p.getProperty("jndi.syntax.endquote2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (syntaxEndQuote2 == null && syntaxBeginQuote2 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            syntaxEndQuote2 = syntaxBeginQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        else if (syntaxBeginQuote2 == null && syntaxEndQuote2 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            syntaxBeginQuote2 = syntaxEndQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        syntaxAvaSeparator = p.getProperty("jndi.syntax.separator.ava");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        syntaxTypevalSeparator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            p.getProperty("jndi.syntax.separator.typeval");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    NameImpl(Properties syntax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (syntax != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            recordNamingConvention(syntax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   275
        components = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    NameImpl(Properties syntax, String n) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        this(syntax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        boolean rToL = (syntaxDirection == RIGHT_TO_LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        boolean compsAllEmpty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int len = n.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        for (int i = 0; i < len; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            i = extractComp(n, i, len, components);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            String comp = rToL
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   289
                ? components.firstElement()
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   290
                : components.lastElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (comp.length() >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                compsAllEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (i < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                i = skipSeparator(n, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if ((i == len) && !compsAllEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    // Trailing separator found.  Add an empty component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    if (rToL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        components.insertElementAt("", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        components.addElement("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   309
    NameImpl(Properties syntax, Enumeration<String> comps) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        this(syntax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // %% comps could shrink in the middle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        while (comps.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            components.addElement(comps.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    // Determines whether this component needs any escaping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private final boolean escapingNeeded(String comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int len = comp.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                if (isA(comp, 0, syntaxBeginQuote1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    isA(comp, 0, syntaxBeginQuote2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (isSeparator(comp, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (isA(comp, i, syntaxEscape)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                i += syntaxEscape.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (i >= len || isMeta(comp, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private final String stringifyComp(String comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int len = comp.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        boolean escapeSeparator = false, escapeSeparator2 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        String beginQuote = null, endQuote = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        StringBuffer strbuf = new StringBuffer(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // determine whether there are any separators; if so escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // or quote them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (syntaxSeparator != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            comp.indexOf(syntaxSeparator) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (syntaxBeginQuote1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                beginQuote = syntaxBeginQuote1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                endQuote = syntaxEndQuote1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            } else if (syntaxBeginQuote2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                beginQuote = syntaxBeginQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                endQuote = syntaxEndQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } else if (syntaxEscape != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                escapeSeparator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (syntaxSeparator2 != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            comp.indexOf(syntaxSeparator2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (syntaxBeginQuote1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (beginQuote == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    beginQuote = syntaxBeginQuote1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    endQuote = syntaxEndQuote1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            } else if (syntaxBeginQuote2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (beginQuote == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    beginQuote = syntaxBeginQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    endQuote = syntaxEndQuote2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            } else if (syntaxEscape != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                escapeSeparator2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // if quoting component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (beginQuote != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // start string off with opening quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            strbuf = strbuf.append(beginQuote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            // component is being quoted, so we only need to worry about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // escaping end quotes that occur in component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            for (int i = 0; i < len; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                if (comp.startsWith(endQuote, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    // end-quotes must be escaped when inside a quoted string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    strbuf.append(syntaxEscape).append(endQuote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    i += endQuote.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    // no special treatment required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    strbuf.append(comp.charAt(i++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            // end with closing quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            strbuf.append(endQuote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            // When component is not quoted, add escape for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            // 1. leading quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // 2. an escape preceding any meta char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            // 3. an escape at the end of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            // 4. separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            // go through characters in component and escape where necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            boolean start = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            for (int i = 0; i < len; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // leading quote must be escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                if (start && isA(comp, i, syntaxBeginQuote1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    strbuf.append(syntaxEscape).append(syntaxBeginQuote1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    i += syntaxBeginQuote1.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                } else if (start && isA(comp, i, syntaxBeginQuote2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    strbuf.append(syntaxEscape).append(syntaxBeginQuote2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    i += syntaxBeginQuote2.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                // Escape an escape preceding meta characters, or at end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                // Other escapes pass through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (isA(comp, i, syntaxEscape)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (i + syntaxEscape.length() >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        // escape an ending escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        strbuf.append(syntaxEscape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    } else if (isMeta(comp, i + syntaxEscape.length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        // escape meta strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        strbuf.append(syntaxEscape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    strbuf.append(syntaxEscape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    i += syntaxEscape.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                // escape unescaped separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                if (escapeSeparator && comp.startsWith(syntaxSeparator, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    // escape separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    strbuf.append(syntaxEscape).append(syntaxSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    i += syntaxSeparator.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                } else if (escapeSeparator2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                           comp.startsWith(syntaxSeparator2, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    // escape separator2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    strbuf.append(syntaxEscape).append(syntaxSeparator2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    i += syntaxSeparator2.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    // no special treatment required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    strbuf.append(comp.charAt(i++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                start = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return (strbuf.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        StringBuffer answer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        String comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        boolean compsAllEmpty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int size = components.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (syntaxDirection == RIGHT_TO_LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                comp =
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   460
                    stringifyComp(components.elementAt(size - 1 - i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            } else {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   462
                comp = stringifyComp(components.elementAt(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if ((i != 0) && (syntaxSeparator != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                answer.append(syntaxSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (comp.length() >= 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                compsAllEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            answer = answer.append(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (compsAllEmpty && (size >= 1) && (syntaxSeparator != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            answer = answer.append(syntaxSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return (answer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if ((obj != null) && (obj instanceof NameImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            NameImpl target = (NameImpl)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (target.size() ==  this.size()) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   479
                Enumeration<String> mycomps = getAll();
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   480
                Enumeration<String> comps = target.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                while (mycomps.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    // %% comps could shrink in the middle.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   483
                    String my = mycomps.nextElement();
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   484
                    String his = comps.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    if (syntaxTrimBlanks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        my = my.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        his = his.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    if (syntaxCaseInsensitive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        if (!(my.equalsIgnoreCase(his)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        if (!(my.equals(his)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
      * Compares obj to this NameImpl to determine ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
      * Takes into account syntactic properties such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
      * elimination of blanks, case-ignore, etc, if relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
      * Note: using syntax of this NameImpl and ignoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
      * that of comparison target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public int compareTo(NameImpl obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        int len1 = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        int len2 = obj.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        int n = Math.min(len1, len2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int index1 = 0, index2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        while (n-- != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            String comp1 = get(index1++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            String comp2 = obj.get(index2++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            // normalize according to syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (syntaxTrimBlanks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                comp1 = comp1.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                comp2 = comp2.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   531
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   532
            int local;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (syntaxCaseInsensitive) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   534
                local = comp1.compareToIgnoreCase(comp2);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   535
            } else {
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   536
                local = comp1.compareTo(comp2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   538
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (local != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                return local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        return len1 - len2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return (components.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   551
    public Enumeration<String> getAll() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return components.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public String get(int posn) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   556
        return components.elementAt(posn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   559
    public Enumeration<String> getPrefix(int posn) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (posn < 0 || posn > size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            throw new ArrayIndexOutOfBoundsException(posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return new NameImplEnumerator(components, 0, posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   566
    public Enumeration<String> getSuffix(int posn) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        int cnt = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (posn < 0 || posn > cnt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            throw new ArrayIndexOutOfBoundsException(posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return new NameImplEnumerator(components, posn, cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return (components.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   578
    public boolean startsWith(int posn, Enumeration<String> prefix) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (posn < 0 || posn > size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   583
            Enumeration<String> mycomps = getPrefix(posn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            while (mycomps.hasMoreElements()) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   585
                String my = mycomps.nextElement();
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   586
                String his = prefix.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                if (syntaxTrimBlanks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    my = my.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    his = his.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                if (syntaxCaseInsensitive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    if (!(my.equalsIgnoreCase(his)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    if (!(my.equals(his)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   605
    public boolean endsWith(int posn, Enumeration<String> suffix) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        // posn is number of elements in suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // startIndex is the starting position in this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // at which to start the comparison. It is calculated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // subtracting 'posn' from size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int startIndex = size() - posn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (startIndex < 0 || startIndex > size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   615
            Enumeration<String> mycomps = getSuffix(startIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            while (mycomps.hasMoreElements()) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   617
                String my = mycomps.nextElement();
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   618
                String his = suffix.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                if (syntaxTrimBlanks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    my = my.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    his = his.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if (syntaxCaseInsensitive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    if (!(my.equalsIgnoreCase(his)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    if (!(my.equals(his)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   637
    public boolean addAll(Enumeration<String> comps) throws InvalidNameException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        boolean added = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        while (comps.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   641
                String comp = comps.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                if (size() > 0 && syntaxDirection == FLAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        "A flat name can only have a single component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                components.addElement(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                added = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                break;  // "comps" has shrunk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   655
    public boolean addAll(int posn, Enumeration<String> comps)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        boolean added = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        for (int i = posn; comps.hasMoreElements(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   660
                String comp = comps.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                if (size() > 0 && syntaxDirection == FLAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        "A flat name can only have a single component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                components.insertElementAt(comp, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                added = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                break;  // "comps" has shrunk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        return added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public void add(String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (size() > 0 && syntaxDirection == FLAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                "A flat name can only have a single component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        components.addElement(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public void add(int posn, String comp) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        if (size() > 0 && syntaxDirection == FLAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                "A flat name can only zero or one component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        components.insertElementAt(comp, posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public Object remove(int posn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        Object r = components.elementAt(posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        components.removeElementAt(posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        int hash = 0;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   698
        for (Enumeration<String> e = getAll(); e.hasMoreElements();) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   699
            String comp = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (syntaxTrimBlanks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                comp = comp.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (syntaxCaseInsensitive) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   704
                comp = comp.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            hash += comp.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
final
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   714
class NameImplEnumerator implements Enumeration<String> {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   715
    Vector<String> vector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    int limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   719
    NameImplEnumerator(Vector<String> v, int start, int lim) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        vector = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        count = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        limit = lim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return count < limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   729
    public String nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (count < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return vector.elementAt(count++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        throw new NoSuchElementException("NameImplEnumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
}