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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing;
/**
* This replaces the incredibly slow javacc RouteParser.jj. It is a has its c++ sibling and
* the implementation is a a copy of the C++ version.
* @author baldersheim
* @since 5.2
*/
public class RouteParser {
private final String routeText;
RouteParser(String route) {
this.routeText = route;
}
Route route() {
Route route = new Route();
for (int from = 0, at = 0, depth = 0; at <= routeText.length(); ++at) {
if (at == routeText.length() || ((depth == 0) && Character.isWhitespace(routeText.charAt(at)))) {
if (from < at) {
Hop hop = createHop(routeText.substring(from, at));
if (hop.hasDirectives() && hop.getDirective(0) instanceof ErrorDirective) {
return new Route().addHop(new Hop().addDirective(hop.getDirective(0)));
}
route.addHop(hop);
}
from = at + 1;
} else if ((routeText.charAt(at) == '(') || (routeText.charAt(at) == '[') ) {
++depth;
} else if ((routeText.charAt(at) == ')') || (routeText.charAt(at) == ']')) {
--depth;
}
}
return route;
}
private static Hop createHop(String s) {
final int len = s.length();
if (len == 0) {
return new Hop().addDirective(createErrorDirective("Failed to parse empty string."));
} else if (len > 1 && (s.charAt(0) == '?')) {
return createHop(s.substring(1, len)).setIgnoreResult(true);
} else if (len > 4 && s.charAt(0) == 't' && s.charAt(1) == 'c' && s.charAt(2) == 'p' && s.charAt(3) == '/') {
HopDirective directive = createTcpDirective(s.substring(4, len));
if (directive != null) {
return new Hop().addDirective(directive);
}
} else if (len > 6 && s.charAt(0) == 'r' && s.charAt(1) == 'o' && s.charAt(2) == 'u'
&& s.charAt(3) == 't' && s.charAt(4) == 'e' && s.charAt(5) == ':') {
return new Hop().addDirective(createRouteDirective(s.substring(6, len)));
}
Hop hop = new Hop();
for (int from = 0, at = 0, depth = 0; at <= len; ++at) {
if (at == len) {
if (depth > 0) {
return new Hop().addDirective(createErrorDirective("Unterminated '[' in '" + s + "'"));
}
hop.addDirective(createDirective(s.substring(from, at)));
from = at + 1;
} else {
char c = s.charAt(at);
if (Character.isWhitespace(c) && depth == 0) {
return new Hop().addDirective(createErrorDirective("Failed to completely parse '" + s + "'."));
} else if ((depth == 0 && c == '/')) {
hop.addDirective(createDirective(s.substring(from, at)));
from = at + 1;
} else if (c == '[') {
++depth;
} else if (c == ']') {
if (depth == 0) {
return new Hop().addDirective(createErrorDirective("Unexpected token ']' in '" + s + "'"));
}
--depth;
}
}
}
return hop;
}
private static HopDirective createErrorDirective(String s) {
return new ErrorDirective(s);
}
private static HopDirective createTcpDirective(String s) {
int posP = s.indexOf(':');
if (posP <= 0) {
return null;
}
int posS = s.indexOf('/', posP+1);
if (posS <= posP + 1) {
return null;
}
return new TcpDirective(s.substring(0, posP),
Integer.valueOf(s.substring(posP + 1, posS)),
s.substring(posS + 1));
}
private static HopDirective createRouteDirective(String s) {
return new RouteDirective(s);
}
private static HopDirective createDirective(String s) {
return (s.length() > 2 && s.charAt(0) == '[')
? ((s.charAt(s.length() - 1) == ']')
? createPolicyDirective(s.substring(1, s.length() - 1))
: createErrorDirective("Unterminated '[' in '" + s + "'"))
: createVerbatimDirective(s);
}
private static HopDirective createPolicyDirective(String s)
{
int pos = s.indexOf(':');
return (pos == -1)
? new PolicyDirective(s, "")
: new PolicyDirective(s.substring(0, pos), s.substring(pos + 1).trim());
}
private static HopDirective createVerbatimDirective(String s) {
return new VerbatimDirective(s);
}
}
|