-
Notifications
You must be signed in to change notification settings - Fork 188
/
AxiomNameResolver.java
69 lines (56 loc) · 2.34 KB
/
AxiomNameResolver.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
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.axiom.lang.spi;
import java.util.Optional;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.meta.Inheritance;
import com.evolveum.axiom.api.schema.AxiomItemDefinition;
import com.evolveum.axiom.api.schema.AxiomTypeDefinition;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import org.jetbrains.annotations.Nullable;
public interface AxiomNameResolver {
final AxiomNameResolver AXIOM_DEFAULT_NAMESPACE = defaultNamespace(AxiomName.AXIOM_NAMESPACE);
final Set<String> BUILTINS = ImmutableSet.of("string","boolean","uri", "int", "binary", "dateTime");
final AxiomNameResolver BUILTIN_TYPES = (prefix, localName) -> {
if((prefix == null || prefix.isEmpty()) && BUILTINS.contains(localName)) {
return AxiomName.axiom(localName);
}
return null;
};
final AxiomNameResolver NULL_RESOLVER = (p, n) -> null;
AxiomName resolveIdentifier(@Nullable String prefix, @NotNull String localName);
static AxiomNameResolver defaultNamespace(String namespace) {
return (prefix, localName) -> Strings.isNullOrEmpty(prefix) ? AxiomName.from(namespace, localName) : null;
}
static AxiomNameResolver nullResolver() {
return NULL_RESOLVER;
}
default AxiomNameResolver or(AxiomNameResolver next) {
return (prefix, localName) -> {
AxiomName maybe = this.resolveIdentifier(prefix, localName);
if (maybe != null) {
return maybe;
}
return next.resolveIdentifier(prefix, localName);
};
}
static AxiomNameResolver defaultNamespaceFromType(AxiomTypeDefinition type) {
return (prefix, localName) -> {
if(Strings.isNullOrEmpty(prefix)) {
AxiomName localNs = AxiomName.local(localName);
Optional<AxiomItemDefinition> childDef = type.itemDefinition(localNs);
if(childDef.isPresent()) {
return Inheritance.adapt(type.name(), childDef.get());
}
}
return null;
};
}
}