1 package org.bitbucket.jrsofty.parser.logging.api;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6
7
8
9
10
11
12 public class TokenMatcher {
13
14 private final Pattern regexPattern;
15 private final String tokenMarker;
16
17 public TokenMatcher(final String regex, final String tokenMarker) {
18 this.tokenMarker = tokenMarker;
19 this.regexPattern = Pattern.compile(regex);
20 }
21
22
23
24
25
26
27 public String getPattern() {
28 return this.regexPattern.toString();
29 }
30
31
32
33
34
35
36
37 public String getTokenMarker() {
38 return this.tokenMarker;
39 }
40
41
42
43
44
45
46
47
48
49
50
51 public String validateToken(final String token) throws TokenParseException {
52
53 final Matcher matcher = this.regexPattern.matcher(token);
54 matcher.find();
55 try {
56 final String result = matcher.group(0);
57 return result;
58 } catch (final IllegalStateException e) {
59 throw new TokenParseException(token, this.tokenMarker, this.regexPattern.toString(), e);
60 }
61
62 }
63
64 }