-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectiveReader.java
75 lines (63 loc) · 3.02 KB
/
DirectiveReader.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
package io.github.chrisruffalo.tome.core.directive;
import io.github.chrisruffalo.tome.core.reader.TransformException;
import io.github.chrisruffalo.tome.core.reader.TransformingReader;
import io.github.chrisruffalo.tome.core.token.DefaultHandler;
import io.github.chrisruffalo.tome.core.token.Handler;
import io.github.chrisruffalo.tome.core.token.Token;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Collectors;
public class DirectiveReader extends TransformingReader<DirectiveContext> {
private final Handler tokenHandler;
private DirectiveContext context;
public DirectiveReader(final Reader input, final DirectiveContext currentContext) {
this(input, currentContext.getDirectiveConfiguration());
this.context = currentContext;
}
public DirectiveReader(final Reader input, final DirectiveConfiguration configuration) {
super(input);
this.context = new DirectiveContext(configuration);
// configure context from configuration (if available) like adding roots
if (configuration != null) {
// set paths from configuration
if (configuration.getRootPaths() != null) {
context.setRoots(configuration.getRootPaths().stream().map(Paths::get).collect(Collectors.toList()));
}
}
String startToken = DirectiveConfiguration.DEFAULT_DIRECTIVE_START_TOKEN;
String endToken = DirectiveConfiguration.DEFAULT_DIRECTIVE_END_TOKEN;
if (configuration != null) {
if (configuration.getDirectiveStartToken() != null && !configuration.getDirectiveStartToken().isEmpty()) {
startToken = configuration.getDirectiveStartToken();
}
if (configuration.getDirectiveEndToken() != null && !configuration.getDirectiveEndToken().isEmpty()) {
endToken = configuration.getDirectiveEndToken();
}
}
this.tokenHandler = new DefaultHandler(startToken, endToken, ' ');
}
@Override
protected String transform(String line, DirectiveContext context) throws IOException, TransformException {
// parse out tokens
if(this.tokenHandler.containsToken(line)) {
for (final Token token : this.tokenHandler.find(line)) {
// get each part
final String firstPart = token.getParts().get(0).getText();
final Directive directive = DirectiveFactory.get(firstPart);
if (directive != null) {
final Optional<String> transformResult = directive.transform(token, context);
if (transformResult.isPresent()) {
line = line.replace(token.getFullText(), transformResult.get());
}
}
}
}
return line;
}
@Override
protected DirectiveContext getCurrentContext() {
return this.context;
}
}