public static Map < DomainParts , String > splitDomain ( String domain ) { Map < DomainParts , String > domainParts = null ; String [ ] dottedParts = domain . trim ( ) . toLowerCase ( Locale . ENGLISH ) . split ( "\\." ) ; if ( dottedParts . length == 2 ) { domainParts = new HashMap <> ( ) ; domainParts . put ( DomainParts . SUBDOMAIN , null ) ; domainParts . put ( DomainParts . DOMAINNAME , dottedParts [ 0 ] ) ; domainParts . put ( DomainParts . TLD , dottedParts [ 1 ] ) ; } else if ( dottedParts . length > 2 ) { int n = dottedParts . length ; if ( COMMON_TLD . contains ( dottedParts [ n - 2 ] + "." + dottedParts [ n - 1 ] ) || ( COMMON_TLD . contains ( dottedParts [ n - 1 ] ) && COMMON_SLD . contains ( dottedParts [ n - 2 ] ) ) || ( COMMON_TLD . contains ( dottedParts [ n - 2 ] ) && COMMON_SLD . contains ( dottedParts [ n - 1 ] ) ) ) { domainParts = new HashMap <> ( ) ; domainParts . put ( DomainParts . TLD , dottedParts [ n - 2 ] + "." + dottedParts [ n - 1 ] ) ; domainParts . put ( DomainParts . DOMAINNAME , dottedParts [ n - 3 ] ) ; StringBuilder sb = new StringBuilder ( dottedParts [ 0 ] ) ; for ( int i = 1 ; i < n - 3 ; ++ i ) { sb . append ( "." ) . append ( dottedParts [ i ] ) ; } domainParts . put ( DomainParts . SUBDOMAIN , sb . toString ( ) ) ; } else if ( COMMON_TLD . contains ( dottedParts [ n - 1 ] ) ) { domainParts = new HashMap <> ( ) ; domainParts . put ( DomainParts . TLD , dottedParts [ n - 1 ] ) ; domainParts . put ( DomainParts . DOMAINNAME , dottedParts [ n - 2 ] ) ; StringBuilder sb = new StringBuilder ( dottedParts [ 0 ] ) ; for ( int i = 1 ; i < n - 2 ; ++ i ) { sb . append ( "." ) . append ( dottedParts [ i ] ) ; } domainParts . put ( DomainParts . SUBDOMAIN , sb . toString ( ) ) ; } } return domainParts ; }
Splits a domain name to parts and returns them in a map .