-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreprocessor.java
More file actions
107 lines (85 loc) · 4.04 KB
/
Preprocessor.java
File metadata and controls
107 lines (85 loc) · 4.04 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
package blue.language.preprocess;
import blue.language.NodeProvider;
import blue.language.model.Node;
import blue.language.preprocess.processor.InferBasicTypesForUntypedValues;
import blue.language.preprocess.processor.ReplaceInlineValuesForTypeAttributesWithImports;
import blue.language.provider.BootstrapProvider;
import blue.language.utils.NodeExtender;
import blue.language.utils.NodeProviderWrapper;
import blue.language.utils.limits.PathLimits;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import static blue.language.utils.UncheckedObjectMapper.YAML_MAPPER;
public class Preprocessor {
public static final String DEFAULT_BLUE_BLUE_ID = "6sqUywMoBRyj9hgQxSu2nDPnqcyiSM7xu9AB9sN98YJK";
private TransformationProcessorProvider processorProvider;
private NodeProvider nodeProvider;
private Node defaultSimpleBlue;
public Preprocessor(TransformationProcessorProvider processorProvider, NodeProvider nodeProvider) {
this.processorProvider = processorProvider;
this.nodeProvider = NodeProviderWrapper.wrap(nodeProvider);
loadDefaultSimpleBlue();
}
public Preprocessor(NodeProvider nodeProvider) {
this(getStandardProvider(), nodeProvider);
}
public Preprocessor() {
this(BootstrapProvider.INSTANCE);
}
public Node preprocess(Node document) {
return preprocess(document, null);
}
public Node preprocessWithDefaultBlue(Node document) {
return preprocess(document, defaultSimpleBlue);
}
public Node preprocess(Node document, Node defaultBlue) {
Node processedDocument = document.clone();
Node blueNode = processedDocument.getBlue();
if (blueNode == null) {
blueNode = defaultBlue.clone();
}
if (blueNode != null) {
new NodeExtender(nodeProvider).extend(blueNode, PathLimits.withSinglePath("/*"));
if (blueNode.getItems() != null) {
List<Node> transformations = blueNode.getItems();
for (Node transformation : transformations) {
Optional<TransformationProcessor> processor = processorProvider.getProcessor(transformation);
if (processor.isPresent()) {
processedDocument = processor.get().process(processedDocument);
} else {
throw new IllegalArgumentException("No processor found for transformation: " + transformation);
}
}
processedDocument.blue(null);
}
}
return processedDocument;
}
public static TransformationProcessorProvider getStandardProvider() {
return new TransformationProcessorProvider() {
private static final String REPLACE_INLINE_TYPES = "27B7fuxQCS1VAptiCPc2RMkKoutP5qxkh3uDxZ7dr6Eo";
private static final String INFER_BASIC_TYPES = "FGYuTXwaoSKfZmpTysLTLsb8WzSqf43384rKZDkXhxD4";
@Override
public Optional<TransformationProcessor> getProcessor(Node transformation) {
String blueId = transformation.getAsText("/type/blueId");
if (REPLACE_INLINE_TYPES.equals(blueId))
return Optional.of(new ReplaceInlineValuesForTypeAttributesWithImports(transformation));
else if (INFER_BASIC_TYPES.equals(blueId))
return Optional.of(new InferBasicTypesForUntypedValues());
return Optional.empty();
}
};
}
private void loadDefaultSimpleBlue() {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("transformation/DefaultBlue.blue")) {
if (inputStream == null) {
throw new RuntimeException("Unable to find DefaultBlue.blue in classpath");
}
this.defaultSimpleBlue = YAML_MAPPER.readValue(inputStream, Node.class);
} catch (IOException e) {
throw new RuntimeException("Error loading DefaultBlue.blue from classpath", e);
}
}
}