forked from StarOpenSource/Engine
Add escape support to ShortcodeParserSkeleton
This commit is contained in:
parent
bbde2e9d2d
commit
b9d4196b8b
8 changed files with 48 additions and 18 deletions
|
@ -20,6 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.classes;
|
package de.staropensource.sosengine.base.classes;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.EngineConfiguration;
|
import de.staropensource.sosengine.base.EngineConfiguration;
|
||||||
|
import de.staropensource.sosengine.base.exceptions.ParserException;
|
||||||
import de.staropensource.sosengine.base.logging.LoggerInstance;
|
import de.staropensource.sosengine.base.logging.LoggerInstance;
|
||||||
import de.staropensource.sosengine.base.types.CodePart;
|
import de.staropensource.sosengine.base.types.CodePart;
|
||||||
import de.staropensource.sosengine.base.types.logging.LogIssuer;
|
import de.staropensource.sosengine.base.types.logging.LogIssuer;
|
||||||
|
@ -76,28 +77,51 @@ public abstract class ShortcodeParserSkeleton {
|
||||||
* Constructs this class.
|
* Constructs this class.
|
||||||
*
|
*
|
||||||
* @param string string to parse
|
* @param string string to parse
|
||||||
|
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
|
||||||
|
* @throws ParserException when parsing failed
|
||||||
* @since v1-alpha1
|
* @since v1-alpha1
|
||||||
*/
|
*/
|
||||||
public ShortcodeParserSkeleton(@NotNull String string) {
|
public ShortcodeParserSkeleton(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
|
||||||
components = parse(string);
|
components = parse(string, ignoreInvalidEscapes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an input string and spits out all of it's components.
|
* Parses an input string and spits out all of it's components.
|
||||||
*
|
*
|
||||||
* @param string string to parse
|
* @param string string to parse
|
||||||
|
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
|
||||||
* @return list of components
|
* @return list of components
|
||||||
|
* @throws ParserException when parsing failed
|
||||||
* @see EngineConfiguration#errorShortcodeConverter
|
* @see EngineConfiguration#errorShortcodeConverter
|
||||||
* @since v1-alpha1
|
* @since v1-alpha1
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
protected LinkedList<@NotNull String> parse(@NotNull String string) {
|
protected LinkedList<@NotNull String> parse(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
|
||||||
LinkedList<String> components = new LinkedList<>(); // List of components
|
LinkedList<String> components = new LinkedList<>(); // List of components
|
||||||
boolean tagActive = false; // Indicates that a tag is being parsed
|
boolean tagActive = false; // Indicates that a tag is being parsed
|
||||||
String part = ""; // Current part. May be a tag, may be regular text
|
boolean escape = false; // Indicates whether the last character was a \ character
|
||||||
|
String part = ""; // Temporary string. May be a tag, may be regular text
|
||||||
|
|
||||||
// Iterate through every character
|
// Iterate through every character
|
||||||
for (char character : string.toCharArray()) {
|
for (char character : string.toCharArray()) {
|
||||||
|
// Escaping
|
||||||
|
if (escape) {
|
||||||
|
if (character == '\\' || character == '<')
|
||||||
|
part += character;
|
||||||
|
else if (!(character == 'r' || character == 'n'))
|
||||||
|
if (ignoreInvalidEscapes)
|
||||||
|
part += "\\" + character;
|
||||||
|
else
|
||||||
|
throw new ParserException("Invalid escape \\" + character);
|
||||||
|
|
||||||
|
escape = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (character == '\\') {
|
||||||
|
escape = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (tagActive) {
|
if (tagActive) {
|
||||||
// A tag is being parsed
|
// A tag is being parsed
|
||||||
if (character == '>') {
|
if (character == '>') {
|
||||||
|
|
|
@ -26,7 +26,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
*
|
*
|
||||||
* @since v1-alpha2
|
* @since v1-alpha2
|
||||||
*/
|
*/
|
||||||
public class ParserException extends Exception {
|
public class ParserException extends RuntimeException {
|
||||||
public ParserException(@NotNull String message) {
|
public ParserException(@NotNull String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ public final class IssuerInfo implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
public String replace(@NotNull String text) {
|
||||||
String replacement = "<none>";
|
String replacement = "\\<none>";
|
||||||
|
|
||||||
if (logIssuer.getAdditionalInformation() != null)
|
if (logIssuer.getAdditionalInformation() != null)
|
||||||
replacement = logIssuer.getAdditionalInformation();
|
replacement = logIssuer.getAdditionalInformation();
|
||||||
|
|
|
@ -75,14 +75,14 @@ public final class CrashHandler {
|
||||||
sos!engine crash
|
sos!engine crash
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you <3
|
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you \\<3
|
||||||
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
|
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
|
||||||
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you <3
|
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you \\<3
|
||||||
%content%
|
%content%
|
||||||
|
|
||||||
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you <3
|
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you \\<3
|
||||||
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
|
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
|
||||||
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you <3
|
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you \\<3
|
||||||
|
|
||||||
------------------------
|
------------------------
|
||||||
sos!engine crash
|
sos!engine crash
|
||||||
|
|
|
@ -66,7 +66,7 @@ public class ColoredLoggerImpl implements LoggerImpl {
|
||||||
@Override
|
@Override
|
||||||
public void print(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String format) {
|
public void print(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String format) {
|
||||||
// Convert to Ansi
|
// Convert to Ansi
|
||||||
Ansi output = new AnsiShortcodeConverter(format).getAnsi();
|
Ansi output = new AnsiShortcodeConverter(format, true).getAnsi();
|
||||||
|
|
||||||
// Print message
|
// Print message
|
||||||
if (level == LogLevel.ERROR || level == LogLevel.CRASH)
|
if (level == LogLevel.ERROR || level == LogLevel.CRASH)
|
||||||
|
|
|
@ -62,7 +62,7 @@ public class PlainLoggerImpl implements LoggerImpl {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public void print(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String format) {
|
public void print(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String format) {
|
||||||
format = new EmptyShortcodeConverter(format).getClean();
|
format = new EmptyShortcodeConverter(format, true).getClean();
|
||||||
if (level == LogLevel.ERROR || level == LogLevel.CRASH)
|
if (level == LogLevel.ERROR || level == LogLevel.CRASH)
|
||||||
if (EngineConfiguration.getInstance().isLoggerForceStandardOutput())
|
if (EngineConfiguration.getInstance().isLoggerForceStandardOutput())
|
||||||
System.out.println(format);
|
System.out.println(format);
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.utility.converter;
|
package de.staropensource.sosengine.base.utility.converter;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.ShortcodeParserSkeleton;
|
import de.staropensource.sosengine.base.classes.ShortcodeParserSkeleton;
|
||||||
|
import de.staropensource.sosengine.base.exceptions.ParserException;
|
||||||
import org.fusesource.jansi.Ansi;
|
import org.fusesource.jansi.Ansi;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
@ -35,10 +36,12 @@ public final class AnsiShortcodeConverter extends ShortcodeParserSkeleton {
|
||||||
* Constructs this class.
|
* Constructs this class.
|
||||||
*
|
*
|
||||||
* @param string string to convert
|
* @param string string to convert
|
||||||
|
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
|
||||||
|
* @throws ParserException when parsing failed
|
||||||
* @since v1-alpha0
|
* @since v1-alpha0
|
||||||
*/
|
*/
|
||||||
public AnsiShortcodeConverter(@NotNull String string) {
|
public AnsiShortcodeConverter(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
|
||||||
super(string);
|
super(string, ignoreInvalidEscapes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.utility.converter;
|
package de.staropensource.sosengine.base.utility.converter;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.ShortcodeParserSkeleton;
|
import de.staropensource.sosengine.base.classes.ShortcodeParserSkeleton;
|
||||||
|
import de.staropensource.sosengine.base.exceptions.ParserException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,10 +35,12 @@ public final class EmptyShortcodeConverter extends ShortcodeParserSkeleton {
|
||||||
* Constructs this class.
|
* Constructs this class.
|
||||||
*
|
*
|
||||||
* @param string string to convert
|
* @param string string to convert
|
||||||
|
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
|
||||||
|
* @throws ParserException when parsing failed
|
||||||
* @since v1-alpha1
|
* @since v1-alpha1
|
||||||
*/
|
*/
|
||||||
public EmptyShortcodeConverter(@NotNull String string) {
|
public EmptyShortcodeConverter(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
|
||||||
super(string);
|
super(string, ignoreInvalidEscapes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue