PatternParser.java
3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.diligrp.assistant.uid.pattern;
import com.diligrp.assistant.shared.ErrorCode;
import com.diligrp.assistant.uid.exception.UidServiceException;
import java.util.ArrayList;
import java.util.List;
public class PatternParser {
private final String pattern;
private final int length;
private TokenizerState state;
private int index;
public PatternParser(String pattern) {
this.pattern = pattern;
this.length = pattern.length();
this.index = 0;
this.state = TokenizerState.LITERAL_STATE;
}
public List<Token> parse() {
List<Token> tokens = new ArrayList<>();
StringBuilder buf = new StringBuilder();
while(index < length) {
char c = pattern.charAt(index);
index ++;
switch (this.state) {
case LITERAL_STATE:
handleLiteralState(c, tokens, buf);
break;
case KEYWORD_STATE:
handleKeywordState(c, tokens, buf);
break;
case OPTION_STATE:
handleOptionState(c, tokens, buf);
break;
}
}
switch (state) {
case LITERAL_STATE:
handleLiteralState('%', tokens, buf);
break;
case KEYWORD_STATE:
handleKeywordState('%', tokens, buf);
break;
default:
throw new UidServiceException(ErrorCode.ILLEGAL_ARGUMENT_ERROR, "Unexpected end of pattern string");
}
return tokens;
}
private void handleLiteralState(char c, List<Token> tokens, StringBuilder buf) {
switch (c) {
case '%':
addLiteralToken(buf, tokens);
state = TokenizerState.KEYWORD_STATE;
break;
default:
buf.append(c);
}
}
private void handleKeywordState(char c, List<Token> tokens, StringBuilder buf) {
switch (c) {
case '%':
addKeywordToken(buf, tokens);
state = TokenizerState.KEYWORD_STATE;
break;
case '{':
this.addKeywordToken(buf, tokens);
this.state = TokenizerState.OPTION_STATE;
break;
default:
buf.append(c);
}
}
private void handleOptionState(char c, List<Token> tokens, StringBuilder buf) {
switch (c) {
case '}':
addOptionToken(buf, tokens);
state = TokenizerState.LITERAL_STATE;
break;
default:
buf.append(c);
}
}
private void addLiteralToken(StringBuilder buf, List<Token> tokens) {
if (!buf.isEmpty()) {
tokens.add(new LiteralToken(buf.toString()));
buf.setLength(0);
}
}
private void addKeywordToken(StringBuilder buf, List<Token> tokens) {
if (!buf.isEmpty()) {
tokens.add(new KeywordToken(buf.toString()));
buf.setLength(0);
}
}
private void addOptionToken(StringBuilder buf, List<Token> tokens) {
if (!buf.isEmpty()) {
tokens.add(new OptionToken(buf.toString()));
buf.setLength(0);
}
}
private enum TokenizerState {
LITERAL_STATE,
KEYWORD_STATE,
OPTION_STATE
}
}