-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
TypeDslLoader.java
81 lines (76 loc) · 2.5 KB
/
TypeDslLoader.java
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
package ${package}.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TypeDslLoader<T> implements Loader<T> {
private final Loader<T> innerLoader;
private final Integer refScope;
private static final Pattern TYPE_DSL_REGEX = Pattern.compile("^([^\\[?]+)(\\[\\])?(\\?)?$");
public TypeDslLoader(final Loader<T> innerLoader, final Integer refScope) {
this.innerLoader = innerLoader;
this.refScope = refScope;
}
private Object resolve(
final String doc_, final String baseUri, final LoadingOptions loadingOptions) {
final Matcher m = TYPE_DSL_REGEX.matcher(doc_);
if (m.matches()) {
final String first =
loadingOptions.expandUrl(m.group(1), baseUri, false, true, this.refScope);
Object second = null;
Object third = null;
if (m.group(2) != null && m.group(2).length() > 0) {
HashMap<String, Object> resolvedMap = new HashMap<String, Object>();
resolvedMap.put("type", "array");
resolvedMap.put("items", first);
second = resolvedMap;
}
if (m.group(3) != null && m.group(3).length() > 0) {
third = Arrays.asList("null", second != null ? second : first);
}
if (third != null) {
return third;
} else {
return second != null ? second : first;
}
} else {
return doc_;
}
}
public T load(
final Object doc_,
final String baseUri,
final LoadingOptions loadingOptions,
final String docRoot) {
Object doc = doc_;
if (doc instanceof List) {
final List<Object> docList = (List<Object>) doc;
final List<Object> r = new ArrayList<Object>();
for (final Object d : docList) {
if (d instanceof String) {
Object resolved = this.resolve((String) d, baseUri, loadingOptions);
if (resolved instanceof List) {
List<Object> resolvedList = (List<Object>) resolved;
for (Object i : resolvedList) {
if (!r.contains(i)) {
r.add(i);
}
}
} else {
if (!r.contains(resolved)) {
r.add(resolved);
}
}
} else {
r.add(d);
}
}
doc = docList;
} else if (doc instanceof String) {
doc = this.resolve((String) doc, baseUri, loadingOptions);
}
return this.innerLoader.load(doc, baseUri, loadingOptions);
}
}