jdk/src/share/classes/sun/security/x509/RFC822Name.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 RFC822Name as required by the GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * ASN.1 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class RFC822Name implements GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Create the RFC822Name object from the passed encoded Der value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param derValue the encoded DER RFC822Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public RFC822Name(DerValue derValue) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        name = derValue.getIA5String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        parseName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
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 RFC822Name object with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param name the RFC822Name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @throws IOException on invalid input name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public RFC822Name(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        parseName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Parse an RFC822Name string to see if it is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * addr-spec according to IETF RFC822 and RFC2459:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * [local-part@]domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * local-part@ could be empty for an RFC822Name NameConstraint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * but the domain at least must be non-empty.  Case is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param name the RFC822Name string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @throws IOException if name is not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void parseName(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (name == null || name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new IOException("RFC822Name may not be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // See if domain is a valid domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String domain = name.substring(name.indexOf('@')+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (domain.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new IOException("RFC822Name may not end with @");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            //An RFC822 NameConstraint could start with a ., although
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            //a DNSName may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (domain.startsWith(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (domain.length() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    throw new IOException("RFC822Name domain may not be just .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Return the type of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return (GeneralNameInterface.NAME_RFC822);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Return the actual name value of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Encode the RFC822 name into the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param out the DER stream to encode the RFC822Name to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        out.putIA5String(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Convert the name into user readable string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return ("RFC822Name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Compares this name with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return true iff the names are equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * according to RFC2459.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (!(obj instanceof RFC822Name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        RFC822Name other = (RFC822Name)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // RFC2459 mandates that these names are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // not case-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return name.equalsIgnoreCase(other.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return name.toUpperCase().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Return constraint type:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *   <li>NAME_MATCH = 0: input name matches name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *   <li>NAME_NARROWS = 1: input name narrows name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *   <li>NAME_WIDENS = 2: input name widens name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * </ul>.  These results are used in checking NameConstraints during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * [RFC2459]    When the subjectAltName extension contains an Internet mail address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * the address MUST be included as an rfc822Name. The format of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * rfc822Name is an "addr-spec" as defined in RFC 822 [RFC 822]. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * addr-spec has the form "local-part@domain". Note that an addr-spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * has no phrase (such as a common name) before it, has no comment (text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * surrounded in parentheses) after it, and is not surrounded by "&lt;" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * "&gt;". Note that while upper and lower case letters are allowed in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * RFC 822 addr-spec, no significance is attached to the case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param inputName to be checked for being constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @returns constraint type above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *          not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (inputName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        else if (inputName.getType() != (GeneralNameInterface.NAME_RFC822)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            //RFC2459 specifies that case is not significant in RFC822Names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            String inName = (((RFC822Name)inputName).getName()).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            String thisName = name.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (inName.equals(thisName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else if (thisName.endsWith(inName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                /* if both names contain @, then they had to match exactly */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (inName.indexOf('@') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                } else if (inName.startsWith(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    int inNdx = thisName.lastIndexOf(inName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    if (thisName.charAt(inNdx-1) == '@' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } else if (inName.endsWith(thisName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                /* if thisName contains @, then they had to match exactly */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (thisName.indexOf('@') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } else if (thisName.startsWith(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    int ndx = inName.lastIndexOf(thisName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    if (inName.charAt(ndx-1) == '@') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * NameConstraints minimum and maximum bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @returns distance of name from root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        String subtree=name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int i=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        /* strip off name@ portion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int atNdx = subtree.lastIndexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (atNdx >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            subtree=subtree.substring(atNdx+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /* count dots in dnsname, adding one if dnsname preceded by @ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (; subtree.lastIndexOf('.') >= 0; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            subtree=subtree.substring(0,subtree.lastIndexOf('.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}