![JAR search and dependency download from the Maven repository](/logo.png)
net.weweave.commerce.DomainList Maven / Gradle / Ivy
package net.weweave.commerce;
import java.util.ArrayList;
import java.util.List;
/**
* A {@link DomainList} object represents a list of domain names.
*
* It can be used to transform a list of domain names into a regular expression
* string which can be used as a {@code subject} in a {@link LicenseKeyEncoder}
* object.
*
* On the other hand, it can be constructed from a regular expression,
* transforming the domain names of the regular expression into user-
* readable string items.
*
* See the documentation of weweave Commerce for more details at:
* weweave.net
*
* @author weweave GbR
* @see LicenseKeyEncoder
*/
public class DomainList {
private static final String DOMAIN_PREFIX = "(.*\\.)?";
private final List domains;
/**
* Constructs a domain list from a regular expression which is formatted
* in a way that is generated by this class.
*
* @param regex the regular expression to be parsed
*/
public DomainList(String regex) {
this();
this.domains.addAll(extractDomains(regex));
}
/**
* Constructs an empty domain list.
*/
public DomainList() {
this.domains = new ArrayList<>();
}
/**
* @return the number of domain names
*/
public int getNumDomains() {
return domains.size();
}
/**
* @return the actual domain names
*/
public List getDomains() {
return new ArrayList<>(this.domains);
}
/**
* Adds a domain name to the list.
*
* @param domain the domain name to be added
* @param assureIncludeDot if true, an {@link IllegalArgumentException}
* will be thrown if no dot is included in the
* domain name (default: true)
* @param assureSingleDot if true, an {@link IllegalArgumentException}
* will be thrown if there is more than one dot
* in the domain name (default: true)
*/
public void addDomain(String domain, boolean assureIncludeDot, boolean assureSingleDot) {
if (GenericValidator.isBlankOrNull(domain)) {
throw new IllegalArgumentException("Domain must not be null or empty");
}
if (assureIncludeDot && !domain.contains(".")) {
throw new IllegalArgumentException("Domain must consist of top and second level domain");
}
if (assureSingleDot && domain.indexOf(".") != domain.lastIndexOf(".")) {
throw new IllegalArgumentException("There must be exactly one dot (.) in the domain name");
}
if (domain.matches(".*[\\*\\[\\]\\{\\}\\(\\)\\?\\+].*")) {
throw new IllegalArgumentException("Invalid characters in domain name");
}
this.domains.add(domain);
}
public void addDomain(String domain, boolean assureIncludeDot) {
this.addDomain(domain, assureIncludeDot, true);
}
public void addDomain(String domain) {
this.addDomain(domain, true, true);
}
/**
* @return a regular expression of domain names which i.e. can be used as
* the subject in a {@link LicenseKeyEncoder} object
*/
public String getRegEx() {
StringBuilder sb = new StringBuilder();
sb.append("^(");
int i = 0;
for (String domain : this.domains) {
if (i > 0) {
sb.append("|");
}
sb.append(DOMAIN_PREFIX + domain.replace(".", "\\."));
i++;
}
sb.append(")$");
return sb.toString();
}
private List extractDomains(String regex) {
List result = new ArrayList<>();
if (regex.endsWith(")$")) {
regex = regex.substring(0, regex.length()-2);
regex = regex.substring(2);
} else if (regex.endsWith("$")) {
regex = regex.substring(0, regex.length()-1);
regex = regex.substring(1);
}
String[] tokens = regex.split("\\|");
for (String token : tokens) {
if (!GenericValidator.isBlankOrNull(token)) {
if (token.startsWith(DOMAIN_PREFIX)) {
token = token.substring(DOMAIN_PREFIX.length());
}
String domain = "*."+token.replace("\\.", ".");
result.add(domain);
}
}
return result;
}
/**
* Extracts the top level domain (TLD) from a domain name.
*
* @param domain the domain name
* @return the top level domain (TLD)
*/
public static String getTldFromDomain(String domain) {
int start = domain.indexOf(".");
if (start == -1) {
return "";
}
String tld = domain.substring(start+1);
return tld;
}
}