use of com.easterlyn.util.text.QuoteConsumer in project Easterlyn by Easterlyn.
the class StringUtil method toJSON.
// TODO appears to not be parsing colors correctly
public static Collection<TextComponent> toJSON(@Nullable String message, Collection<QuoteConsumer> additionalHandlers) {
if (message == null) {
return Collections.emptyList();
}
ParsedText parsedText = new ParsedText();
StringBuilder builder = new StringBuilder();
Collection<QuoteConsumer> consumers = Stream.concat(QUOTE_CONSUMERS.stream(), additionalHandlers.stream()).collect(Collectors.toSet());
int maxIndex = message.length() - 1;
nextChar: for (int i = 0; i < message.length(); ++i) {
char c = message.charAt(i);
BlockQuoteMatcher matcher = BLOCK_QUOTES.get(c);
// noinspection ConstantConditions
do {
if (matcher == null) {
break;
}
BlockQuote quote = matcher.findQuote(message, i);
if (quote == null) {
break;
}
i += quote.getQuoteLength() - 1;
if (!matcher.allowAdditionalParsing()) {
if (quote.getQuoteMarks() != null) {
builder.append(quote.getQuoteMarks()).append(quote.getQuoteText()).append(quote.getQuoteMarks());
} else {
builder.append(quote.getQuoteText());
}
continue nextChar;
}
if (quote.getQuoteMarks() != null) {
builder.append(quote.getQuoteMarks());
}
String quoteText = quote.getQuoteText();
consumeQuote(parsedText, consumers, builder, quote.getQuoteText());
if (quote.getQuoteMarks() != null) {
builder.append(quote.getQuoteMarks());
}
continue nextChar;
} while (false);
if (c == ' ') {
builder.append(c);
continue;
}
int nextSpace = message.indexOf(' ', i);
if (nextSpace == -1) {
nextSpace = message.length();
}
consumeQuote(parsedText, consumers, builder, message.substring(i, nextSpace));
i = nextSpace - 1;
}
// Parse remaining text in builder
consumeQuote(parsedText, consumers, builder, null);
// The client will crash if the array is empty
if (parsedText.getComponents().isEmpty()) {
parsedText.addComponent(new TextComponent(""));
}
return parsedText.getComponents();
}
use of com.easterlyn.util.text.QuoteConsumer in project Easterlyn by Easterlyn.
the class StringUtil method consumeQuote.
private static void consumeQuote(@NotNull ParsedText parsedText, @NotNull Collection<QuoteConsumer> consumers, @NotNull StringBuilder builder, @Nullable String quote) {
if (quote == null) {
if (builder.length() == 0) {
return;
}
parsedText.addText(builder.toString());
if (builder.length() > 0) {
builder.delete(0, builder.length());
}
return;
}
for (QuoteConsumer consumer : consumers) {
Supplier<Matcher> matcher = consumer.handleQuote(quote);
if (matcher == null) {
continue;
}
if (builder.length() > 0) {
parsedText.addText(builder.toString());
builder.delete(0, builder.length());
}
consumer.addComponents(parsedText, matcher);
return;
}
builder.append(quote);
}
Aggregations