jdk/src/share/classes/sun/security/x509/DNSName.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2000 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class implements the DNSName as required by the GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * ASN.1 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * [RFC2459] When the subjectAltName extension contains a domain name service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * label, the domain name MUST be stored in the dNSName (an IA5String).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The name MUST be in the "preferred name syntax," as specified by RFC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * 1034 [RFC 1034]. Note that while upper and lower case letters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * allowed in domain names, no signifigance is attached to the case.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * addition, while the string " " is a legal domain name, subjectAltName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * extensions with a dNSName " " are not permitted.  Finally, the use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the DNS representation for Internet mail addresses (wpolk.nist.gov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * instead of wpolk@nist.gov) is not permitted; such identities are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * be encoded as rfc822Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class DNSName implements GeneralNameInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final String digitsAndHyphen = "0123456789-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final String alphaDigitsAndHyphen = alpha + digitsAndHyphen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Create the DNSName object from the passed encoded Der value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param derValue the encoded DER DNSName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public DNSName(DerValue derValue) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        name = derValue.getIA5String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Create the DNSName object with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param name the DNSName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @throws IOException if the name is not a valid DNSName subjectAltName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public DNSName(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (name == null || name.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new IOException("DNS name must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (name.indexOf(' ') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new IOException("DNS names or NameConstraints with blank components are not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (name.charAt(0) == '.' || name.charAt(name.length() -1) == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new IOException("DNS names or NameConstraints may not begin or end with a .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        //Name will consist of label components separated by "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        //startIndex is the index of the first character of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        //endIndex is the index of the last character of a component plus 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int endIndex,startIndex=0; startIndex < name.length(); startIndex = endIndex+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            endIndex = name.indexOf('.', startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (endIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                endIndex = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if ((endIndex-startIndex) < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                throw new IOException("DNSName SubjectAltNames with empty components are not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            //DNSName components must begin with a letter A-Z or a-z
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (alpha.indexOf(name.charAt(startIndex)) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                throw new IOException("DNSName components must begin with a letter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            //nonStartIndex: index for characters in the component beyond the first one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            for (int nonStartIndex=startIndex+1; nonStartIndex < endIndex; nonStartIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                char x = name.charAt(nonStartIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if ((alphaDigitsAndHyphen).indexOf(x) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    throw new IOException("DNSName components must consist of letters, digits, and hyphens");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Return the type of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return (GeneralNameInterface.NAME_DNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Return the actual name value of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return name;
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
     * Encode the DNS name into the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param out the DER stream to encode the DNSName to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        out.putIA5String(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Convert the name into user readable string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return ("DNSName: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Compares this name with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return true iff the names are equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * according to RFC2459.
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
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (!(obj instanceof DNSName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        DNSName other = (DNSName)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // RFC2459 mandates that these names are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // not case-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return name.equalsIgnoreCase(other.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return name.toUpperCase().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Return type of constraint inputName places on this name:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *   <li>NAME_MATCH = 0: input name matches name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *   <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *   <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * </ul>.  These results are used in checking NameConstraints during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * RFC2459: DNS name restrictions are expressed as foo.bar.com. Any subdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * satisfies the name constraint. For example, www.foo.bar.com would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * satisfy the constraint but bigfoo.bar.com would not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * draft-ietf-pkix-new-part1-00.txt:  DNS name restrictions are expressed as foo.bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Any DNS name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * can be constructed by simply adding to the left hand side of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * satisfies the name constraint. For example, www.foo.bar.com would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * satisfy the constraint but foo1.bar.com would not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * RFC1034: By convention, domain names can be stored with arbitrary case, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * domain name comparisons for all present domain functions are done in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * case-insensitive manner, assuming an ASCII character set, and a high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * order zero bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param inputName to be checked for being constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @returns constraint type above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *          not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (inputName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        else if (inputName.getType() != NAME_DNS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            String inName = (((DNSName)inputName).getName()).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            String thisName = name.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (inName.equals(thisName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            else if (thisName.endsWith(inName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                int inNdx = thisName.lastIndexOf(inName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (thisName.charAt(inNdx-1) == '.' )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } else if (inName.endsWith(thisName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                int ndx = inName.lastIndexOf(thisName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (inName.charAt(ndx-1) == '.' )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * NameConstraints minimum and maximum bounds and for calculating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * path lengths in name subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @returns distance of name from root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        String subtree=name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int i=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        /* count dots */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (; subtree.lastIndexOf('.') >= 0; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            subtree=subtree.substring(0,subtree.lastIndexOf('.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
}