| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.parser.css.impl; |
| 23 |
|
| 24 |
import java.text.MessageFormat; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.Collections; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Objects; |
| 29 |
|
| 30 |
import org.jetbrains.annotations.NotNull; |
| 31 |
|
| 32 |
import com.github.weisj.jsvg.logging.Logger; |
| 33 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 34 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 35 |
import com.github.weisj.jsvg.parser.css.CssParser; |
| 36 |
import com.github.weisj.jsvg.parser.css.StyleProperty; |
| 37 |
|
| 38 |
public final class SimpleCssParser implements CssParser { |
| 39 |
|
| 40 |
private static final Logger LOGGER = LogFactory.createLogger(SimpleCssParser.class); |
| 41 |
|
| 42 |
public enum Mode { |
| 43 |
STYLE_SHEET, |
| 44 |
SINGLE_RULE |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public @NotNull SimpleStyleSheet parse(@NotNull List<char[]> input) { |
| 49 |
return new Parser(input, Mode.STYLE_SHEET).parse(); |
| 50 |
} |
| 51 |
|
| 52 |
public @NotNull List<@NotNull StyleProperty> parseRules(@NotNull List<char[]> input) { |
| 53 |
return new Parser(input, Mode.SINGLE_RULE).parseRules(); |
| 54 |
} |
| 55 |
|
| 56 |
private static final class Parser { |
| 57 |
|
| 58 |
private final @NotNull Lexer lexer; |
| 59 |
private final @NotNull SimpleStyleSheet sheet = new SimpleStyleSheet(); |
| 60 |
private final TokenType ruleListEndType; |
| 61 |
private @NotNull Token current = new Token(TokenType.START); |
| 62 |
|
| 63 |
|
| 64 |
private Parser(@NotNull List<char[]> input, Mode mode) { |
| 65 |
this.ruleListEndType = mode == Mode.SINGLE_RULE |
| 66 |
? TokenType.EOF |
| 67 |
: TokenType.CURLY_CLOSE; |
| 68 |
this.lexer = new Lexer(input, mode); |
| 69 |
} |
| 70 |
|
| 71 |
private void next() { |
| 72 |
Token next; |
| 73 |
do { |
| 74 |
next = lexer.nextToken(); |
| 75 |
} while (next.type() == TokenType.COMMENT); |
| 76 |
current = next; |
| 77 |
} |
| 78 |
|
| 79 |
private void expected(@NotNull String type) { |
| 80 |
LOGGER.log(Level.WARNING, () -> MessageFormat.format("Expected ''{0}'' but got ''{1}''", type, current)); |
| 81 |
} |
| 82 |
|
| 83 |
private void consumeOrSkipAllowedToken(TokenType type, TokenType allowedTokeToSkip) { |
| 84 |
if (current.type() != type) { |
| 85 |
if (current.type() != allowedTokeToSkip) { |
| 86 |
expected(type.toString()); |
| 87 |
throw new ParserException(); |
| 88 |
} |
| 89 |
return; |
| 90 |
} |
| 91 |
next(); |
| 92 |
} |
| 93 |
|
| 94 |
private void consume(TokenType type) { |
| 95 |
consumeOrSkipAllowedToken(type, null); |
| 96 |
} |
| 97 |
|
| 98 |
private @NotNull String consumeValue(TokenType type) { |
| 99 |
if (current.type() != type) { |
| 100 |
expected(type.toString()); |
| 101 |
throw new ParserException(); |
| 102 |
} |
| 103 |
if (current.data() == null) { |
| 104 |
throw new ParserException(); |
| 105 |
} |
| 106 |
String value = Objects.requireNonNull(current.data()); |
| 107 |
next(); |
| 108 |
return value; |
| 109 |
} |
| 110 |
|
| 111 |
private @NotNull List<Token> readIdentifierList() { |
| 112 |
List<Token> list = new ArrayList<>(); |
| 113 |
|
| 114 |
while (current.type() != TokenType.CURLY_OPEN && current.type() != TokenType.EOF) { |
| 115 |
TokenType type = current.type(); |
| 116 |
if (type != TokenType.IDENTIFIER |
| 117 |
&& type != TokenType.ID_NAME |
| 118 |
&& type != TokenType.CLASS_NAME) { |
| 119 |
expected("identifier"); |
| 120 |
throw new ParserException(); |
| 121 |
} |
| 122 |
list.add(current); |
| 123 |
next(); |
| 124 |
|
| 125 |
if (current.type() == TokenType.COMMA) { |
| 126 |
next(); |
| 127 |
} else { |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
return list; |
| 132 |
} |
| 133 |
|
| 134 |
private @NotNull List<StyleProperty> readProperties() { |
| 135 |
List<StyleProperty> list = new ArrayList<>(); |
| 136 |
|
| 137 |
while (current.type() != TokenType.CURLY_CLOSE && current.type() != TokenType.EOF) { |
| 138 |
String name = consumeValue(TokenType.IDENTIFIER); |
| 139 |
consume(TokenType.COLON); |
| 140 |
String value = consumeValue(TokenType.RAW_DATA); |
| 141 |
consumeOrSkipAllowedToken(TokenType.SEMICOLON, ruleListEndType); |
| 142 |
list.add(new StyleProperty(name, value.trim())); |
| 143 |
} |
| 144 |
|
| 145 |
return list; |
| 146 |
} |
| 147 |
|
| 148 |
private void skipToNextDefinition() { |
| 149 |
while (current.type() != TokenType.CURLY_CLOSE && current.type() != TokenType.EOF) { |
| 150 |
try { |
| 151 |
next(); |
| 152 |
} catch (ParserException ignored) { |
| 153 |
|
| 154 |
} |
| 155 |
} |
| 156 |
if (current.type() != TokenType.EOF) { |
| 157 |
current = new Token(TokenType.START); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
@NotNull |
| 162 |
List<@NotNull StyleProperty> parseRules() { |
| 163 |
try { |
| 164 |
if (current.type() == TokenType.START) { |
| 165 |
next(); |
| 166 |
} |
| 167 |
return readProperties(); |
| 168 |
} catch (ParserException e) { |
| 169 |
return Collections.emptyList(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
@NotNull |
| 174 |
SimpleStyleSheet parse() { |
| 175 |
do { |
| 176 |
try { |
| 177 |
if (current.type() == TokenType.START) { |
| 178 |
next(); |
| 179 |
} |
| 180 |
|
| 181 |
List<Token> identifierList = readIdentifierList(); |
| 182 |
|
| 183 |
consume(TokenType.CURLY_OPEN); |
| 184 |
List<StyleProperty> properties = readProperties(); |
| 185 |
consume(TokenType.CURLY_CLOSE); |
| 186 |
|
| 187 |
for (Token token : identifierList) { |
| 188 |
switch (token.type()) { |
| 189 |
case CLASS_NAME: |
| 190 |
sheet.addClassRules(Objects.requireNonNull(token.data()), properties); |
| 191 |
break; |
| 192 |
case ID_NAME: |
| 193 |
sheet.addIdRules(Objects.requireNonNull(token.data()), properties); |
| 194 |
break; |
| 195 |
case IDENTIFIER: |
| 196 |
sheet.addTagNameRules(Objects.requireNonNull(token.data()), properties); |
| 197 |
break; |
| 198 |
default: |
| 199 |
throw new IllegalStateException("Toke = " + token); |
| 200 |
} |
| 201 |
} |
| 202 |
} catch (ParserException e) { |
| 203 |
skipToNextDefinition(); |
| 204 |
} |
| 205 |
} while (current.type() != TokenType.EOF); |
| 206 |
return sheet; |
| 207 |
} |
| 208 |
} |
| 209 |
} |