001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Alexei Y. Zakharov
021: * @version $Revision: 1.1.2.4 $
022: */package org.apache.harmony.jndi.provider.dns;
023:
024: import java.util.StringTokenizer;
025: import javax.naming.InvalidNameException;
026: import javax.naming.Name;
027: import javax.naming.NameParser;
028:
029: import org.apache.harmony.jndi.internal.nls.Messages;
030:
031: /**
032: * DNS name parser
033: */
034: public class DNSNameParser implements NameParser {
035:
036: /**
037: * Constructs a new name parser.
038: */
039: public DNSNameParser() {
040: }
041:
042: /**
043: * Parses string representation of DNS name. Following situations will be
044: * treated as an error:
045: * <ul>
046: * <li>The length of the whole name is longer than 255 characters</li>
047: * <li>The length of each label is more than 63 characters</li>
048: * <li>more than one null label encountered or null label is not the least
049: * specific label (the rightmost)</li>
050: * </ul>
051: *
052: * @param name
053: * string representation of DNS name
054: * @return new instance of <code>DNSName</code> class
055: * @throws InvalidNameException
056: * if given string is not a correct DNS name
057: * @see javax.naming.NameParser#parse(java.lang.String)
058: * @see RFC 1034
059: */
060: public Name parse(String name) throws InvalidNameException {
061: StringTokenizer st;
062: boolean lastTokenWasDilim = false;
063: DNSName dnsName = new DNSName();
064:
065: if (name == null) {
066: // jndi.2E=The name is null
067: throw new InvalidNameException(Messages
068: .getString("jndi.2E")); //$NON-NLS-1$
069: }
070: if (name.length() > 255) {
071: // jndi.54=The length of the name is more than 255 characters
072: throw new InvalidNameException(Messages
073: .getString("jndi.54")); //$NON-NLS-1$
074: }
075: st = new StringTokenizer(name, ".", true); //$NON-NLS-1$
076: while (st.hasMoreTokens()) {
077: String comp = st.nextToken();
078:
079: if (comp.equals(".")) { //$NON-NLS-1$
080: if (lastTokenWasDilim) {
081: // two delimiters one after another
082: // jndi.55=Null label is not the rightmost one
083: throw new InvalidNameException(Messages
084: .getString("jndi.55")); //$NON-NLS-1$
085: }
086: lastTokenWasDilim = true;
087: if (dnsName.size() == 0 && st.hasMoreTokens()) {
088: // jndi.56=DNS name shouldn't start with a dot
089: throw new InvalidNameException(Messages
090: .getString("jndi.56")); //$NON-NLS-1$
091: }
092: } else {
093: if (comp.length() > 63) {
094: // jndi.57=The length of {0} label is more than 63
095: // characters
096: throw new InvalidNameException(Messages.getString(
097: "jndi.57", comp)); //$NON-NLS-1$
098: }
099: dnsName.add(0, comp);
100: lastTokenWasDilim = false;
101: }
102: }
103: if (lastTokenWasDilim) {
104: dnsName.add(0, ""); //$NON-NLS-1$
105: }
106: return dnsName;
107: }
108:
109: /**
110: * @param obj
111: * the object to compare with
112: * @return <code>true</code> if and only if the given object is instance
113: * of class <code>DNSParser</code>; otherwise returns
114: * <code>false</code>.
115: */
116: @Override
117: public boolean equals(Object obj) {
118: if (obj != null && obj instanceof DNSNameParser) {
119: return true;
120: }
121: return false;
122: }
123:
124: }
|