-
Notifications
You must be signed in to change notification settings - Fork 188
/
AxiomItemTarget.java
323 lines (253 loc) · 9.44 KB
/
AxiomItemTarget.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
e * 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.api.stream;
import java.util.Optional;
import java.util.function.Supplier;
import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.AxiomItem;
import com.evolveum.axiom.api.AxiomItemBuilder;
import com.evolveum.axiom.api.AxiomValue;
import com.evolveum.axiom.api.AxiomValueBuilder;
import com.evolveum.axiom.api.schema.AxiomItemDefinition;
import com.evolveum.axiom.api.schema.AxiomSchemaContext;
import com.evolveum.axiom.api.schema.AxiomTypeDefinition;
import com.evolveum.axiom.concepts.Lazy;
import com.evolveum.axiom.concepts.SourceLocation;
import com.evolveum.axiom.lang.api.AxiomBuiltIn;
import com.evolveum.axiom.lang.spi.AxiomNameResolver;
import com.evolveum.axiom.lang.spi.AxiomSemanticException;
import com.google.common.base.Preconditions;
public class AxiomItemTarget extends AxiomBuilderStreamTarget implements Supplier<AxiomItem<?>>, AxiomItemStream.TargetWithResolver {
private final AxiomSchemaContext context;
private final AxiomNameResolver resolver;
private AxiomTypeDefinition infraType = AxiomBuiltIn.Type.AXIOM_VALUE;
private Item<?> result;
public AxiomItemTarget(AxiomSchemaContext context) {
this(context, AxiomNameResolver.nullResolver());
}
public AxiomItemTarget(AxiomSchemaContext context, AxiomNameResolver rootResolver) {
offer(new Root());
this.context = context;
this.resolver = Preconditions.checkNotNull(rootResolver, "rootResolver");
}
@Override
public AxiomItem<?> get() {
return result.get();
}
private final class Root implements ValueBuilder {
@Override
public AxiomName name() {
return AxiomName.axiom("AbstractRoot");
}
@Override
public AxiomNameResolver itemResolver() {
return axiomAsConditionalDefault().or(resolver);
}
@Override
public AxiomNameResolver valueResolver() {
return resolver;
}
@Override
public Optional<AxiomItemDefinition> childItemDef(AxiomName statement) {
return context.getRoot(statement);
}
@Override
public Optional<AxiomItemDefinition> infraItemDef(AxiomName item) {
return infraType.itemDefinition(item);
}
@Override
public ItemBuilder startItem(AxiomName name, SourceLocation loc) {
result = new Item<>(childItemDef(name).get());
return result;
}
@Override
public void endValue(SourceLocation loc) {
}
@Override
public ItemBuilder startInfra(AxiomName name, SourceLocation loc) {
// TODO Auto-generated method stub
return null;
}
}
private class Item<V> implements ItemBuilder, Supplier<AxiomItem<V>> {
protected final AxiomItemBuilder<V> builder;
public Item(AxiomItemDefinition definition) {
this.builder = new AxiomItemBuilder<>(definition);
}
@Override
public AxiomName name() {
return builder.definition().name();
}
@Override
public AxiomNameResolver itemResolver() {
return resolver;
}
@Override
public AxiomNameResolver valueResolver() {
return resolver;
}
protected Value<V> onlyValue() {
return (Value<V>) builder.onlyValue();
}
@Override
public Value<V> startValue(Object value, SourceLocation loc) {
Value<V> newValue = new Value<>((V) value, builder.definition().typeDefinition());
addValue(newValue);
return newValue;
}
protected void addValue(Value<V> value) {
builder.addValue(value);
}
@Override
public void endNode(SourceLocation loc) {
// Noop for now
}
@Override
public AxiomItem<V> get() {
return builder.get();
}
}
private final class SubstitutionItem<V> extends Item<V> {
private Lazy<? extends Item<V>> target;
public SubstitutionItem(AxiomItemDefinition definition, Lazy<? extends Item<V>> target) {
super(definition);
this.target = target;
}
@Override
public Value<V> startValue(Object value, SourceLocation loc) {
Value<V> valueCtx = super.startValue(value, loc);
target.get().addValue(valueCtx);
return valueCtx;
}
}
private final class ValueItem<V> implements ItemBuilder, Supplier<AxiomItem<V>> {
private Value<V> value;
public ValueItem(Value<V> value) {
this.value = value;
}
@Override
public AxiomName name() {
return AxiomValue.VALUE;
}
@Override
public AxiomNameResolver itemResolver() {
return resolver;
}
@Override
public AxiomNameResolver valueResolver() {
return resolver;
}
@Override
public ValueBuilder startValue(Object value, SourceLocation loc) {
this.value.setValue((V) value);
return this.value;
}
@Override
public void endNode(SourceLocation loc) {
// Noop for now
}
@Override
public AxiomItem<V> get() {
throw new UnsupportedOperationException("Should not be called");
}
}
private final class TypeItem extends Item<AxiomName> {
private Value<?> value;
public TypeItem(AxiomItemDefinition definition) {
super(definition);
}
public TypeItem(Value<?> value, AxiomItemDefinition definition) {
super(definition);
this.value = value;
}
@Override
public void endNode(SourceLocation loc) {
AxiomName typeName = (AxiomName) onlyValue().get().asComplex().get().item(AxiomTypeDefinition.NAME).get().onlyValue().value();
Optional<AxiomTypeDefinition> typeDef = context.getType(typeName);
AxiomSemanticException.check(typeDef.isPresent(), loc, "% type is not defined.", typeName);
this.value.setType(typeDef.get(),loc);
super.endNode(loc);
}
}
private final class Value<V> implements ValueBuilder, Supplier<AxiomValue<V>> {
private final AxiomValueBuilder<V> builder;
private AxiomTypeDefinition type;
public Value(V value, AxiomTypeDefinition type) {
this.type = type;
builder = AxiomValueBuilder.from(type);
if(value != null) {
setValue(value);
}
}
public void setType(AxiomTypeDefinition type, SourceLocation start) {
AxiomSemanticException.check(type.isSubtypeOf(this.type), start, "%s is not subtype of %s", type.name(), this.type.name());
this.type = type;
builder.setType(type);
}
void setValue(V value) {
if(type.argument().isPresent()) {
startItem(type.argument().get().name(), null).startValue(value, null);
} else {
builder.setValue(value);
}
}
@Override
public AxiomName name() {
return builder.type().name();
}
@Override
public AxiomNameResolver itemResolver() {
return AxiomNameResolver.defaultNamespaceFromType(builder.type());
}
@Override
public AxiomNameResolver valueResolver() {
return resolver;
}
@Override
public Optional<AxiomItemDefinition> childItemDef(AxiomName statement) {
return builder.type().itemDefinition(statement);
}
@Override
public Optional<AxiomItemDefinition> infraItemDef(AxiomName item) {
return infraType.itemDefinition(item);
}
@Override
public Item<?> startItem(AxiomName name, SourceLocation loc) {
@SuppressWarnings({ "unchecked", "rawtypes" })
Object itemImpl = builder.get(name, (id) -> {
AxiomItemDefinition childDef = childItemDef(name).get();
if(childDef.substitutionOf().isPresent()) {
Lazy<Item<?>> targetItem = Lazy.from(() -> startItem(childDef.substitutionOf().get(), loc));
// Because of JAVA 8 can not infer types correctly
return new SubstitutionItem(childDef, targetItem);
}
return new Item<>(childItemDef(name).get());
});
return (Item<?>) (itemImpl);
}
@Override
public void endValue(SourceLocation loc) {
// Noop for now
}
@Override
public AxiomValue<V> get() {
return builder.get();
}
@Override
public ItemBuilder startInfra(AxiomName name, SourceLocation loc) {
if(AxiomValue.VALUE.equals(name)) {
return new ValueItem(this);
} else if (AxiomValue.TYPE.equals(name)) {
return new TypeItem(this, infraItemDef(name).get());
}
Supplier<? extends AxiomItem<?>> itemImpl = builder.getInfra(name, (id) -> {
return new Item<>(infraItemDef(name).get());
});
return (Item) itemImpl;
}
}
}