Class DomainValidator
- java.lang.Object
-
- org.openstreetmap.josm.data.validation.routines.AbstractValidator
-
- org.openstreetmap.josm.data.validation.routines.DomainValidator
-
public final class DomainValidator extends AbstractValidator
Domain name validation routines.
This validator provides methods for validating Internet domain names and top-level domains.
Domain names are evaluated according to the standards RFC1034, section 3, and RFC1123, section 2.1. No accommodation is provided for the specialized needs of other applications; if the domain name has been URL-encoded, for example, validation will fail even though the equivalent plaintext version of the same name would have passed.
Validation is also provided for top-level domains (TLDs) as defined and maintained by the Internet Assigned Numbers Authority (IANA):
isValidInfrastructureTld(java.lang.String)- validates infrastructure TLDs (.arpa, etc.)isValidGenericTld(java.lang.String)- validates generic TLDs (.com, .org, etc.)isValidCountryCodeTld(java.lang.String)- validates country code TLDs (.us, .uk, .cn, etc.)
(NOTE: This class does not provide IP address lookup for domain names or methods to ensure that a given domain name matches a specific IP; see
InetAddressfor that functionality.)- Since:
- Validator 1.4
- Version:
- $Revision: 1740822 $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDomainValidator.ArrayTypeenum used byupdateTLDOverride(ArrayType, String[])to determine which override array to update / fetchprivate static classDomainValidator.IdnBugHolder
-
Field Summary
Fields Modifier and Type Field Description private booleanallowLocalprivate static java.lang.String[]COUNTRY_CODE_TLDSprivate static java.lang.String[]countryCodeTLDsMinusprivate static java.lang.String[]countryCodeTLDsPlusprivate static java.lang.StringDOMAIN_LABEL_REGEXprivate static java.lang.StringDOMAIN_NAME_REGEXprivate static DomainValidatorDOMAIN_VALIDATORSingleton instance of this validator, which doesn't consider local addresses as valid.private static DomainValidatorDOMAIN_VALIDATOR_WITH_LOCALSingleton instance of this validator, which does consider local addresses valid.private RegexValidatordomainRegexRegexValidator for matching domains.private static java.lang.String[]EMPTY_STRING_ARRAYprivate static java.lang.String[]GENERIC_TLDSprivate static java.lang.String[]genericTLDsMinusprivate static java.lang.String[]genericTLDsPlusprivate RegexValidatorhostnameRegexRegexValidator for matching a local hostnameprivate static java.lang.String[]INFRASTRUCTURE_TLDSprivate static booleaninUseprivate static java.lang.String[]LOCAL_TLDSprivate static intMAX_DOMAIN_LENGTHprivate static java.lang.StringTOP_LABEL_REGEX
-
Constructor Summary
Constructors Modifier Constructor Description privateDomainValidator(boolean allowLocal)Private constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static booleanarrayContains(java.lang.String[] sortedArray, java.lang.String key)Check if a sorted array contains the specified keyprivate static java.lang.StringchompLeadingDot(java.lang.String str)(package private) static voidclearTLDOverrides()static DomainValidatorgetInstance()Returns the singleton instance of this validator.static DomainValidatorgetInstance(boolean allowLocal)Returns the singleton instance of this validator, with local validation as required.static java.lang.String[]getTLDEntries(DomainValidator.ArrayType table)Get a copy of the internal array.java.lang.StringgetValidatorName()Returns the name of this validatorprivate static booleanisOnlyASCII(java.lang.String input)booleanisValid(java.lang.String domain)Returns true if the specifiedStringparses as a valid domain name with a recognized top-level domain.booleanisValidCountryCodeTld(java.lang.String ccTld)Returns true if the specifiedStringmatches any IANA-defined country code top-level domain.(package private) booleanisValidDomainSyntax(java.lang.String domain)booleanisValidGenericTld(java.lang.String gTld)Returns true if the specifiedStringmatches any IANA-defined generic top-level domain.booleanisValidInfrastructureTld(java.lang.String iTld)Returns true if the specifiedStringmatches any IANA-defined infrastructure top-level domain.booleanisValidLocalTld(java.lang.String lTld)Returns true if the specifiedStringmatches any widely used "local" domains (localhost or localdomain).booleanisValidTld(java.lang.String tld)Returns true if the specifiedStringmatches any IANA-defined top-level domain.static java.lang.StringunicodeToASCII(java.lang.String input)Converts potentially Unicode input to punycode.static voidupdateTLDOverride(DomainValidator.ArrayType table, java.lang.String... tlds)Update one of the TLD override arrays.-
Methods inherited from class org.openstreetmap.josm.data.validation.routines.AbstractValidator
getErrorMessage, setErrorMessage
-
-
-
-
Field Detail
-
MAX_DOMAIN_LENGTH
private static final int MAX_DOMAIN_LENGTH
- See Also:
- Constant Field Values
-
EMPTY_STRING_ARRAY
private static final java.lang.String[] EMPTY_STRING_ARRAY
-
DOMAIN_LABEL_REGEX
private static final java.lang.String DOMAIN_LABEL_REGEX
- See Also:
- Constant Field Values
-
TOP_LABEL_REGEX
private static final java.lang.String TOP_LABEL_REGEX
- See Also:
- Constant Field Values
-
DOMAIN_NAME_REGEX
private static final java.lang.String DOMAIN_NAME_REGEX
- See Also:
- Constant Field Values
-
allowLocal
private final boolean allowLocal
-
DOMAIN_VALIDATOR
private static final DomainValidator DOMAIN_VALIDATOR
Singleton instance of this validator, which doesn't consider local addresses as valid.
-
DOMAIN_VALIDATOR_WITH_LOCAL
private static final DomainValidator DOMAIN_VALIDATOR_WITH_LOCAL
Singleton instance of this validator, which does consider local addresses valid.
-
domainRegex
private final RegexValidator domainRegex
RegexValidator for matching domains.
-
hostnameRegex
private final RegexValidator hostnameRegex
RegexValidator for matching a local hostname
-
INFRASTRUCTURE_TLDS
private static final java.lang.String[] INFRASTRUCTURE_TLDS
-
GENERIC_TLDS
private static final java.lang.String[] GENERIC_TLDS
-
COUNTRY_CODE_TLDS
private static final java.lang.String[] COUNTRY_CODE_TLDS
-
LOCAL_TLDS
private static final java.lang.String[] LOCAL_TLDS
-
inUse
private static boolean inUse
-
countryCodeTLDsPlus
private static volatile java.lang.String[] countryCodeTLDsPlus
-
genericTLDsPlus
private static volatile java.lang.String[] genericTLDsPlus
-
countryCodeTLDsMinus
private static volatile java.lang.String[] countryCodeTLDsMinus
-
genericTLDsMinus
private static volatile java.lang.String[] genericTLDsMinus
-
-
Constructor Detail
-
DomainValidator
private DomainValidator(boolean allowLocal)
Private constructor.- Parameters:
allowLocal- whether to allow local domains
-
-
Method Detail
-
getInstance
public static DomainValidator getInstance()
Returns the singleton instance of this validator. It will not consider local addresses as valid.- Returns:
- the singleton instance of this validator
-
getInstance
public static DomainValidator getInstance(boolean allowLocal)
Returns the singleton instance of this validator, with local validation as required.- Parameters:
allowLocal- Should local addresses be considered valid?- Returns:
- the singleton instance of this validator
-
isValid
public boolean isValid(java.lang.String domain)
Returns true if the specifiedStringparses as a valid domain name with a recognized top-level domain. The parsing is case-insensitive.- Specified by:
isValidin classAbstractValidator- Parameters:
domain- the parameter to check for domain name syntax- Returns:
- true if the parameter is a valid domain name
-
getValidatorName
public java.lang.String getValidatorName()
Description copied from class:AbstractValidatorReturns the name of this validator- Specified by:
getValidatorNamein classAbstractValidator- Returns:
- the name of this validator
-
isValidDomainSyntax
boolean isValidDomainSyntax(java.lang.String domain)
-
isValidTld
public boolean isValidTld(java.lang.String tld)
Returns true if the specifiedStringmatches any IANA-defined top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
tld- the parameter to check for TLD status, not null- Returns:
- true if the parameter is a TLD
-
isValidInfrastructureTld
public boolean isValidInfrastructureTld(java.lang.String iTld)
Returns true if the specifiedStringmatches any IANA-defined infrastructure top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
iTld- the parameter to check for infrastructure TLD status, not null- Returns:
- true if the parameter is an infrastructure TLD
-
isValidGenericTld
public boolean isValidGenericTld(java.lang.String gTld)
Returns true if the specifiedStringmatches any IANA-defined generic top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
gTld- the parameter to check for generic TLD status, not null- Returns:
- true if the parameter is a generic TLD
-
isValidCountryCodeTld
public boolean isValidCountryCodeTld(java.lang.String ccTld)
Returns true if the specifiedStringmatches any IANA-defined country code top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
ccTld- the parameter to check for country code TLD status, not null- Returns:
- true if the parameter is a country code TLD
-
isValidLocalTld
public boolean isValidLocalTld(java.lang.String lTld)
Returns true if the specifiedStringmatches any widely used "local" domains (localhost or localdomain). Leading dots are ignored if present. The search is case-insensitive.- Parameters:
lTld- the parameter to check for local TLD status, not null- Returns:
- true if the parameter is an local TLD
-
chompLeadingDot
private static java.lang.String chompLeadingDot(java.lang.String str)
-
clearTLDOverrides
static void clearTLDOverrides()
-
updateTLDOverride
public static void updateTLDOverride(DomainValidator.ArrayType table, java.lang.String... tlds)
Update one of the TLD override arrays. This must only be done at program startup, before any instances are accessed using getInstance.For example:
DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, new String[]{"apache"})}To clear an override array, provide an empty array.
- Parameters:
table- the table to update, seeDomainValidator.ArrayTypeMust be one of the following- COUNTRY_CODE_MINUS
- COUNTRY_CODE_PLUS
- GENERIC_MINUS
- GENERIC_PLUS
tlds- the array of TLDs, must not be null- Throws:
java.lang.IllegalStateException- if the method is called after getInstancejava.lang.IllegalArgumentException- if one of the read-only tables is requested- Since:
- 1.5.0
-
getTLDEntries
public static java.lang.String[] getTLDEntries(DomainValidator.ArrayType table)
Get a copy of the internal array.- Parameters:
table- the array type (any of the enum values)- Returns:
- a copy of the array
- Throws:
java.lang.IllegalArgumentException- if the table type is unexpected (should not happen)- Since:
- 1.5.1
-
unicodeToASCII
public static java.lang.String unicodeToASCII(java.lang.String input)
Converts potentially Unicode input to punycode. If conversion fails, returns the original input.- Parameters:
input- the string to convert, not null- Returns:
- converted input, or original input if conversion fails
-
isOnlyASCII
private static boolean isOnlyASCII(java.lang.String input)
-
arrayContains
private static boolean arrayContains(java.lang.String[] sortedArray, java.lang.String key)
Check if a sorted array contains the specified key- Parameters:
sortedArray- the array to searchkey- the key to find- Returns:
trueif the array contains the key
-
-