fix: handle null result in TranslatingTransformer
All checks were successful
ci/woodpecker/push/jfmod Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2024-05-10 14:28:40 +02:00
parent 6ede586866
commit e2d5de3c62
Signed by: Johannes
GPG Key ID: E76429612C2929F4

View File

@ -8,10 +8,13 @@ import io.gitlab.jfronny.translater.Translater;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.util.function.Function.identity;
public class TranslatingTransformer<T extends Language> implements Transformer {
public TranslatingTransformer(TranslateService<T> ts) {
rnd = new Random();
@ -37,10 +40,10 @@ public class TranslatingTransformer<T extends Language> implements Transformer {
for (String s : str.split("%")) {
if (!f) {
res.append("%");
if (!s.isEmpty())
res.append(s.charAt(0)).append(transform(s.substring(1)).replace("%", ""));
} else
res.append(transform(s));
if (!s.isEmpty()) {
appendTransformed(res.append(s.charAt(0)), s.substring(1), x -> x.replace("%", ""));
}
} else appendTransformed(res, s, identity());
f = false;
}
return res.toString();
@ -48,10 +51,8 @@ public class TranslatingTransformer<T extends Language> implements Transformer {
StringBuilder res = new StringBuilder();
boolean f = true;
for (String s : str.split("\\$")) {
if (!f) {
res.append("$").append(s.charAt(0)).append(transform(s.substring(1)).replace("$", ""));
} else
res.append(transform(s));
if (!f) appendTransformed(res.append("$").append(s.charAt(0)), s.substring(1), x -> x.replace("$", ""));
else appendTransformed(res, s, identity());
f = false;
}
return res.toString();
@ -59,10 +60,8 @@ public class TranslatingTransformer<T extends Language> implements Transformer {
StringBuilder res = new StringBuilder();
boolean f = true;
for (String s : str.split("§")) {
if (!f) {
res.append("§").append(s.charAt(0)).append(transform(s.substring(1)).replace("§", ""));
} else
res.append(transform(s));
if (!f) appendTransformed(res.append("§").append(s.charAt(0)), s.substring(1), x -> x.replace("§", ""));
else appendTransformed(res, s, identity());
f = false;
}
return res.toString();
@ -75,6 +74,12 @@ public class TranslatingTransformer<T extends Language> implements Transformer {
}
}
private StringBuilder appendTransformed(StringBuilder sb, String transformable, Function<String, String> fix) {
if (transformable.isEmpty()) return sb;
String transformed = transform(transformable);
return sb.append(transformed == null ? transformable : fix.apply(transformed));
}
@Override
public void transformMultiple(Stream<? extends String> strings, ResultConsumer results) {
strings.flatMap(s -> {