| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.dnslabel; |
| 12 |
|
| 13 |
/** |
| 14 |
* A LDH (<b>L</b>etters, <b>D</b>igits, <b>H</b>yphen) label, which is the classical label form. |
| 15 |
* |
| 16 |
* @see <a href="https://tools.ietf.org/html/rfc5890#section-2.3.1">RFC 5890 ยง 2.3.1. LDH Label</a> |
| 17 |
* |
| 18 |
*/ |
| 19 |
public abstract class LdhLabel extends DnsLabel { |
| 20 |
|
| 21 |
protected LdhLabel(String label) { |
| 22 |
super(label); |
| 23 |
} |
| 24 |
|
| 25 |
public static boolean isLdhLabel(String label) { |
| 26 |
if (label.isEmpty()) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
if (LeadingOrTrailingHyphenLabel.isLeadingOrTrailingHypenLabelInternal(label)) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
for (int i = 0; i < label.length(); i++) { |
| 35 |
char c = label.charAt(i); |
| 36 |
if ((c >= 'a' && c <= 'z') |
| 37 |
|| (c >= 'A' && c <= 'Z') |
| 38 |
|| (c >= '0' && c <= '9') |
| 39 |
|| c == '-') { |
| 40 |
continue; |
| 41 |
} |
| 42 |
return false; |
| 43 |
} |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
protected static LdhLabel fromInternal(String label) { |
| 48 |
assert isLdhLabel(label); |
| 49 |
|
| 50 |
if (ReservedLdhLabel.isReservedLdhLabel(label)) { |
| 51 |
// Label starts with '??--'. Now let us see if it is a XN-Label, starting with 'xn--', but be aware that the |
| 52 |
// 'xn' part is case insensitive. The XnLabel.isXnLabelInternal(String) method takes care of this. |
| 53 |
if (XnLabel.isXnLabelInternal(label)) { |
| 54 |
return XnLabel.fromInternal(label); |
| 55 |
} else { |
| 56 |
return new ReservedLdhLabel(label); |
| 57 |
} |
| 58 |
} |
| 59 |
return new NonReservedLdhLabel(label); |
| 60 |
} |
| 61 |
} |