parse_arguments.dart
6.07 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import 'package:flutter/material.dart';
class ParseRoute {
final List<ParseRouteSplit> _routeSplits = <ParseRouteSplit>[];
void addRoute(String routePath) {
String path = routePath;
if (path == Navigator.defaultRouteName) {
var routeSplit = ParseRouteSplit(path, ParseRouteSplitType.component);
routeSplit.routes = [routePath];
_routeSplits.add(routeSplit);
return;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
List<String> pathComponents = path.split('/');
ParseRouteSplit parent;
for (int i = 0; i < pathComponents.length; i++) {
String component = pathComponents[i];
ParseRouteSplit routeSplit = _routeSplitForComponent(component, parent);
if (routeSplit == null) {
ParseRouteSplitType type = _typeForComponent(component);
routeSplit = ParseRouteSplit(component, type);
routeSplit.parent = parent;
if (parent == null) {
_routeSplits.add(routeSplit);
} else {
parent.routeSplits.add(routeSplit);
}
}
if (i == pathComponents.length - 1) {
if (routeSplit.routes == null) {
routeSplit.routes = [routePath];
} else {
routeSplit.routes.add(routePath);
}
}
parent = routeSplit;
}
}
AppRouteMatch split(String path) {
String usePath = path;
if (usePath.startsWith("/")) {
usePath = path.substring(1);
}
List<String> components = usePath.split("/");
if (path == Navigator.defaultRouteName) {
components = ["/"];
}
Map<ParseRouteSplit, ParseRouteSplitMatch> routeSplitMatches =
<ParseRouteSplit, ParseRouteSplitMatch>{};
List<ParseRouteSplit> routeSplitsToCheck = _routeSplits;
for (String checkComponent in components) {
Map<ParseRouteSplit, ParseRouteSplitMatch> currentMatches =
<ParseRouteSplit, ParseRouteSplitMatch>{};
List<ParseRouteSplit> nextrouteSplits = <ParseRouteSplit>[];
for (ParseRouteSplit routeSplit in routeSplitsToCheck) {
String pathPart = checkComponent;
Map<String, String> queryMap = {};
if (checkComponent.contains("?") && !checkComponent.contains("=")) {
var splitParam = checkComponent.split("?");
pathPart = splitParam[0];
queryMap = {pathPart: splitParam[1]};
} else if (checkComponent.contains("?")) {
var splitParam = checkComponent.split("?");
var splitParam2 = splitParam[1].split("=");
if (!splitParam2[1].contains("&")) {
pathPart = splitParam[0];
queryMap = {splitParam2[0]: splitParam2[1]};
} else {
pathPart = splitParam[0];
final segunda = splitParam[1];
var other = segunda.split(RegExp(r"[&,=]"));
for (var i = 0; i < (other.length - 1); i++) {
bool impar = (i % 2 == 0);
if (impar) {
queryMap.addAll({other[0 + i]: other[1 + i]});
}
}
}
}
bool isMatch =
(routeSplit.part == pathPart || routeSplit.isParameter());
if (isMatch) {
ParseRouteSplitMatch parentMatch =
routeSplitMatches[routeSplit.parent];
ParseRouteSplitMatch match =
ParseRouteSplitMatch.fromMatch(parentMatch, routeSplit);
if (routeSplit.isParameter()) {
String paramKey = routeSplit.part.substring(1);
match.parameters[paramKey] = pathPart;
}
if (queryMap != null) {
match.parameters.addAll(queryMap);
}
currentMatches[routeSplit] = match;
if (routeSplit.routeSplits != null) {
nextrouteSplits.addAll(routeSplit.routeSplits);
}
}
}
routeSplitMatches = currentMatches;
routeSplitsToCheck = nextrouteSplits;
if (currentMatches.values.length == 0) {
return null;
}
}
List<ParseRouteSplitMatch> matches = routeSplitMatches.values.toList();
if (matches.length > 0) {
ParseRouteSplitMatch match = matches.first;
ParseRouteSplit routeSplitToUse = match.routeSplit;
if (routeSplitToUse != null &&
routeSplitToUse.routes != null &&
routeSplitToUse.routes.length > 0) {
AppRouteMatch routeMatch = AppRouteMatch();
routeMatch.parameters = match.parameters;
if (routeSplitToUse.isParameter()) {
routeMatch.route = match.routeSplit.parent.part;
} else {
routeMatch.route = match.routeSplit.part;
}
return routeMatch;
}
}
return null;
}
ParseRouteSplit _routeSplitForComponent(
String component, ParseRouteSplit parent) {
List<ParseRouteSplit> routeSplits = _routeSplits;
if (parent != null) {
routeSplits = parent.routeSplits;
}
for (ParseRouteSplit routeSplit in routeSplits) {
if (routeSplit.part == component) {
return routeSplit;
}
}
return null;
}
ParseRouteSplitType _typeForComponent(String component) {
ParseRouteSplitType type = ParseRouteSplitType.component;
if (_isParameterComponent(component)) {
type = ParseRouteSplitType.parameter;
}
return type;
}
bool _isParameterComponent(String component) {
return component.startsWith(":");
}
}
enum ParseRouteSplitType {
component,
parameter,
}
class AppRouteMatch {
Map<String, String> parameters = <String, String>{};
String route = '/';
}
class ParseRouteSplitMatch {
ParseRouteSplitMatch(this.routeSplit);
ParseRouteSplitMatch.fromMatch(ParseRouteSplitMatch match, this.routeSplit) {
parameters = <String, String>{};
if (match != null) {
parameters.addAll(match.parameters);
}
}
ParseRouteSplit routeSplit;
Map<String, String> parameters = <String, String>{};
}
class ParseRouteSplit {
ParseRouteSplit(this.part, this.type);
String part;
ParseRouteSplitType type;
List<String> routes = [];
List<ParseRouteSplit> routeSplits = <ParseRouteSplit>[];
ParseRouteSplit parent;
bool isParameter() {
return type == ParseRouteSplitType.parameter;
}
}