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 org.zwobble.mammoth.internal.styles.parsing;
import org.zwobble.mammoth.internal.styles.HtmlPath;
import org.zwobble.mammoth.internal.styles.StyleMap;
import org.zwobble.mammoth.internal.styles.StyleMapBuilder;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static java.util.Arrays.asList;
public class StyleMapParser {
public static StyleMap parse(String input) {
return parseStyleMappings(asList(input.split("\\r?\\n")));
}
public static StyleMap parseStyleMappings(List<String> lines) {
StyleMapBuilder styleMap = StyleMap.builder();
for (int lineIndex = 0; lineIndex < lines.size(); lineIndex += 1) {
String line = lines.get(lineIndex);
try {
handleLine(styleMap, line);
} catch (LineParseException exception) {
throw new ParseException(generateErrorMessage(line, lineIndex + 1, exception.getCharacterIndex(), exception.getMessage()));
}
}
return styleMap.build();
}
private static void handleLine(StyleMapBuilder styleMap, String line) {
if (line.startsWith("#")) {
return;
}
line = line.trim();
if (line.isEmpty()) {
return;
}
parseStyleMapping(line).accept(styleMap);
}
private static Consumer<StyleMapBuilder> parseStyleMapping(String line) {
TokenIterator<TokenType> tokens = StyleMappingTokeniser.tokenise(line);
BiConsumer<StyleMapBuilder, HtmlPath> documentMatcher = DocumentMatcherParser.parse(tokens);
tokens.skip(TokenType.WHITESPACE);
tokens.skip(TokenType.SYMBOL, "=>");
HtmlPath htmlPath = parseHtmlPath(tokens);
tokens.skip(TokenType.EOF);
return styleMap -> documentMatcher.accept(styleMap, htmlPath);
}
private static HtmlPath parseHtmlPath(TokenIterator<TokenType> tokens) {
if (tokens.peekTokenType() == TokenType.EOF) {
return HtmlPath.EMPTY;
} else {
tokens.skip(TokenType.WHITESPACE);
return HtmlPathParser.parse(tokens);
}
}
private static String generateErrorMessage(String line, int lineNumber, int characterIndex, String message) {
return "error reading style map at line " + lineNumber + ", character " + (characterIndex + 1) +
": " + message + "\n\n" +
line + "\n" +
repeatString(" ", characterIndex) + "^";
}
private static String repeatString(String value, int times) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < times; i += 1) {
builder.append(value);
}
return builder.toString();
}
}
|