|
2 | 2 |
|
3 | 3 | import emailvalidator4j.lexer.EmailLexer; |
4 | 4 | import emailvalidator4j.lexer.Token; |
| 5 | +import emailvalidator4j.lexer.TokenInterface; |
5 | 6 | import emailvalidator4j.lexer.Tokens; |
6 | 7 | import emailvalidator4j.parser.exception.*; |
7 | 8 |
|
| 9 | +import java.util.HashSet; |
8 | 10 | import java.util.regex.Matcher; |
9 | 11 | import java.util.regex.Pattern; |
10 | 12 |
|
11 | 13 | final class DomainPart extends Parser { |
12 | 14 |
|
13 | 15 | private static final int DOMAINPART_MAX_LENGTH = 255; |
14 | 16 | private static final int LABEL_MAX_LENGTH = 63; |
| 17 | + private final HashSet<TokenInterface> notAllowedTokens = new HashSet<TokenInterface>(2) {{ |
| 18 | + add(Tokens.BACKSLASH); |
| 19 | + add(Tokens.SLASH); |
| 20 | + }}; |
15 | 21 |
|
16 | 22 | DomainPart (EmailLexer lexer) { |
17 | 23 | super(lexer); |
@@ -51,11 +57,7 @@ private void doParseDomainPart() throws InvalidEmail { |
51 | 57 | throw new ExpectedATEXT("Expected ATEXT"); |
52 | 58 | } |
53 | 59 |
|
54 | | - if (this.lexer.getCurrent().equals(Tokens.SLASH)) { |
55 | | - throw new DomainNotAllowedCharacter( |
56 | | - String.format("%s is not allowed in domain part", this.lexer.getCurrent().getName()) |
57 | | - ); |
58 | | - } |
| 60 | + checkNotAllowedChars(this.lexer.getCurrent()); |
59 | 61 |
|
60 | 62 | if (this.lexer.getCurrent().equals(Tokens.OPENPARETHESIS)) { |
61 | 63 | if (this.lexer.getPrevious().equals(Tokens.AT)) { |
@@ -83,6 +85,15 @@ private void doParseDomainPart() throws InvalidEmail { |
83 | 85 | } while (!this.lexer.isAtEnd()); |
84 | 86 | } |
85 | 87 |
|
| 88 | + private void checkNotAllowedChars(TokenInterface token) throws DomainNotAllowedCharacter { |
| 89 | + if (notAllowedTokens.contains(token)) { |
| 90 | + throw new DomainNotAllowedCharacter( |
| 91 | + String.format("%s is not allowed in domain part", token.getName()) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
86 | 97 | private void checkLabelLength() { |
87 | 98 | if (this.lexer.getCurrent().equals(Tokens.DOT) && |
88 | 99 | this.lexer.getPrevious().equals(Tokens.get(Tokens.GENERIC)) && |
|
0 commit comments