com.internationalnetwork.util
Class Hostname

Object
  extended by com.internationalnetwork.util.Hostname

public class Hostname
extends Object

This class can be used to validate a hostname, and also to determine some of its properties, such as the number of levels, RFC1034 compliance, etc.

Although RFC1034 defines the following rules, special methods are provided for slightly loose validity checking since many Registrars have been permitting their customers to register IDNs (Internet Domain Names) that actually violate these specifications:

Outright violations, such as two consecutive periods, or other invalid characters (not letters, digits, nor a hyphen), will result in a ParseException being thrown. For strict compliance, use the isStandard() method in addition to catching the ParseException exception.

Strangly, the rules also specify that " " (a single space) is a valid domain, even though it doesn't seem to be documented anywhere in RFC1034. Due to this lack of documentation, and the fact that nobody that we are aware of is using these characters in domain names, a space or a quotation mark will be treated the same as other obviously invalid characters.


Field Summary
static String VERSION
          Version number of this Package (read-only).
 
Constructor Summary
Hostname()
          Constructs an empty Hostname object.
Hostname(String hostname)
          Constructs a Hostname object containing the specified hostname.
 
Method Summary
 String getLevel(int level)
          A period marks the boundary between hierarchy levels in hostnames, with the first (or top) level located at the right-hand side.
 int getLevelCount()
          A period marks the boundary between hierarchy levels in hostnames, with the first (or top) level located at the right-hand side.
 boolean hasLeadingDigit()
          Hostnames that include one level that begins with a digit are not compliant with the rules defined in RFC1034.
 boolean hasLeadingDigits()
          Hostnames that include two or more levels that begin with a digit are not compliant with the rules defined in RFC1034.
 boolean hasLeadingHyphen()
          Hostnames that include one level that begins with a hyphen are not compliant with the rules defined in RFC1034.
 boolean hasLeadingHyphens()
          Hostnames that include two or more levels that begin with a hyphen are not compliant with the rules defined in RFC1034.
 boolean hasLongLabel()
          Hostnames that include one level that is too long are not compliant with the rules defined in RFC1034.
 boolean hasLongLabels()
          Hostnames that include two or more levels that are too long are not compliant with the rules defined in RFC1034.
 boolean hasTrailingHyphen()
          Hostnames that include one level that ends with a hyphen are not compliant with the rules defined in RFC1034.
 boolean hasTrailingHyphens()
          Hostnames that include two or more levels that end with a hyphen are not compliant with the rules defined in RFC1034.
 boolean isStandard()
          Certain forms of hostnames that lack compliance with RFC1034 are commonly used on the internet because most systems accept a loose interpretation of the hostname defintions rules.
static void main(String... args)
          For testing purposes only
 String toString()
          Returns the hostname, re-constructed from its originally split levels.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Version number of this Package (read-only).

See Also:
Constant Field Values
Constructor Detail

Hostname

public Hostname()
Constructs an empty Hostname object. Although this Hostname object is technically invalid, the validity will change as soon as it is populated with an Internet Domain Name by way of various set methods.


Hostname

public Hostname(String hostname)
         throws java.text.ParseException
Constructs a Hostname object containing the specified hostname.

Parameters:
hostname - RFC-compliant hostname (if a null or a blank String is specified, a new object is created but with an error condition set)
Throws:
java.text.ParseException - When a non-compliant hostname is encountered that is definitely invalid. If no exception is thrown, you may still wish to use the isStandard() method to determine if the name strictly adheres to the standard (an invalid name that doesn't throw this exception may actually work due to how common these violations of RFC1034 are).
Method Detail

getLevel

public String getLevel(int level)
A period marks the boundary between hierarchy levels in hostnames, with the first (or top) level located at the right-hand side. This method returns a specific level by its number.

The first (right-most) level begins at 1 (not 0), so for convenience 0 will return the entire hostname instead of generating an error.

If a negative value is specified, then it will be treated as an absolute value and with the levels reversed, hence a level of -1 will return the last (left-most) level.

Use the getLevelCount() method to determine the maximum number of levels.

Parameters:
level - The level number to return (or entire hostname if 0)
Returns:
The String label for the respective level (or the entire hostname if level 0 was specified)

getLevelCount

public int getLevelCount()
A period marks the boundary between hierarchy levels in hostnames, with the first (or top) level located at the right-hand side. This method returns the number of levels in the current hostname.

Returns:
Number of levels

hasLeadingDigit

public boolean hasLeadingDigit()
Hostnames that include one level that begins with a digit are not compliant with the rules defined in RFC1034.

Returns:
True if only one level begins with a digit

hasLeadingDigits

public boolean hasLeadingDigits()
Hostnames that include two or more levels that begin with a digit are not compliant with the rules defined in RFC1034.

Returns:
True if one or more levels begin with a digit

hasLeadingHyphen

public boolean hasLeadingHyphen()
Hostnames that include one level that begins with a hyphen are not compliant with the rules defined in RFC1034.

Returns:
True if only one level begins with a hyphen

hasLeadingHyphens

public boolean hasLeadingHyphens()
Hostnames that include two or more levels that begin with a hyphen are not compliant with the rules defined in RFC1034.

Returns:
True if one or more levels begin with a hyphen

hasLongLabel

public boolean hasLongLabel()
Hostnames that include one level that is too long are not compliant with the rules defined in RFC1034.

Returns:
True if only one label is too long

hasLongLabels

public boolean hasLongLabels()
Hostnames that include two or more levels that are too long are not compliant with the rules defined in RFC1034.

Returns:
True if one or more levels are too long

hasTrailingHyphen

public boolean hasTrailingHyphen()
Hostnames that include one level that ends with a hyphen are not compliant with the rules defined in RFC1034.

Returns:
True if only one level ends with a hyphen

hasTrailingHyphens

public boolean hasTrailingHyphens()
Hostnames that include two or more levels that end with a hyphen are not compliant with the rules defined in RFC1034.

Returns:
True if one or more levels end with a hyphen

isStandard

public boolean isStandard()
Certain forms of hostnames that lack compliance with RFC1034 are commonly used on the internet because most systems accept a loose interpretation of the hostname defintions rules. For most purposes, reliance on this method for validity checking is not recommended because it is impractical.

Use this method only to determine strict compliance with the rules defined in RFC1034. Rely on the constructor to throw the ParseException exception to catch hostnames that are invalid.

Returns:
True only if hostname strictly complies with RFC1034

main

public static void main(String... args)
For testing purposes only


toString

public String toString()
Returns the hostname, re-constructed from its originally split levels.

Overrides:
toString in class Object
Returns:
Hostname