mirror of
https://github.com/plexusorg/Plex.git
synced 2025-01-10 09:17:36 +00:00
Format
This commit is contained in:
parent
770fe65f98
commit
bb8c34e0cd
.github/workflows
proxy/src/main/java/dev/plex
command
config
handlers
listener
settings
toml
ArrayValueReader.javaArrayValueWriter.javaBooleanValueReaderWriter.javaContainer.javaContext.javaDatePolicy.javaDateValueReaderWriter.javaIdentifier.javaIdentifierConverter.javaIndentationPolicy.javaInlineTableValueReader.javaKeys.javaLiteralStringValueReader.javaMapValueWriter.javaMultilineLiteralStringValueReader.javaMultilineStringValueReader.javaNumberValueReaderWriter.javaObjectValueWriter.javaPrimitiveArrayValueWriter.javaResults.javaStringValueReaderWriter.javaTableArrayValueWriter.javaToml.javaTomlParser.javaTomlWriter.javaValueReader.javaValueReaders.javaValueWriter.javaValueWriters.javaWriterContext.java
util
server/src/main/java/dev/plex
Plex.java
cache
command
PlexCommand.java
impl
listener/impl
menu
module
player
punishment
rank/enums
services
storage
util
64
.github/workflows/codeql-analysis.yml
vendored
64
.github/workflows/codeql-analysis.yml
vendored
@ -37,41 +37,41 @@ jobs:
|
||||
# Learn more about CodeQL language support at https://git.io/codeql-language-support
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 17
|
||||
cache: gradle
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 17
|
||||
cache: gradle
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
||||
- run: |
|
||||
chmod +x gradlew
|
||||
./gradlew build --no-daemon
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
||||
- run: |
|
||||
chmod +x gradlew
|
||||
./gradlew build --no-daemon
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
||||
|
@ -9,13 +9,12 @@ import dev.plex.Plex;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import java.util.Arrays;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public abstract class PlexCommand implements SimpleCommand
|
||||
{
|
||||
/**
|
||||
@ -81,7 +80,7 @@ public abstract class PlexCommand implements SimpleCommand
|
||||
return;
|
||||
}
|
||||
}
|
||||
Component component = this.execute(invocation.source(), invocation.source() instanceof ConsoleCommandSource ? null : (Player) invocation.source(), invocation.arguments());
|
||||
Component component = this.execute(invocation.source(), invocation.source() instanceof ConsoleCommandSource ? null : (Player)invocation.source(), invocation.arguments());
|
||||
if (component != null)
|
||||
{
|
||||
send(invocation.source(), component);
|
||||
|
@ -3,13 +3,12 @@ package dev.plex.config;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.toml.Toml;
|
||||
import dev.plex.toml.TomlWriter;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.function.Consumer;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
public class TomlConfig
|
||||
@ -43,7 +42,8 @@ public class TomlConfig
|
||||
{
|
||||
this.onCreate.accept(this.toml);
|
||||
}
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -57,7 +57,8 @@ public class TomlConfig
|
||||
{
|
||||
this.onCreate.accept(this.toml);
|
||||
}
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -89,7 +90,8 @@ public class TomlConfig
|
||||
{
|
||||
writer.write(object, this.file);
|
||||
this.load();
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
|
||||
import dev.plex.listener.PlexListener;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.ReflectionsUtil;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
@ -5,6 +5,7 @@ import dev.plex.Plex;
|
||||
public class PlexListener
|
||||
{
|
||||
protected final Plex plugin = Plex.get();
|
||||
|
||||
public PlexListener()
|
||||
{
|
||||
Plex.get().getServer().getEventManager().register(Plex.get(), this);
|
||||
|
@ -2,18 +2,18 @@ package dev.plex.settings;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public class ServerSettings
|
||||
{
|
||||
private final Server server = new Server();
|
||||
|
||||
@Data
|
||||
public static class Server {
|
||||
public static class Server
|
||||
{
|
||||
private String name = "Server";
|
||||
private List<String> motd = Lists.newArrayList("%randomgradient%%servername% - %mcversion%", "Another motd");
|
||||
private boolean colorizeMotd = false;
|
||||
|
@ -4,74 +4,105 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class ArrayValueReader implements ValueReader {
|
||||
public class ArrayValueReader implements ValueReader
|
||||
{
|
||||
|
||||
public static final ArrayValueReader ARRAY_VALUE_READER = new ArrayValueReader();
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
return s.startsWith("[");
|
||||
}
|
||||
public static final ArrayValueReader ARRAY_VALUE_READER = new ArrayValueReader();
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context) {
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int startIndex = index.get();
|
||||
List<Object> arrayItems = new ArrayList<Object>();
|
||||
boolean terminated = false;
|
||||
boolean inComment = false;
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
|
||||
for (int i = index.incrementAndGet(); i < s.length(); i = index.incrementAndGet()) {
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("[");
|
||||
}
|
||||
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '#' && !inComment) {
|
||||
inComment = true;
|
||||
} else if (c == '\n') {
|
||||
inComment = false;
|
||||
line.incrementAndGet();
|
||||
} else if (inComment || Character.isWhitespace(c) || c == ',') {
|
||||
continue;
|
||||
} else if (c == '[') {
|
||||
Object converted = read(s, index, context);
|
||||
if (converted instanceof dev.plex.toml.Results.Errors) {
|
||||
errors.add((dev.plex.toml.Results.Errors) converted);
|
||||
} else if (!isHomogenousArray(converted, arrayItems)) {
|
||||
errors.heterogenous(context.identifier.getName(), line.get());
|
||||
} else {
|
||||
arrayItems.add(converted);
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int startIndex = index.get();
|
||||
List<Object> arrayItems = new ArrayList<Object>();
|
||||
boolean terminated = false;
|
||||
boolean inComment = false;
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
|
||||
for (int i = index.incrementAndGet(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '#' && !inComment)
|
||||
{
|
||||
inComment = true;
|
||||
}
|
||||
else if (c == '\n')
|
||||
{
|
||||
inComment = false;
|
||||
line.incrementAndGet();
|
||||
}
|
||||
else if (inComment || Character.isWhitespace(c) || c == ',')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (c == '[')
|
||||
{
|
||||
Object converted = read(s, index, context);
|
||||
if (converted instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
errors.add((dev.plex.toml.Results.Errors)converted);
|
||||
}
|
||||
else if (!isHomogenousArray(converted, arrayItems))
|
||||
{
|
||||
errors.heterogenous(context.identifier.getName(), line.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
arrayItems.add(converted);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else if (c == ']')
|
||||
{
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
Object converted = ValueReaders.VALUE_READERS.convert(s, index, context);
|
||||
if (converted instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
errors.add((dev.plex.toml.Results.Errors)converted);
|
||||
}
|
||||
else if (!isHomogenousArray(converted, arrayItems))
|
||||
{
|
||||
errors.heterogenous(context.identifier.getName(), line.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
arrayItems.add(converted);
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
} else if (c == ']') {
|
||||
terminated = true;
|
||||
break;
|
||||
} else {
|
||||
Object converted = ValueReaders.VALUE_READERS.convert(s, index, context);
|
||||
if (converted instanceof dev.plex.toml.Results.Errors) {
|
||||
errors.add((dev.plex.toml.Results.Errors) converted);
|
||||
} else if (!isHomogenousArray(converted, arrayItems)) {
|
||||
errors.heterogenous(context.identifier.getName(), line.get());
|
||||
} else {
|
||||
arrayItems.add(converted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!terminated) {
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex, s.length()), startLine);
|
||||
}
|
||||
|
||||
if (errors.hasErrors()) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
return arrayItems;
|
||||
}
|
||||
|
||||
private boolean isHomogenousArray(Object o, List<?> values) {
|
||||
return values.isEmpty() || values.get(0).getClass().isAssignableFrom(o.getClass()) || o.getClass().isAssignableFrom(values.get(0).getClass());
|
||||
}
|
||||
|
||||
private ArrayValueReader() {}
|
||||
if (!terminated)
|
||||
{
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex), startLine);
|
||||
}
|
||||
|
||||
if (errors.hasErrors())
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
return arrayItems;
|
||||
}
|
||||
|
||||
private boolean isHomogenousArray(Object o, List<?> values)
|
||||
{
|
||||
return values.isEmpty() || values.get(0).getClass().isAssignableFrom(o.getClass()) || o.getClass().isAssignableFrom(values.get(0).getClass());
|
||||
}
|
||||
|
||||
private ArrayValueReader()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -4,63 +4,79 @@ package dev.plex.toml;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static dev.plex.toml.ValueWriters.WRITERS;
|
||||
|
||||
public abstract class ArrayValueWriter implements dev.plex.toml.ValueWriter
|
||||
{
|
||||
static protected boolean isArrayish(Object value) {
|
||||
return value instanceof Collection || value.getClass().isArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean isArrayOfPrimitive(Object array) {
|
||||
Object first = peek(array);
|
||||
if (first != null) {
|
||||
dev.plex.toml.ValueWriter valueWriter = WRITERS.findWriterFor(first);
|
||||
return valueWriter.isPrimitiveType() || isArrayish(first);
|
||||
static protected boolean isArrayish(Object value)
|
||||
{
|
||||
return value instanceof Collection || value.getClass().isArray();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Collection<?> normalize(Object value) {
|
||||
Collection<Object> collection;
|
||||
|
||||
if (value.getClass().isArray()) {
|
||||
// Arrays.asList() interprets an array as a single element,
|
||||
// so convert it to a list by hand
|
||||
collection = new ArrayList<Object>(Array.getLength(value));
|
||||
for (int i = 0; i < Array.getLength(value); i++) {
|
||||
Object elem = Array.get(value, i);
|
||||
collection.add(elem);
|
||||
}
|
||||
} else {
|
||||
collection = (Collection<Object>) value;
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
static boolean isArrayOfPrimitive(Object array)
|
||||
{
|
||||
Object first = peek(array);
|
||||
if (first != null)
|
||||
{
|
||||
dev.plex.toml.ValueWriter valueWriter = WRITERS.findWriterFor(first);
|
||||
return valueWriter.isPrimitiveType() || isArrayish(first);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Collection<?> normalize(Object value)
|
||||
{
|
||||
Collection<Object> collection;
|
||||
|
||||
if (value.getClass().isArray())
|
||||
{
|
||||
// Arrays.asList() interprets an array as a single element,
|
||||
// so convert it to a list by hand
|
||||
collection = new ArrayList<Object>(Array.getLength(value));
|
||||
for (int i = 0; i < Array.getLength(value); i++)
|
||||
{
|
||||
Object elem = Array.get(value, i);
|
||||
collection.add(elem);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
collection = (Collection<Object>)value;
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
private static Object peek(Object value)
|
||||
{
|
||||
if (value.getClass().isArray())
|
||||
{
|
||||
if (Array.getLength(value) > 0)
|
||||
{
|
||||
return Array.get(value, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Collection<?> collection = (Collection<?>)value;
|
||||
if (collection.size() > 0)
|
||||
{
|
||||
return collection.iterator().next();
|
||||
}
|
||||
}
|
||||
|
||||
private static Object peek(Object value) {
|
||||
if (value.getClass().isArray()) {
|
||||
if (Array.getLength(value) > 0) {
|
||||
return Array.get(value, 0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
Collection<?> collection = (Collection<?>) value;
|
||||
if (collection.size() > 0) {
|
||||
return collection.iterator().next();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -3,46 +3,55 @@ package dev.plex.toml;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
||||
class BooleanValueReaderWriter implements ValueReader, ValueWriter {
|
||||
|
||||
static final BooleanValueReaderWriter BOOLEAN_VALUE_READER_WRITER = new BooleanValueReaderWriter();
|
||||
class BooleanValueReaderWriter implements ValueReader, ValueWriter
|
||||
{
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
return s.startsWith("true") || s.startsWith("false");
|
||||
}
|
||||
static final BooleanValueReaderWriter BOOLEAN_VALUE_READER_WRITER = new BooleanValueReaderWriter();
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context) {
|
||||
s = s.substring(index.get());
|
||||
Boolean b = s.startsWith("true") ? Boolean.TRUE : Boolean.FALSE;
|
||||
|
||||
int endIndex = b == Boolean.TRUE ? 4 : 5;
|
||||
|
||||
index.addAndGet(endIndex - 1);
|
||||
|
||||
return b;
|
||||
}
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("true") || s.startsWith("false");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value) {
|
||||
return Boolean.class.isInstance(value);
|
||||
}
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
s = s.substring(index.get());
|
||||
Boolean b = s.startsWith("true") ? Boolean.TRUE : Boolean.FALSE;
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context) {
|
||||
context.write(value.toString());
|
||||
}
|
||||
int endIndex = b == Boolean.TRUE ? 4 : 5;
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return true;
|
||||
}
|
||||
index.addAndGet(endIndex - 1);
|
||||
|
||||
private BooleanValueReaderWriter() {}
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "boolean";
|
||||
}
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof Boolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
context.write(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private BooleanValueReaderWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "boolean";
|
||||
}
|
||||
}
|
||||
|
@ -5,118 +5,148 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Container {
|
||||
public abstract class Container
|
||||
{
|
||||
|
||||
abstract boolean accepts(String key);
|
||||
abstract void put(String key, Object value);
|
||||
abstract Object get(String key);
|
||||
abstract boolean isImplicit();
|
||||
abstract boolean accepts(String key);
|
||||
|
||||
static class Table extends Container {
|
||||
private final Map<String, Object> values = new HashMap<String, Object>();
|
||||
final String name;
|
||||
final boolean implicit;
|
||||
abstract void put(String key, Object value);
|
||||
|
||||
Table() {
|
||||
this(null, false);
|
||||
}
|
||||
|
||||
public Table(String name) {
|
||||
this(name, false);
|
||||
}
|
||||
abstract Object get(String key);
|
||||
|
||||
public Table(String tableName, boolean implicit) {
|
||||
this.name = tableName;
|
||||
this.implicit = implicit;
|
||||
}
|
||||
abstract boolean isImplicit();
|
||||
|
||||
@Override
|
||||
boolean accepts(String key) {
|
||||
return !values.containsKey(key) || values.get(key) instanceof TableArray;
|
||||
}
|
||||
static class Table extends Container
|
||||
{
|
||||
private final Map<String, Object> values = new HashMap<String, Object>();
|
||||
final String name;
|
||||
final boolean implicit;
|
||||
|
||||
@Override
|
||||
void put(String key, Object value) {
|
||||
values.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
Object get(String key) {
|
||||
return values.get(key);
|
||||
}
|
||||
|
||||
boolean isImplicit() {
|
||||
return implicit;
|
||||
}
|
||||
|
||||
/**
|
||||
* This modifies the Table's internal data structure, such that it is no longer usable.
|
||||
*
|
||||
* Therefore, this method must only be called when all data has been gathered.
|
||||
|
||||
* @return A Map-and-List-based of the TOML data
|
||||
*/
|
||||
Map<String, Object> consume() {
|
||||
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
||||
if (entry.getValue() instanceof Table) {
|
||||
entry.setValue(((Table) entry.getValue()).consume());
|
||||
} else if (entry.getValue() instanceof TableArray) {
|
||||
entry.setValue(((TableArray) entry.getValue()).getValues());
|
||||
Table()
|
||||
{
|
||||
this(null, false);
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
public Table(String name)
|
||||
{
|
||||
this(name, false);
|
||||
}
|
||||
|
||||
public Table(String tableName, boolean implicit)
|
||||
{
|
||||
this.name = tableName;
|
||||
this.implicit = implicit;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean accepts(String key)
|
||||
{
|
||||
return !values.containsKey(key) || values.get(key) instanceof TableArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
void put(String key, Object value)
|
||||
{
|
||||
values.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
Object get(String key)
|
||||
{
|
||||
return values.get(key);
|
||||
}
|
||||
|
||||
boolean isImplicit()
|
||||
{
|
||||
return implicit;
|
||||
}
|
||||
|
||||
/**
|
||||
* This modifies the Table's internal data structure, such that it is no longer usable.
|
||||
* <p>
|
||||
* Therefore, this method must only be called when all data has been gathered.
|
||||
*
|
||||
* @return A Map-and-List-based of the TOML data
|
||||
*/
|
||||
Map<String, Object> consume()
|
||||
{
|
||||
for (Map.Entry<String, Object> entry : values.entrySet())
|
||||
{
|
||||
if (entry.getValue() instanceof Table)
|
||||
{
|
||||
entry.setValue(((Table)entry.getValue()).consume());
|
||||
}
|
||||
else if (entry.getValue() instanceof TableArray)
|
||||
{
|
||||
entry.setValue(((TableArray)entry.getValue()).getValues());
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return values.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return values.toString();
|
||||
}
|
||||
}
|
||||
static class TableArray extends Container
|
||||
{
|
||||
private final List<Table> values = new ArrayList<Table>();
|
||||
|
||||
static class TableArray extends Container {
|
||||
private final List<Table> values = new ArrayList<Table>();
|
||||
TableArray()
|
||||
{
|
||||
values.add(new Table());
|
||||
}
|
||||
|
||||
TableArray() {
|
||||
values.add(new Table());
|
||||
@Override
|
||||
boolean accepts(String key)
|
||||
{
|
||||
return getCurrent().accepts(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
void put(String key, Object value)
|
||||
{
|
||||
values.add((Table)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
Object get(String key)
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
boolean isImplicit()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Map<String, Object>> getValues()
|
||||
{
|
||||
ArrayList<Map<String, Object>> unwrappedValues = new ArrayList<Map<String, Object>>();
|
||||
for (Table table : values)
|
||||
{
|
||||
unwrappedValues.add(table.consume());
|
||||
}
|
||||
return unwrappedValues;
|
||||
}
|
||||
|
||||
Table getCurrent()
|
||||
{
|
||||
return values.get(values.size() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return values.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean accepts(String key) {
|
||||
return getCurrent().accepts(key);
|
||||
private Container()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
void put(String key, Object value) {
|
||||
values.add((Table) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
Object get(String key) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
boolean isImplicit() {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Map<String, Object>> getValues() {
|
||||
ArrayList<Map<String, Object>> unwrappedValues = new ArrayList<Map<String,Object>>();
|
||||
for (Table table : values) {
|
||||
unwrappedValues.add(table.consume());
|
||||
}
|
||||
return unwrappedValues;
|
||||
}
|
||||
|
||||
Table getCurrent() {
|
||||
return values.get(values.size() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return values.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private Container() {}
|
||||
}
|
||||
|
@ -2,18 +2,21 @@ package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Context {
|
||||
final dev.plex.toml.Identifier identifier;
|
||||
final AtomicInteger line;
|
||||
final Results.Errors errors;
|
||||
|
||||
public Context(dev.plex.toml.Identifier identifier, AtomicInteger line, Results.Errors errors) {
|
||||
this.identifier = identifier;
|
||||
this.line = line;
|
||||
this.errors = errors;
|
||||
}
|
||||
public class Context
|
||||
{
|
||||
final dev.plex.toml.Identifier identifier;
|
||||
final AtomicInteger line;
|
||||
final Results.Errors errors;
|
||||
|
||||
public Context with(dev.plex.toml.Identifier identifier) {
|
||||
return new Context(identifier, line, errors);
|
||||
}
|
||||
public Context(dev.plex.toml.Identifier identifier, AtomicInteger line, Results.Errors errors)
|
||||
{
|
||||
this.identifier = identifier;
|
||||
this.line = line;
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public Context with(dev.plex.toml.Identifier identifier)
|
||||
{
|
||||
return new Context(identifier, line, errors);
|
||||
}
|
||||
}
|
||||
|
@ -2,21 +2,25 @@ package dev.plex.toml;
|
||||
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DatePolicy {
|
||||
public class DatePolicy
|
||||
{
|
||||
|
||||
private final TimeZone timeZone;
|
||||
private final boolean showFractionalSeconds;
|
||||
|
||||
DatePolicy(TimeZone timeZone, boolean showFractionalSeconds) {
|
||||
this.timeZone = timeZone;
|
||||
this.showFractionalSeconds = showFractionalSeconds;
|
||||
}
|
||||
private final TimeZone timeZone;
|
||||
private final boolean showFractionalSeconds;
|
||||
|
||||
TimeZone getTimeZone() {
|
||||
return timeZone;
|
||||
}
|
||||
DatePolicy(TimeZone timeZone, boolean showFractionalSeconds)
|
||||
{
|
||||
this.timeZone = timeZone;
|
||||
this.showFractionalSeconds = showFractionalSeconds;
|
||||
}
|
||||
|
||||
boolean isShowFractionalSeconds() {
|
||||
return showFractionalSeconds;
|
||||
}
|
||||
TimeZone getTimeZone()
|
||||
{
|
||||
return timeZone;
|
||||
}
|
||||
|
||||
boolean isShowFractionalSeconds()
|
||||
{
|
||||
return showFractionalSeconds;
|
||||
}
|
||||
}
|
||||
|
@ -7,154 +7,198 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DateValueReaderWriter implements ValueReader, ValueWriter {
|
||||
public class DateValueReaderWriter implements ValueReader, ValueWriter
|
||||
{
|
||||
|
||||
static final DateValueReaderWriter DATE_VALUE_READER_WRITER = new DateValueReaderWriter();
|
||||
static final DateValueReaderWriter DATE_PARSER_JDK_6 = new DateConverterJdk6();
|
||||
private static final Pattern DATE_REGEX = Pattern.compile("(\\d{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9])(\\.\\d*)?(Z|(?:[+\\-]\\d{2}:\\d{2}))(.*)");
|
||||
static final DateValueReaderWriter DATE_VALUE_READER_WRITER = new DateValueReaderWriter();
|
||||
static final DateValueReaderWriter DATE_PARSER_JDK_6 = new DateConverterJdk6();
|
||||
private static final Pattern DATE_REGEX = Pattern.compile("(\\d{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9])(\\.\\d*)?(Z|(?:[+\\-]\\d{2}:\\d{2}))(.*)");
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
if (s.length() < 5) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (i < 4) {
|
||||
if (!Character.isDigit(c)) {
|
||||
return false;
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
if (s.length() < 5)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} else if (c != '-') {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (i < 4)
|
||||
{
|
||||
if (!Character.isDigit(c))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (c != '-')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String original, AtomicInteger index, Context context) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = index.get(); i < original.length(); i = index.incrementAndGet()) {
|
||||
char c = original.charAt(i);
|
||||
if (Character.isDigit(c) || c == '-' || c == '+' || c == ':' || c == '.' || c == 'T' || c == 'Z') {
|
||||
sb.append(c);
|
||||
} else {
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String s = sb.toString();
|
||||
Matcher matcher = DATE_REGEX.matcher(s);
|
||||
|
||||
if (!matcher.matches()) {
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), s, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
String dateString = matcher.group(1);
|
||||
String zone = matcher.group(3);
|
||||
String fractionalSeconds = matcher.group(2);
|
||||
String format = "yyyy-MM-dd'T'HH:mm:ss";
|
||||
if (fractionalSeconds != null && !fractionalSeconds.isEmpty()) {
|
||||
format += ".SSS";
|
||||
dateString += fractionalSeconds;
|
||||
}
|
||||
format += "Z";
|
||||
if ("Z".equals(zone)) {
|
||||
dateString += "+0000";
|
||||
} else if (zone.contains(":")) {
|
||||
dateString += zone.replace(":", "");
|
||||
}
|
||||
|
||||
try {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
||||
dateFormat.setLenient(false);
|
||||
return dateFormat.parse(dateString);
|
||||
} catch (Exception e) {
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), s, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value) {
|
||||
return value instanceof Date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context) {
|
||||
DateFormat formatter = getFormatter(context.getDatePolicy());
|
||||
context.write(formatter.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private DateFormat getFormatter(DatePolicy datePolicy) {
|
||||
boolean utc = "UTC".equals(datePolicy.getTimeZone().getID());
|
||||
String format;
|
||||
|
||||
if (utc && datePolicy.isShowFractionalSeconds()) {
|
||||
format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
|
||||
} else if (utc) {
|
||||
format = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||
} else if (datePolicy.isShowFractionalSeconds()) {
|
||||
format = getTimeZoneAndFractionalSecondsFormat();
|
||||
} else {
|
||||
format = getTimeZoneFormat();
|
||||
}
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(format);
|
||||
formatter.setTimeZone(datePolicy.getTimeZone());
|
||||
|
||||
return formatter;
|
||||
}
|
||||
|
||||
String getTimeZoneFormat() {
|
||||
return "yyyy-MM-dd'T'HH:mm:ssXXX";
|
||||
}
|
||||
|
||||
String getTimeZoneAndFractionalSecondsFormat() {
|
||||
return "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
|
||||
}
|
||||
|
||||
private DateValueReaderWriter() {}
|
||||
|
||||
private static class DateConverterJdk6 extends DateValueReaderWriter {
|
||||
@Override
|
||||
public void write(Object value, WriterContext context) {
|
||||
DateFormat formatter = super.getFormatter(context.getDatePolicy());
|
||||
String date = formatter.format(value);
|
||||
|
||||
if ("UTC".equals(context.getDatePolicy().getTimeZone().getID())) {
|
||||
context.write(date);
|
||||
} else {
|
||||
int insertionIndex = date.length() - 2;
|
||||
context.write(date.substring(0, insertionIndex)).write(':').write(date.substring(insertionIndex));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
String getTimeZoneAndFractionalSecondsFormat() {
|
||||
return "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
||||
}
|
||||
|
||||
@Override
|
||||
String getTimeZoneFormat() {
|
||||
return "yyyy-MM-dd'T'HH:mm:ssZ";
|
||||
}
|
||||
}
|
||||
public Object read(String original, AtomicInteger index, Context context)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "datetime";
|
||||
}
|
||||
for (int i = index.get(); i < original.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = original.charAt(i);
|
||||
if (Character.isDigit(c) || c == '-' || c == '+' || c == ':' || c == '.' || c == 'T' || c == 'Z')
|
||||
{
|
||||
sb.append(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String s = sb.toString();
|
||||
Matcher matcher = DATE_REGEX.matcher(s);
|
||||
|
||||
if (!matcher.matches())
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), s, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
String dateString = matcher.group(1);
|
||||
String zone = matcher.group(3);
|
||||
String fractionalSeconds = matcher.group(2);
|
||||
String format = "yyyy-MM-dd'T'HH:mm:ss";
|
||||
if (fractionalSeconds != null && !fractionalSeconds.isEmpty())
|
||||
{
|
||||
format += ".SSS";
|
||||
dateString += fractionalSeconds;
|
||||
}
|
||||
format += "Z";
|
||||
if ("Z".equals(zone))
|
||||
{
|
||||
dateString += "+0000";
|
||||
}
|
||||
else if (zone.contains(":"))
|
||||
{
|
||||
dateString += zone.replace(":", "");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
||||
dateFormat.setLenient(false);
|
||||
return dateFormat.parse(dateString);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), s, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof Date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
DateFormat formatter = getFormatter(context.getDatePolicy());
|
||||
context.write(formatter.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private DateFormat getFormatter(DatePolicy datePolicy)
|
||||
{
|
||||
boolean utc = "UTC".equals(datePolicy.getTimeZone().getID());
|
||||
String format;
|
||||
|
||||
if (utc && datePolicy.isShowFractionalSeconds())
|
||||
{
|
||||
format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
|
||||
}
|
||||
else if (utc)
|
||||
{
|
||||
format = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||
}
|
||||
else if (datePolicy.isShowFractionalSeconds())
|
||||
{
|
||||
format = getTimeZoneAndFractionalSecondsFormat();
|
||||
}
|
||||
else
|
||||
{
|
||||
format = getTimeZoneFormat();
|
||||
}
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(format);
|
||||
formatter.setTimeZone(datePolicy.getTimeZone());
|
||||
|
||||
return formatter;
|
||||
}
|
||||
|
||||
String getTimeZoneFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ssXXX";
|
||||
}
|
||||
|
||||
String getTimeZoneAndFractionalSecondsFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
|
||||
}
|
||||
|
||||
private DateValueReaderWriter()
|
||||
{
|
||||
}
|
||||
|
||||
private static class DateConverterJdk6 extends DateValueReaderWriter
|
||||
{
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
DateFormat formatter = super.getFormatter(context.getDatePolicy());
|
||||
String date = formatter.format(value);
|
||||
|
||||
if ("UTC".equals(context.getDatePolicy().getTimeZone().getID()))
|
||||
{
|
||||
context.write(date);
|
||||
}
|
||||
else
|
||||
{
|
||||
int insertionIndex = date.length() - 2;
|
||||
context.write(date.substring(0, insertionIndex)).write(':').write(date.substring(insertionIndex));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
String getTimeZoneAndFractionalSecondsFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
||||
}
|
||||
|
||||
@Override
|
||||
String getTimeZoneFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ssZ";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "datetime";
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,13 @@ public class Identifier
|
||||
{
|
||||
type = Type.TABLE_ARRAY;
|
||||
valid = isValidTableArray(name, context);
|
||||
} else if (name.startsWith("["))
|
||||
}
|
||||
else if (name.startsWith("["))
|
||||
{
|
||||
type = Type.TABLE;
|
||||
valid = isValidTable(name, context);
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
type = Type.KEY;
|
||||
valid = isValidKey(name, context);
|
||||
@ -78,9 +80,9 @@ public class Identifier
|
||||
return type == Type.TABLE_ARRAY;
|
||||
}
|
||||
|
||||
private static enum Type
|
||||
private enum Type
|
||||
{
|
||||
KEY, TABLE, TABLE_ARRAY;
|
||||
KEY, TABLE, TABLE_ARRAY
|
||||
}
|
||||
|
||||
private static String extractName(String raw)
|
||||
@ -95,7 +97,8 @@ public class Identifier
|
||||
{
|
||||
quoted = !quoted;
|
||||
sb.append('"');
|
||||
} else if (quoted || !Character.isWhitespace(c))
|
||||
}
|
||||
else if (quoted || !Character.isWhitespace(c))
|
||||
{
|
||||
sb.append(c);
|
||||
}
|
||||
@ -125,7 +128,8 @@ public class Identifier
|
||||
return false;
|
||||
}
|
||||
quoted = !quoted;
|
||||
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1))
|
||||
}
|
||||
else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1))
|
||||
{
|
||||
context.errors.invalidKey(name, context.line.get());
|
||||
return false;
|
||||
@ -137,12 +141,7 @@ public class Identifier
|
||||
|
||||
private static boolean isValidTable(String name, Context context)
|
||||
{
|
||||
boolean valid = true;
|
||||
|
||||
if (!name.endsWith("]"))
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
boolean valid = name.endsWith("]");
|
||||
|
||||
String trimmed = name.substring(1, name.length() - 1).trim();
|
||||
if (trimmed.isEmpty() || trimmed.charAt(0) == '.' || trimmed.endsWith("."))
|
||||
@ -175,32 +174,38 @@ public class Identifier
|
||||
if (!quoteAllowed)
|
||||
{
|
||||
valid = false;
|
||||
} else if (quoted && trimmed.charAt(i - 1) != '\\')
|
||||
}
|
||||
else if (quoted && trimmed.charAt(i - 1) != '\\')
|
||||
{
|
||||
charAllowed = false;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
} else if (!quoted)
|
||||
}
|
||||
else if (!quoted)
|
||||
{
|
||||
quoted = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
} else if (quoted)
|
||||
}
|
||||
else if (quoted)
|
||||
{
|
||||
continue;
|
||||
} else if (c == '.')
|
||||
}
|
||||
else if (c == '.')
|
||||
{
|
||||
if (dotAllowed)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = false;
|
||||
quoteAllowed = true;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
context.errors.emptyImplicitTable(name, context.line.get());
|
||||
return false;
|
||||
}
|
||||
} else if (Character.isWhitespace(c))
|
||||
}
|
||||
else if (Character.isWhitespace(c))
|
||||
{
|
||||
char prev = trimmed.charAt(i - 1);
|
||||
if (!Character.isWhitespace(prev) && prev != '.')
|
||||
@ -209,14 +214,16 @@ public class Identifier
|
||||
dotAllowed = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
if (charAllowed && ALLOWED_CHARS.indexOf(c) > -1)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
@ -234,12 +241,7 @@ public class Identifier
|
||||
|
||||
private static boolean isValidTableArray(String line, Context context)
|
||||
{
|
||||
boolean valid = true;
|
||||
|
||||
if (!line.endsWith("]]"))
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
boolean valid = line.endsWith("]]");
|
||||
|
||||
String trimmed = line.substring(2, line.length() - 2).trim();
|
||||
if (trimmed.isEmpty() || trimmed.charAt(0) == '.' || trimmed.endsWith("."))
|
||||
@ -272,32 +274,38 @@ public class Identifier
|
||||
if (!quoteAllowed)
|
||||
{
|
||||
valid = false;
|
||||
} else if (quoted && trimmed.charAt(i - 1) != '\\')
|
||||
}
|
||||
else if (quoted && trimmed.charAt(i - 1) != '\\')
|
||||
{
|
||||
charAllowed = false;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
} else if (!quoted)
|
||||
}
|
||||
else if (!quoted)
|
||||
{
|
||||
quoted = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
} else if (quoted)
|
||||
}
|
||||
else if (quoted)
|
||||
{
|
||||
continue;
|
||||
} else if (c == '.')
|
||||
}
|
||||
else if (c == '.')
|
||||
{
|
||||
if (dotAllowed)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = false;
|
||||
quoteAllowed = true;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
context.errors.emptyImplicitTable(line, context.line.get());
|
||||
return false;
|
||||
}
|
||||
} else if (Character.isWhitespace(c))
|
||||
}
|
||||
else if (Character.isWhitespace(c))
|
||||
{
|
||||
char prev = trimmed.charAt(i - 1);
|
||||
if (!Character.isWhitespace(prev) && prev != '.' && prev != '"')
|
||||
@ -306,14 +314,16 @@ public class Identifier
|
||||
dotAllowed = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
if (charAllowed && ALLOWED_CHARS.indexOf(c) > -1)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
|
@ -2,61 +2,87 @@ package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class IdentifierConverter {
|
||||
|
||||
static final IdentifierConverter IDENTIFIER_CONVERTER = new IdentifierConverter();
|
||||
public class IdentifierConverter
|
||||
{
|
||||
|
||||
Identifier convert(String s, AtomicInteger index, Context context) {
|
||||
boolean quoted = false;
|
||||
StringBuilder name = new StringBuilder();
|
||||
boolean terminated = false;
|
||||
boolean isKey = s.charAt(index.get()) != '[';
|
||||
boolean isTableArray = !isKey && s.length() > index.get() + 1 && s.charAt(index.get() + 1) == '[';
|
||||
boolean inComment = false;
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
|
||||
char c = s.charAt(i);
|
||||
if (Keys.isQuote(c) && (i == 0 || s.charAt(i - 1) != '\\')) {
|
||||
quoted = !quoted;
|
||||
name.append(c);
|
||||
} else if (c == '\n') {
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
} else if (quoted) {
|
||||
name.append(c);
|
||||
} else if (c == '=' && isKey) {
|
||||
terminated = true;
|
||||
break;
|
||||
} else if (c == ']' && !isKey) {
|
||||
if (!isTableArray || s.length() > index.get() + 1 && s.charAt(index.get() + 1) == ']') {
|
||||
terminated = true;
|
||||
name.append(']');
|
||||
if (isTableArray) {
|
||||
name.append(']');
|
||||
}
|
||||
static final IdentifierConverter IDENTIFIER_CONVERTER = new IdentifierConverter();
|
||||
|
||||
Identifier convert(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
boolean quoted = false;
|
||||
StringBuilder name = new StringBuilder();
|
||||
boolean terminated = false;
|
||||
boolean isKey = s.charAt(index.get()) != '[';
|
||||
boolean isTableArray = !isKey && s.length() > index.get() + 1 && s.charAt(index.get() + 1) == '[';
|
||||
boolean inComment = false;
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
if (Keys.isQuote(c) && (i == 0 || s.charAt(i - 1) != '\\'))
|
||||
{
|
||||
quoted = !quoted;
|
||||
name.append(c);
|
||||
}
|
||||
else if (c == '\n')
|
||||
{
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
else if (quoted)
|
||||
{
|
||||
name.append(c);
|
||||
}
|
||||
else if (c == '=' && isKey)
|
||||
{
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
else if (c == ']' && !isKey)
|
||||
{
|
||||
if (!isTableArray || s.length() > index.get() + 1 && s.charAt(index.get() + 1) == ']')
|
||||
{
|
||||
terminated = true;
|
||||
name.append(']');
|
||||
if (isTableArray)
|
||||
{
|
||||
name.append(']');
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (terminated && c == '#')
|
||||
{
|
||||
inComment = true;
|
||||
}
|
||||
else if (terminated && !Character.isWhitespace(c) && !inComment)
|
||||
{
|
||||
terminated = false;
|
||||
break;
|
||||
}
|
||||
else if (!terminated)
|
||||
{
|
||||
name.append(c);
|
||||
}
|
||||
}
|
||||
} else if (terminated && c == '#') {
|
||||
inComment = true;
|
||||
} else if (terminated && !Character.isWhitespace(c) && !inComment) {
|
||||
terminated = false;
|
||||
break;
|
||||
} else if (!terminated) {
|
||||
name.append(c);
|
||||
}
|
||||
|
||||
if (!terminated)
|
||||
{
|
||||
if (isKey)
|
||||
{
|
||||
context.errors.unterminatedKey(name.toString(), context.line.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
context.errors.invalidKey(name.toString(), context.line.get());
|
||||
}
|
||||
|
||||
return Identifier.INVALID;
|
||||
}
|
||||
|
||||
return Identifier.from(name.toString(), context);
|
||||
}
|
||||
|
||||
if (!terminated) {
|
||||
if (isKey) {
|
||||
context.errors.unterminatedKey(name.toString(), context.line.get());
|
||||
} else {
|
||||
context.errors.invalidKey(name.toString(), context.line.get());
|
||||
}
|
||||
|
||||
return Identifier.INVALID;
|
||||
|
||||
private IdentifierConverter()
|
||||
{
|
||||
}
|
||||
|
||||
return Identifier.from(name.toString(), context);
|
||||
}
|
||||
|
||||
private IdentifierConverter() {}
|
||||
}
|
||||
|
@ -2,29 +2,34 @@ package dev.plex.toml;
|
||||
|
||||
/**
|
||||
* Controls how a {@link TomlWriter} indents tables and key/value pairs.
|
||||
*
|
||||
* <p>
|
||||
* The default policy is to not indent.
|
||||
*/
|
||||
public class IndentationPolicy {
|
||||
private final int tableIndent;
|
||||
private final int keyValueIndent;
|
||||
private final int arrayDelimiterPadding;
|
||||
public class IndentationPolicy
|
||||
{
|
||||
private final int tableIndent;
|
||||
private final int keyValueIndent;
|
||||
private final int arrayDelimiterPadding;
|
||||
|
||||
IndentationPolicy(int keyIndentation, int tableIndentation, int arrayDelimiterPadding) {
|
||||
this.keyValueIndent = keyIndentation;
|
||||
this.tableIndent = tableIndentation;
|
||||
this.arrayDelimiterPadding = arrayDelimiterPadding;
|
||||
}
|
||||
IndentationPolicy(int keyIndentation, int tableIndentation, int arrayDelimiterPadding)
|
||||
{
|
||||
this.keyValueIndent = keyIndentation;
|
||||
this.tableIndent = tableIndentation;
|
||||
this.arrayDelimiterPadding = arrayDelimiterPadding;
|
||||
}
|
||||
|
||||
int getTableIndent() {
|
||||
return tableIndent;
|
||||
}
|
||||
int getTableIndent()
|
||||
{
|
||||
return tableIndent;
|
||||
}
|
||||
|
||||
int getKeyValueIndent() {
|
||||
return keyValueIndent;
|
||||
}
|
||||
int getKeyValueIndent()
|
||||
{
|
||||
return keyValueIndent;
|
||||
}
|
||||
|
||||
int getArrayDelimiterPadding() {
|
||||
return arrayDelimiterPadding;
|
||||
}
|
||||
int getArrayDelimiterPadding()
|
||||
{
|
||||
return arrayDelimiterPadding;
|
||||
}
|
||||
}
|
||||
|
@ -6,71 +6,89 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
class InlineTableValueReader implements dev.plex.toml.ValueReader
|
||||
{
|
||||
|
||||
static final InlineTableValueReader INLINE_TABLE_VALUE_READER = new InlineTableValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
return s.startsWith("{");
|
||||
}
|
||||
static final InlineTableValueReader INLINE_TABLE_VALUE_READER = new InlineTableValueReader();
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger sharedIndex, dev.plex.toml.Context context) {
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int startIndex = sharedIndex.get();
|
||||
boolean inKey = true;
|
||||
boolean inValue = false;
|
||||
boolean terminated = false;
|
||||
StringBuilder currentKey = new StringBuilder();
|
||||
HashMap<String, Object> results = new HashMap<String, Object>();
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
|
||||
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet()) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (inValue && !Character.isWhitespace(c)) {
|
||||
Object converted = dev.plex.toml.ValueReaders.VALUE_READERS.convert(s, sharedIndex, context.with(dev.plex.toml.Identifier.from(currentKey.toString(), context)));
|
||||
|
||||
if (converted instanceof dev.plex.toml.Results.Errors) {
|
||||
errors.add((dev.plex.toml.Results.Errors) converted);
|
||||
return errors;
|
||||
}
|
||||
|
||||
String currentKeyTrimmed = currentKey.toString().trim();
|
||||
Object previous = results.put(currentKeyTrimmed, converted);
|
||||
|
||||
if (previous != null) {
|
||||
errors.duplicateKey(currentKeyTrimmed, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
currentKey = new StringBuilder();
|
||||
inValue = false;
|
||||
} else if (c == ',') {
|
||||
inKey = true;
|
||||
inValue = false;
|
||||
currentKey = new StringBuilder();
|
||||
} else if (c == '=') {
|
||||
inKey = false;
|
||||
inValue = true;
|
||||
} else if (c == '}') {
|
||||
terminated = true;
|
||||
break;
|
||||
} else if (inKey) {
|
||||
currentKey.append(c);
|
||||
}
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("{");
|
||||
}
|
||||
|
||||
if (!terminated) {
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex), startLine);
|
||||
}
|
||||
|
||||
if (errors.hasErrors()) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private InlineTableValueReader() {}
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger sharedIndex, dev.plex.toml.Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int startIndex = sharedIndex.get();
|
||||
boolean inKey = true;
|
||||
boolean inValue = false;
|
||||
boolean terminated = false;
|
||||
StringBuilder currentKey = new StringBuilder();
|
||||
HashMap<String, Object> results = new HashMap<String, Object>();
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
|
||||
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (inValue && !Character.isWhitespace(c))
|
||||
{
|
||||
Object converted = dev.plex.toml.ValueReaders.VALUE_READERS.convert(s, sharedIndex, context.with(dev.plex.toml.Identifier.from(currentKey.toString(), context)));
|
||||
|
||||
if (converted instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
errors.add((dev.plex.toml.Results.Errors)converted);
|
||||
return errors;
|
||||
}
|
||||
|
||||
String currentKeyTrimmed = currentKey.toString().trim();
|
||||
Object previous = results.put(currentKeyTrimmed, converted);
|
||||
|
||||
if (previous != null)
|
||||
{
|
||||
errors.duplicateKey(currentKeyTrimmed, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
currentKey = new StringBuilder();
|
||||
inValue = false;
|
||||
}
|
||||
else if (c == ',')
|
||||
{
|
||||
inKey = true;
|
||||
inValue = false;
|
||||
currentKey = new StringBuilder();
|
||||
}
|
||||
else if (c == '=')
|
||||
{
|
||||
inKey = false;
|
||||
inValue = true;
|
||||
}
|
||||
else if (c == '}')
|
||||
{
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
else if (inKey)
|
||||
{
|
||||
currentKey.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
if (!terminated)
|
||||
{
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex), startLine);
|
||||
}
|
||||
|
||||
if (errors.hasErrors())
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private InlineTableValueReader()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -3,67 +3,84 @@ package dev.plex.toml;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Keys {
|
||||
|
||||
static class Key {
|
||||
final String name;
|
||||
final int index;
|
||||
final String path;
|
||||
class Keys
|
||||
{
|
||||
|
||||
Key(String name, int index, Key next) {
|
||||
this.name = name;
|
||||
this.index = index;
|
||||
if (next != null) {
|
||||
this.path = name + "." + next.path;
|
||||
} else {
|
||||
this.path = name;
|
||||
}
|
||||
static class Key
|
||||
{
|
||||
final String name;
|
||||
final int index;
|
||||
final String path;
|
||||
|
||||
Key(String name, int index, Key next)
|
||||
{
|
||||
this.name = name;
|
||||
this.index = index;
|
||||
if (next != null)
|
||||
{
|
||||
this.path = name + "." + next.path;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.path = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Key[] split(String key) {
|
||||
List<Key> splitKey = new ArrayList<Key>();
|
||||
StringBuilder current = new StringBuilder();
|
||||
boolean quoted = false;
|
||||
boolean indexable = true;
|
||||
boolean inIndex = false;
|
||||
int index = -1;
|
||||
|
||||
for (int i = key.length() - 1; i > -1; i--) {
|
||||
char c = key.charAt(i);
|
||||
if (c == ']' && indexable) {
|
||||
inIndex = true;
|
||||
continue;
|
||||
}
|
||||
indexable = false;
|
||||
if (c == '[' && inIndex) {
|
||||
inIndex = false;
|
||||
index = Integer.parseInt(current.toString());
|
||||
current = new StringBuilder();
|
||||
continue;
|
||||
}
|
||||
if (isQuote(c) && (i == 0 || key.charAt(i - 1) != '\\')) {
|
||||
quoted = !quoted;
|
||||
indexable = false;
|
||||
}
|
||||
if (c != '.' || quoted) {
|
||||
current.insert(0, c);
|
||||
} else {
|
||||
static Key[] split(String key)
|
||||
{
|
||||
List<Key> splitKey = new ArrayList<Key>();
|
||||
StringBuilder current = new StringBuilder();
|
||||
boolean quoted = false;
|
||||
boolean indexable = true;
|
||||
boolean inIndex = false;
|
||||
int index = -1;
|
||||
|
||||
for (int i = key.length() - 1; i > -1; i--)
|
||||
{
|
||||
char c = key.charAt(i);
|
||||
if (c == ']' && indexable)
|
||||
{
|
||||
inIndex = true;
|
||||
continue;
|
||||
}
|
||||
indexable = false;
|
||||
if (c == '[' && inIndex)
|
||||
{
|
||||
inIndex = false;
|
||||
index = Integer.parseInt(current.toString());
|
||||
current = new StringBuilder();
|
||||
continue;
|
||||
}
|
||||
if (isQuote(c) && (i == 0 || key.charAt(i - 1) != '\\'))
|
||||
{
|
||||
quoted = !quoted;
|
||||
indexable = false;
|
||||
}
|
||||
if (c != '.' || quoted)
|
||||
{
|
||||
current.insert(0, c);
|
||||
}
|
||||
else
|
||||
{
|
||||
splitKey.add(0, new Key(current.toString(), index, !splitKey.isEmpty() ? splitKey.get(0) : null));
|
||||
indexable = true;
|
||||
index = -1;
|
||||
current = new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
splitKey.add(0, new Key(current.toString(), index, !splitKey.isEmpty() ? splitKey.get(0) : null));
|
||||
indexable = true;
|
||||
index = -1;
|
||||
current = new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
splitKey.add(0, new Key(current.toString(), index, !splitKey.isEmpty() ? splitKey.get(0) : null));
|
||||
|
||||
return splitKey.toArray(new Key[0]);
|
||||
}
|
||||
|
||||
static boolean isQuote(char c) {
|
||||
return c == '"' || c == '\'';
|
||||
}
|
||||
|
||||
private Keys() {}
|
||||
return splitKey.toArray(new Key[0]);
|
||||
}
|
||||
|
||||
static boolean isQuote(char c)
|
||||
{
|
||||
return c == '"' || c == '\'';
|
||||
}
|
||||
|
||||
private Keys()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
public class LiteralStringValueReader implements ValueReader
|
||||
{
|
||||
public static final LiteralStringValueReader LITERAL_STRING_VALUE_READER = new LiteralStringValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
|
@ -27,7 +27,7 @@ class MapValueWriter implements dev.plex.toml.ValueWriter
|
||||
file = context.file;
|
||||
}
|
||||
|
||||
Map<?, ?> from = (Map<?, ?>) value;
|
||||
Map<?, ?> from = (Map<?, ?>)value;
|
||||
|
||||
Toml toml = null;
|
||||
|
||||
@ -46,7 +46,8 @@ class MapValueWriter implements dev.plex.toml.ValueWriter
|
||||
{
|
||||
context.writeKey();
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
context.writeKey();
|
||||
}
|
||||
@ -70,8 +71,14 @@ class MapValueWriter implements dev.plex.toml.ValueWriter
|
||||
{
|
||||
if (context.key != null)
|
||||
{
|
||||
if (key.toString().equalsIgnoreCase(context.key)) continue;
|
||||
if (toml.contains(context.key + "." + key)) continue;
|
||||
if (key.toString().equalsIgnoreCase(context.key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (toml.contains(context.key + "." + key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,7 +89,8 @@ class MapValueWriter implements dev.plex.toml.ValueWriter
|
||||
context.write(quoteKey(key)).write(" = ");
|
||||
valueWriter.write(fromValue, context);
|
||||
context.write('\n');
|
||||
} else if (valueWriter == dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER)
|
||||
}
|
||||
else if (valueWriter == dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER)
|
||||
{
|
||||
context.indent();
|
||||
context.setArrayKey(key.toString());
|
||||
@ -105,8 +113,14 @@ class MapValueWriter implements dev.plex.toml.ValueWriter
|
||||
{
|
||||
if (context.key != null)
|
||||
{
|
||||
if (key.toString().equalsIgnoreCase(context.key)) continue;
|
||||
if (toml.contains(context.key + "." + key)) continue;
|
||||
if (key.toString().equalsIgnoreCase(context.key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (toml.contains(context.key + "." + key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,49 +4,58 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class MultilineLiteralStringValueReader implements ValueReader
|
||||
{
|
||||
|
||||
static final MultilineLiteralStringValueReader MULTILINE_LITERAL_STRING_VALUE_READER = new MultilineLiteralStringValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
return s.startsWith("'''");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context) {
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int originalStartIndex = index.get();
|
||||
int startIndex = index.addAndGet(3);
|
||||
int endIndex = -1;
|
||||
|
||||
if (s.charAt(startIndex) == '\n') {
|
||||
startIndex = index.incrementAndGet();
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
for (int i = startIndex; i < s.length(); i = index.incrementAndGet()) {
|
||||
char c = s.charAt(i);
|
||||
static final MultilineLiteralStringValueReader MULTILINE_LITERAL_STRING_VALUE_READER = new MultilineLiteralStringValueReader();
|
||||
|
||||
if (c == '\n') {
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
if (c == '\'' && s.length() > i + 2 && s.charAt(i + 1) == '\'' && s.charAt(i + 2) == '\'') {
|
||||
endIndex = i;
|
||||
index.addAndGet(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1) {
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
|
||||
return errors;
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("'''");
|
||||
}
|
||||
|
||||
return s.substring(startIndex, endIndex);
|
||||
}
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int originalStartIndex = index.get();
|
||||
int startIndex = index.addAndGet(3);
|
||||
int endIndex = -1;
|
||||
|
||||
private MultilineLiteralStringValueReader() {}
|
||||
if (s.charAt(startIndex) == '\n')
|
||||
{
|
||||
startIndex = index.incrementAndGet();
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
for (int i = startIndex; i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
if (c == '\'' && s.length() > i + 2 && s.charAt(i + 1) == '\'' && s.charAt(i + 2) == '\'')
|
||||
{
|
||||
endIndex = i;
|
||||
index.addAndGet(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
|
||||
return errors;
|
||||
}
|
||||
|
||||
return s.substring(startIndex, endIndex);
|
||||
}
|
||||
|
||||
private MultilineLiteralStringValueReader()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -5,53 +5,62 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
class MultilineStringValueReader implements ValueReader
|
||||
{
|
||||
|
||||
static final MultilineStringValueReader MULTILINE_STRING_VALUE_READER = new MultilineStringValueReader();
|
||||
static final MultilineStringValueReader MULTILINE_STRING_VALUE_READER = new MultilineStringValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
return s.startsWith("\"\"\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context) {
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int originalStartIndex = index.get();
|
||||
int startIndex = index.addAndGet(3);
|
||||
int endIndex = -1;
|
||||
|
||||
if (s.charAt(startIndex) == '\n') {
|
||||
startIndex = index.incrementAndGet();
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
for (int i = startIndex; i < s.length(); i = index.incrementAndGet()) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '\n') {
|
||||
line.incrementAndGet();
|
||||
} else if (c == '"' && s.length() > i + 2 && s.charAt(i + 1) == '"' && s.charAt(i + 2) == '"') {
|
||||
endIndex = i;
|
||||
index.addAndGet(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1) {
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
|
||||
return errors;
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("\"\"\"");
|
||||
}
|
||||
|
||||
s = s.substring(startIndex, endIndex);
|
||||
s = s.replaceAll("\\\\\\s+", "");
|
||||
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(s);
|
||||
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceSpecialCharacters(s);
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int originalStartIndex = index.get();
|
||||
int startIndex = index.addAndGet(3);
|
||||
int endIndex = -1;
|
||||
|
||||
return s;
|
||||
}
|
||||
if (s.charAt(startIndex) == '\n')
|
||||
{
|
||||
startIndex = index.incrementAndGet();
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
private MultilineStringValueReader() {
|
||||
}
|
||||
for (int i = startIndex; i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
line.incrementAndGet();
|
||||
}
|
||||
else if (c == '"' && s.length() > i + 2 && s.charAt(i + 1) == '"' && s.charAt(i + 2) == '"')
|
||||
{
|
||||
endIndex = i;
|
||||
index.addAndGet(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1)
|
||||
{
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
|
||||
return errors;
|
||||
}
|
||||
|
||||
s = s.substring(startIndex, endIndex);
|
||||
s = s.replaceAll("\\\\\\s+", "");
|
||||
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(s);
|
||||
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceSpecialCharacters(s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
private MultilineStringValueReader()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,103 +4,131 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class NumberValueReaderWriter implements dev.plex.toml.ValueReader, dev.plex.toml.ValueWriter
|
||||
{
|
||||
static final NumberValueReaderWriter NUMBER_VALUE_READER_WRITER = new NumberValueReaderWriter();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
char firstChar = s.charAt(0);
|
||||
|
||||
return firstChar == '+' || firstChar == '-' || Character.isDigit(firstChar);
|
||||
}
|
||||
static final NumberValueReaderWriter NUMBER_VALUE_READER_WRITER = new NumberValueReaderWriter();
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context) {
|
||||
boolean signable = true;
|
||||
boolean dottable = false;
|
||||
boolean exponentable = false;
|
||||
boolean terminatable = false;
|
||||
boolean underscorable = false;
|
||||
String type = "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
char firstChar = s.charAt(0);
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
|
||||
char c = s.charAt(i);
|
||||
boolean notLastChar = s.length() > i + 1;
|
||||
|
||||
if (Character.isDigit(c)) {
|
||||
sb.append(c);
|
||||
signable = false;
|
||||
terminatable = true;
|
||||
if (type.isEmpty()) {
|
||||
type = "integer";
|
||||
dottable = true;
|
||||
}
|
||||
underscorable = notLastChar;
|
||||
exponentable = !type.equals("exponent");
|
||||
} else if ((c == '+' || c == '-') && signable && notLastChar) {
|
||||
signable = false;
|
||||
terminatable = false;
|
||||
if (c == '-') {
|
||||
sb.append('-');
|
||||
}
|
||||
} else if (c == '.' && dottable && notLastChar) {
|
||||
sb.append('.');
|
||||
type = "float";
|
||||
terminatable = false;
|
||||
dottable = false;
|
||||
exponentable = false;
|
||||
underscorable = false;
|
||||
} else if ((c == 'E' || c == 'e') && exponentable && notLastChar) {
|
||||
sb.append('E');
|
||||
type = "exponent";
|
||||
terminatable = false;
|
||||
signable = true;
|
||||
dottable = false;
|
||||
exponentable = false;
|
||||
underscorable = false;
|
||||
} else if (c == '_' && underscorable && notLastChar && Character.isDigit(s.charAt(i + 1))) {
|
||||
underscorable = false;
|
||||
} else {
|
||||
if (!terminatable) {
|
||||
type = "";
|
||||
}
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
return firstChar == '+' || firstChar == '-' || Character.isDigit(firstChar);
|
||||
}
|
||||
|
||||
if (type.equals("integer")) {
|
||||
return Long.valueOf(sb.toString());
|
||||
} else if (type.equals("float")) {
|
||||
return Double.valueOf(sb.toString());
|
||||
} else if (type.equals("exponent")) {
|
||||
String[] exponentString = sb.toString().split("E");
|
||||
|
||||
return Double.parseDouble(exponentString[0]) * Math.pow(10, Double.parseDouble(exponentString[1]));
|
||||
} else {
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), sb.toString(), context.line.get());
|
||||
return errors;
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
boolean signable = true;
|
||||
boolean dottable = false;
|
||||
boolean exponentable = false;
|
||||
boolean terminatable = false;
|
||||
boolean underscorable = false;
|
||||
String type = "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
boolean notLastChar = s.length() > i + 1;
|
||||
|
||||
if (Character.isDigit(c))
|
||||
{
|
||||
sb.append(c);
|
||||
signable = false;
|
||||
terminatable = true;
|
||||
if (type.isEmpty())
|
||||
{
|
||||
type = "integer";
|
||||
dottable = true;
|
||||
}
|
||||
underscorable = notLastChar;
|
||||
exponentable = !type.equals("exponent");
|
||||
}
|
||||
else if ((c == '+' || c == '-') && signable && notLastChar)
|
||||
{
|
||||
signable = false;
|
||||
terminatable = false;
|
||||
if (c == '-')
|
||||
{
|
||||
sb.append('-');
|
||||
}
|
||||
}
|
||||
else if (c == '.' && dottable && notLastChar)
|
||||
{
|
||||
sb.append('.');
|
||||
type = "float";
|
||||
terminatable = false;
|
||||
dottable = false;
|
||||
exponentable = false;
|
||||
underscorable = false;
|
||||
}
|
||||
else if ((c == 'E' || c == 'e') && exponentable && notLastChar)
|
||||
{
|
||||
sb.append('E');
|
||||
type = "exponent";
|
||||
terminatable = false;
|
||||
signable = true;
|
||||
dottable = false;
|
||||
exponentable = false;
|
||||
underscorable = false;
|
||||
}
|
||||
else if (c == '_' && underscorable && notLastChar && Character.isDigit(s.charAt(i + 1)))
|
||||
{
|
||||
underscorable = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!terminatable)
|
||||
{
|
||||
type = "";
|
||||
}
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type.equals("integer"))
|
||||
{
|
||||
return Long.valueOf(sb.toString());
|
||||
}
|
||||
else if (type.equals("float"))
|
||||
{
|
||||
return Double.valueOf(sb.toString());
|
||||
}
|
||||
else if (type.equals("exponent"))
|
||||
{
|
||||
String[] exponentString = sb.toString().split("E");
|
||||
|
||||
return Double.parseDouble(exponentString[0]) * Math.pow(10, Double.parseDouble(exponentString[1]));
|
||||
}
|
||||
else
|
||||
{
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), sb.toString(), context.line.get());
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value) {
|
||||
return Number.class.isInstance(value);
|
||||
}
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof Number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, dev.plex.toml.WriterContext context) {
|
||||
context.write(value.toString());
|
||||
}
|
||||
@Override
|
||||
public void write(Object value, dev.plex.toml.WriterContext context)
|
||||
{
|
||||
context.write(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "number";
|
||||
}
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "number";
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,14 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
class ObjectValueWriter implements ValueWriter
|
||||
{
|
||||
@ -26,7 +30,9 @@ class ObjectValueWriter implements ValueWriter
|
||||
if (field.isAnnotationPresent(SerializedName.class))
|
||||
{
|
||||
to.put(field.getDeclaredAnnotation(SerializedName.class).value(), getFieldValue(field, value));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
to.put(field.getName(), getFieldValue(field, value));
|
||||
}
|
||||
}
|
||||
@ -74,7 +80,8 @@ class ObjectValueWriter implements ValueWriter
|
||||
try
|
||||
{
|
||||
value = field.get(o);
|
||||
} catch (IllegalAccessException ignored)
|
||||
}
|
||||
catch (IllegalAccessException ignored)
|
||||
{
|
||||
}
|
||||
field.setAccessible(isAccessible);
|
||||
|
@ -4,50 +4,60 @@ import java.util.Collection;
|
||||
|
||||
class PrimitiveArrayValueWriter extends ArrayValueWriter
|
||||
{
|
||||
static final ValueWriter PRIMITIVE_ARRAY_VALUE_WRITER = new PrimitiveArrayValueWriter();
|
||||
static final ValueWriter PRIMITIVE_ARRAY_VALUE_WRITER = new PrimitiveArrayValueWriter();
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value) {
|
||||
return isArrayish(value) && isArrayOfPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object o, WriterContext context) {
|
||||
Collection<?> values = normalize(o);
|
||||
|
||||
context.write('[');
|
||||
context.writeArrayDelimiterPadding();
|
||||
|
||||
boolean first = true;
|
||||
ValueWriter firstWriter = null;
|
||||
|
||||
for (Object value : values) {
|
||||
if (first) {
|
||||
firstWriter = ValueWriters.WRITERS.findWriterFor(value);
|
||||
first = false;
|
||||
} else {
|
||||
ValueWriter writer = ValueWriters.WRITERS.findWriterFor(value);
|
||||
if (writer != firstWriter) {
|
||||
throw new IllegalStateException(
|
||||
context.getContextPath() +
|
||||
": cannot write a heterogeneous array; first element was of type " + firstWriter +
|
||||
" but found " + writer
|
||||
);
|
||||
}
|
||||
context.write(", ");
|
||||
}
|
||||
|
||||
ValueWriters.WRITERS.findWriterFor(value).write(value, context);
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return isArrayish(value) && isArrayOfPrimitive(value);
|
||||
}
|
||||
|
||||
context.writeArrayDelimiterPadding();
|
||||
context.write(']');
|
||||
}
|
||||
@Override
|
||||
public void write(Object o, WriterContext context)
|
||||
{
|
||||
Collection<?> values = normalize(o);
|
||||
|
||||
private PrimitiveArrayValueWriter() {}
|
||||
context.write('[');
|
||||
context.writeArrayDelimiterPadding();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "primitive-array";
|
||||
}
|
||||
boolean first = true;
|
||||
ValueWriter firstWriter = null;
|
||||
|
||||
for (Object value : values)
|
||||
{
|
||||
if (first)
|
||||
{
|
||||
firstWriter = ValueWriters.WRITERS.findWriterFor(value);
|
||||
first = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
ValueWriter writer = ValueWriters.WRITERS.findWriterFor(value);
|
||||
if (writer != firstWriter)
|
||||
{
|
||||
throw new IllegalStateException(
|
||||
context.getContextPath() +
|
||||
": cannot write a heterogeneous array; first element was of type " + firstWriter +
|
||||
" but found " + writer
|
||||
);
|
||||
}
|
||||
context.write(", ");
|
||||
}
|
||||
|
||||
ValueWriters.WRITERS.findWriterFor(value).write(value, context);
|
||||
}
|
||||
|
||||
context.writeArrayDelimiterPadding();
|
||||
context.write(']');
|
||||
}
|
||||
|
||||
private PrimitiveArrayValueWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "primitive-array";
|
||||
}
|
||||
}
|
||||
|
@ -1,290 +1,361 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class Results {
|
||||
|
||||
static class Errors {
|
||||
|
||||
private final StringBuilder sb = new StringBuilder();
|
||||
|
||||
void duplicateTable(String table, int line) {
|
||||
sb.append("Duplicate table definition on line ")
|
||||
.append(line)
|
||||
.append(": [")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
class Results
|
||||
{
|
||||
|
||||
public void tableDuplicatesKey(String table, AtomicInteger line) {
|
||||
sb.append("Key already exists for table defined on line ")
|
||||
.append(line.get())
|
||||
.append(": [")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
static class Errors
|
||||
{
|
||||
|
||||
public void keyDuplicatesTable(String key, AtomicInteger line) {
|
||||
sb.append("Table already exists for key defined on line ")
|
||||
.append(line.get())
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void emptyImplicitTable(String table, int line) {
|
||||
sb.append("Invalid table definition due to empty implicit table name: ")
|
||||
.append(table);
|
||||
}
|
||||
|
||||
void invalidTable(String table, int line) {
|
||||
sb.append("Invalid table definition on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
|
||||
void duplicateKey(String key, int line) {
|
||||
sb.append("Duplicate key");
|
||||
if (line > -1) {
|
||||
sb.append(" on line ")
|
||||
.append(line);
|
||||
}
|
||||
sb.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void invalidTextAfterIdentifier(dev.plex.toml.Identifier identifier, char text, int line) {
|
||||
sb.append("Invalid text after key ")
|
||||
.append(identifier.getName())
|
||||
.append(" on line ")
|
||||
.append(line)
|
||||
.append(". Make sure to terminate the value or add a comment (#).");
|
||||
}
|
||||
|
||||
void invalidKey(String key, int line) {
|
||||
sb.append("Invalid key on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void invalidTableArray(String tableArray, int line) {
|
||||
sb.append("Invalid table array definition on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(tableArray);
|
||||
}
|
||||
|
||||
void invalidValue(String key, String value, int line) {
|
||||
sb.append("Invalid value on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key)
|
||||
.append(" = ")
|
||||
.append(value);
|
||||
}
|
||||
|
||||
void unterminatedKey(String key, int line) {
|
||||
sb.append("Key is not followed by an equals sign on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void unterminated(String key, String value, int line) {
|
||||
sb.append("Unterminated value on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key)
|
||||
.append(" = ")
|
||||
.append(value.trim());
|
||||
}
|
||||
private final StringBuilder sb = new StringBuilder();
|
||||
|
||||
public void heterogenous(String key, int line) {
|
||||
sb.append(key)
|
||||
.append(" becomes a heterogeneous array on line ")
|
||||
.append(line);
|
||||
}
|
||||
|
||||
boolean hasErrors() {
|
||||
return sb.length() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void add(Errors other) {
|
||||
sb.append(other.sb);
|
||||
}
|
||||
}
|
||||
|
||||
final Errors errors = new Errors();
|
||||
private final Set<String> tables = new HashSet<String>();
|
||||
private final Deque<Container> stack = new ArrayDeque<Container>();
|
||||
|
||||
Results() {
|
||||
stack.push(new Container.Table(""));
|
||||
}
|
||||
|
||||
void addValue(String key, Object value, AtomicInteger line) {
|
||||
Container currentTable = stack.peek();
|
||||
|
||||
if (value instanceof Map) {
|
||||
String path = getInlineTablePath(key);
|
||||
if (path == null) {
|
||||
startTable(key, line);
|
||||
} else if (path.isEmpty()) {
|
||||
startTables(dev.plex.toml.Identifier.from(key, null), line);
|
||||
} else {
|
||||
startTables(dev.plex.toml.Identifier.from(path, null), line);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> valueMap = (Map<String, Object>) value;
|
||||
for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
|
||||
addValue(entry.getKey(), entry.getValue(), line);
|
||||
}
|
||||
stack.pop();
|
||||
} else if (currentTable.accepts(key)) {
|
||||
currentTable.put(key, value);
|
||||
} else {
|
||||
if (currentTable.get(key) instanceof Container) {
|
||||
errors.keyDuplicatesTable(key, line);
|
||||
} else {
|
||||
errors.duplicateKey(key, line != null ? line.get() : -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startTableArray(dev.plex.toml.Identifier identifier, AtomicInteger line) {
|
||||
String tableName = identifier.getBareName();
|
||||
while (stack.size() > 1) {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
|
||||
for (int i = 0; i < tableParts.length; i++) {
|
||||
String tablePart = tableParts[i].name;
|
||||
Container currentContainer = stack.peek();
|
||||
|
||||
if (currentContainer.get(tablePart) instanceof Container.TableArray) {
|
||||
Container.TableArray currentTableArray = (Container.TableArray) currentContainer.get(tablePart);
|
||||
stack.push(currentTableArray);
|
||||
|
||||
if (i == tableParts.length - 1) {
|
||||
currentTableArray.put(tablePart, new Container.Table());
|
||||
void duplicateTable(String table, int line)
|
||||
{
|
||||
sb.append("Duplicate table definition on line ")
|
||||
.append(line)
|
||||
.append(": [")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
|
||||
stack.push(currentTableArray.getCurrent());
|
||||
currentContainer = stack.peek();
|
||||
} else if (currentContainer.get(tablePart) instanceof Container.Table && i < tableParts.length - 1) {
|
||||
Container nextTable = (Container) currentContainer.get(tablePart);
|
||||
stack.push(nextTable);
|
||||
} else if (currentContainer.accepts(tablePart)) {
|
||||
Container newContainer = i == tableParts.length - 1 ? new Container.TableArray() : new Container.Table();
|
||||
addValue(tablePart, newContainer, line);
|
||||
stack.push(newContainer);
|
||||
|
||||
if (newContainer instanceof Container.TableArray) {
|
||||
stack.push(((Container.TableArray) newContainer).getCurrent());
|
||||
public void tableDuplicatesKey(String table, AtomicInteger line)
|
||||
{
|
||||
sb.append("Key already exists for table defined on line ")
|
||||
.append(line.get())
|
||||
.append(": [")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
} else {
|
||||
errors.duplicateTable(tableName, line.get());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startTables(dev.plex.toml.Identifier id, AtomicInteger line) {
|
||||
String tableName = id.getBareName();
|
||||
|
||||
while (stack.size() > 1) {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
|
||||
for (int i = 0; i < tableParts.length; i++) {
|
||||
String tablePart = tableParts[i].name;
|
||||
Container currentContainer = stack.peek();
|
||||
if (currentContainer.get(tablePart) instanceof Container) {
|
||||
Container nextTable = (Container) currentContainer.get(tablePart);
|
||||
if (i == tableParts.length - 1 && !nextTable.isImplicit()) {
|
||||
errors.duplicateTable(tableName, line.get());
|
||||
return;
|
||||
public void keyDuplicatesTable(String key, AtomicInteger line)
|
||||
{
|
||||
sb.append("Table already exists for key defined on line ")
|
||||
.append(line.get())
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
stack.push(nextTable);
|
||||
if (stack.peek() instanceof Container.TableArray) {
|
||||
stack.push(((Container.TableArray) stack.peek()).getCurrent());
|
||||
|
||||
void emptyImplicitTable(String table, int line)
|
||||
{
|
||||
sb.append("Invalid table definition due to empty implicit table name: ")
|
||||
.append(table);
|
||||
}
|
||||
} else if (currentContainer.accepts(tablePart)) {
|
||||
startTable(tablePart, i < tableParts.length - 1, line);
|
||||
} else {
|
||||
errors.tableDuplicatesKey(tablePart, line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning: After this method has been called, this instance is no longer usable.
|
||||
*/
|
||||
Map<String, Object> consume() {
|
||||
Container values = stack.getLast();
|
||||
stack.clear();
|
||||
void invalidTable(String table, int line)
|
||||
{
|
||||
sb.append("Invalid table definition on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
|
||||
return ((Container.Table) values).consume();
|
||||
}
|
||||
void duplicateKey(String key, int line)
|
||||
{
|
||||
sb.append("Duplicate key");
|
||||
if (line > -1)
|
||||
{
|
||||
sb.append(" on line ")
|
||||
.append(line);
|
||||
}
|
||||
sb.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
private Container startTable(String tableName, AtomicInteger line) {
|
||||
Container newTable = new Container.Table(tableName);
|
||||
addValue(tableName, newTable, line);
|
||||
stack.push(newTable);
|
||||
void invalidTextAfterIdentifier(dev.plex.toml.Identifier identifier, char text, int line)
|
||||
{
|
||||
sb.append("Invalid text after key ")
|
||||
.append(identifier.getName())
|
||||
.append(" on line ")
|
||||
.append(line)
|
||||
.append(". Make sure to terminate the value or add a comment (#).");
|
||||
}
|
||||
|
||||
return newTable;
|
||||
}
|
||||
void invalidKey(String key, int line)
|
||||
{
|
||||
sb.append("Invalid key on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
private Container startTable(String tableName, boolean implicit, AtomicInteger line) {
|
||||
Container newTable = new Container.Table(tableName, implicit);
|
||||
addValue(tableName, newTable, line);
|
||||
stack.push(newTable);
|
||||
void invalidTableArray(String tableArray, int line)
|
||||
{
|
||||
sb.append("Invalid table array definition on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(tableArray);
|
||||
}
|
||||
|
||||
return newTable;
|
||||
}
|
||||
|
||||
private String getInlineTablePath(String key) {
|
||||
Iterator<Container> descendingIterator = stack.descendingIterator();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
while (descendingIterator.hasNext()) {
|
||||
Container next = descendingIterator.next();
|
||||
if (next instanceof Container.TableArray) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Container.Table table = (Container.Table) next;
|
||||
|
||||
if (table.name == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (sb.length() > 0) {
|
||||
sb.append('.');
|
||||
}
|
||||
|
||||
sb.append(table.name);
|
||||
}
|
||||
|
||||
if (sb.length() > 0) {
|
||||
sb.append('.');
|
||||
void invalidValue(String key, String value, int line)
|
||||
{
|
||||
sb.append("Invalid value on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key)
|
||||
.append(" = ")
|
||||
.append(value);
|
||||
}
|
||||
|
||||
void unterminatedKey(String key, int line)
|
||||
{
|
||||
sb.append("Key is not followed by an equals sign on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void unterminated(String key, String value, int line)
|
||||
{
|
||||
sb.append("Unterminated value on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key)
|
||||
.append(" = ")
|
||||
.append(value.trim());
|
||||
}
|
||||
|
||||
public void heterogenous(String key, int line)
|
||||
{
|
||||
sb.append(key)
|
||||
.append(" becomes a heterogeneous array on line ")
|
||||
.append(line);
|
||||
}
|
||||
|
||||
boolean hasErrors()
|
||||
{
|
||||
return sb.length() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void add(Errors other)
|
||||
{
|
||||
sb.append(other.sb);
|
||||
}
|
||||
}
|
||||
|
||||
sb.append(key)
|
||||
.insert(0, '[')
|
||||
.append(']');
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
final Errors errors = new Errors();
|
||||
private final Set<String> tables = new HashSet<String>();
|
||||
private final Deque<Container> stack = new ArrayDeque<Container>();
|
||||
|
||||
Results()
|
||||
{
|
||||
stack.push(new Container.Table(""));
|
||||
}
|
||||
|
||||
void addValue(String key, Object value, AtomicInteger line)
|
||||
{
|
||||
Container currentTable = stack.peek();
|
||||
|
||||
if (value instanceof Map)
|
||||
{
|
||||
String path = getInlineTablePath(key);
|
||||
if (path == null)
|
||||
{
|
||||
startTable(key, line);
|
||||
}
|
||||
else if (path.isEmpty())
|
||||
{
|
||||
startTables(dev.plex.toml.Identifier.from(key, null), line);
|
||||
}
|
||||
else
|
||||
{
|
||||
startTables(dev.plex.toml.Identifier.from(path, null), line);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> valueMap = (Map<String, Object>)value;
|
||||
for (Map.Entry<String, Object> entry : valueMap.entrySet())
|
||||
{
|
||||
addValue(entry.getKey(), entry.getValue(), line);
|
||||
}
|
||||
stack.pop();
|
||||
}
|
||||
else if (currentTable.accepts(key))
|
||||
{
|
||||
currentTable.put(key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentTable.get(key) instanceof Container)
|
||||
{
|
||||
errors.keyDuplicatesTable(key, line);
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.duplicateKey(key, line != null ? line.get() : -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startTableArray(dev.plex.toml.Identifier identifier, AtomicInteger line)
|
||||
{
|
||||
String tableName = identifier.getBareName();
|
||||
while (stack.size() > 1)
|
||||
{
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
|
||||
for (int i = 0; i < tableParts.length; i++)
|
||||
{
|
||||
String tablePart = tableParts[i].name;
|
||||
Container currentContainer = stack.peek();
|
||||
|
||||
if (currentContainer.get(tablePart) instanceof Container.TableArray)
|
||||
{
|
||||
Container.TableArray currentTableArray = (Container.TableArray)currentContainer.get(tablePart);
|
||||
stack.push(currentTableArray);
|
||||
|
||||
if (i == tableParts.length - 1)
|
||||
{
|
||||
currentTableArray.put(tablePart, new Container.Table());
|
||||
}
|
||||
|
||||
stack.push(currentTableArray.getCurrent());
|
||||
currentContainer = stack.peek();
|
||||
}
|
||||
else if (currentContainer.get(tablePart) instanceof Container.Table && i < tableParts.length - 1)
|
||||
{
|
||||
Container nextTable = (Container)currentContainer.get(tablePart);
|
||||
stack.push(nextTable);
|
||||
}
|
||||
else if (currentContainer.accepts(tablePart))
|
||||
{
|
||||
Container newContainer = i == tableParts.length - 1 ? new Container.TableArray() : new Container.Table();
|
||||
addValue(tablePart, newContainer, line);
|
||||
stack.push(newContainer);
|
||||
|
||||
if (newContainer instanceof Container.TableArray)
|
||||
{
|
||||
stack.push(((Container.TableArray)newContainer).getCurrent());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.duplicateTable(tableName, line.get());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startTables(dev.plex.toml.Identifier id, AtomicInteger line)
|
||||
{
|
||||
String tableName = id.getBareName();
|
||||
|
||||
while (stack.size() > 1)
|
||||
{
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
|
||||
for (int i = 0; i < tableParts.length; i++)
|
||||
{
|
||||
String tablePart = tableParts[i].name;
|
||||
Container currentContainer = stack.peek();
|
||||
if (currentContainer.get(tablePart) instanceof Container)
|
||||
{
|
||||
Container nextTable = (Container)currentContainer.get(tablePart);
|
||||
if (i == tableParts.length - 1 && !nextTable.isImplicit())
|
||||
{
|
||||
errors.duplicateTable(tableName, line.get());
|
||||
return;
|
||||
}
|
||||
stack.push(nextTable);
|
||||
if (stack.peek() instanceof Container.TableArray)
|
||||
{
|
||||
stack.push(((Container.TableArray)stack.peek()).getCurrent());
|
||||
}
|
||||
}
|
||||
else if (currentContainer.accepts(tablePart))
|
||||
{
|
||||
startTable(tablePart, i < tableParts.length - 1, line);
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.tableDuplicatesKey(tablePart, line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning: After this method has been called, this instance is no longer usable.
|
||||
*/
|
||||
Map<String, Object> consume()
|
||||
{
|
||||
Container values = stack.getLast();
|
||||
stack.clear();
|
||||
|
||||
return ((Container.Table)values).consume();
|
||||
}
|
||||
|
||||
private Container startTable(String tableName, AtomicInteger line)
|
||||
{
|
||||
Container newTable = new Container.Table(tableName);
|
||||
addValue(tableName, newTable, line);
|
||||
stack.push(newTable);
|
||||
|
||||
return newTable;
|
||||
}
|
||||
|
||||
private Container startTable(String tableName, boolean implicit, AtomicInteger line)
|
||||
{
|
||||
Container newTable = new Container.Table(tableName, implicit);
|
||||
addValue(tableName, newTable, line);
|
||||
stack.push(newTable);
|
||||
|
||||
return newTable;
|
||||
}
|
||||
|
||||
private String getInlineTablePath(String key)
|
||||
{
|
||||
Iterator<Container> descendingIterator = stack.descendingIterator();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
while (descendingIterator.hasNext())
|
||||
{
|
||||
Container next = descendingIterator.next();
|
||||
if (next instanceof Container.TableArray)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Container.Table table = (Container.Table)next;
|
||||
|
||||
if (table.name == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (sb.length() > 0)
|
||||
{
|
||||
sb.append('.');
|
||||
}
|
||||
|
||||
sb.append(table.name);
|
||||
}
|
||||
|
||||
if (sb.length() > 0)
|
||||
{
|
||||
sb.append('.');
|
||||
}
|
||||
|
||||
sb.append(key)
|
||||
.insert(0, '[')
|
||||
.append(']');
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -8,122 +8,147 @@ import java.util.regex.Pattern;
|
||||
|
||||
class StringValueReaderWriter implements ValueReader, ValueWriter
|
||||
{
|
||||
|
||||
static final StringValueReaderWriter STRING_VALUE_READER_WRITER = new StringValueReaderWriter();
|
||||
private static final Pattern UNICODE_REGEX = Pattern.compile("\\\\[uU](.{4})");
|
||||
|
||||
static private final String[] specialCharacterEscapes = new String[93];
|
||||
static final StringValueReaderWriter STRING_VALUE_READER_WRITER = new StringValueReaderWriter();
|
||||
private static final Pattern UNICODE_REGEX = Pattern.compile("\\\\[uU](.{4})");
|
||||
|
||||
static {
|
||||
specialCharacterEscapes['\b'] = "\\b";
|
||||
specialCharacterEscapes['\t'] = "\\t";
|
||||
specialCharacterEscapes['\n'] = "\\n";
|
||||
specialCharacterEscapes['\f'] = "\\f";
|
||||
specialCharacterEscapes['\r'] = "\\r";
|
||||
specialCharacterEscapes['"'] = "\\\"";
|
||||
specialCharacterEscapes['\\'] = "\\\\";
|
||||
}
|
||||
static private final String[] specialCharacterEscapes = new String[93];
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s) {
|
||||
return s.startsWith("\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context) {
|
||||
int startIndex = index.incrementAndGet();
|
||||
int endIndex = -1;
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
|
||||
char ch = s.charAt(i);
|
||||
if (ch == '"' && s.charAt(i - 1) != '\\') {
|
||||
endIndex = i;
|
||||
break;
|
||||
}
|
||||
static
|
||||
{
|
||||
specialCharacterEscapes['\b'] = "\\b";
|
||||
specialCharacterEscapes['\t'] = "\\t";
|
||||
specialCharacterEscapes['\n'] = "\\n";
|
||||
specialCharacterEscapes['\f'] = "\\f";
|
||||
specialCharacterEscapes['\r'] = "\\r";
|
||||
specialCharacterEscapes['"'] = "\\\"";
|
||||
specialCharacterEscapes['\\'] = "\\\\";
|
||||
}
|
||||
|
||||
if (endIndex == -1) {
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex - 1), context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
String raw = s.substring(startIndex, endIndex);
|
||||
s = replaceUnicodeCharacters(raw);
|
||||
s = replaceSpecialCharacters(s);
|
||||
|
||||
if (s == null) {
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), raw, context.line.get());
|
||||
return errors;
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("\"");
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
int startIndex = index.incrementAndGet();
|
||||
int endIndex = -1;
|
||||
|
||||
String replaceUnicodeCharacters(String value) {
|
||||
Matcher unicodeMatcher = UNICODE_REGEX.matcher(value);
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char ch = s.charAt(i);
|
||||
if (ch == '"' && s.charAt(i - 1) != '\\')
|
||||
{
|
||||
endIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (unicodeMatcher.find()) {
|
||||
value = value.replace(unicodeMatcher.group(), new String(Character.toChars(Integer.parseInt(unicodeMatcher.group(1), 16))));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
if (endIndex == -1)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex - 1), context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
String replaceSpecialCharacters(String s) {
|
||||
for (int i = 0; i < s.length() - 1; i++) {
|
||||
char ch = s.charAt(i);
|
||||
char next = s.charAt(i + 1);
|
||||
String raw = s.substring(startIndex, endIndex);
|
||||
s = replaceUnicodeCharacters(raw);
|
||||
s = replaceSpecialCharacters(s);
|
||||
|
||||
if (ch == '\\' && next == '\\') {
|
||||
i++;
|
||||
} else if (ch == '\\' && !(next == 'b' || next == 'f' || next == 'n' || next == 't' || next == 'r' || next == '"' || next == '\\')) {
|
||||
return null;
|
||||
}
|
||||
if (s == null)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), raw, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
return s.replace("\\n", "\n")
|
||||
.replace("\\\"", "\"")
|
||||
.replace("\\t", "\t")
|
||||
.replace("\\r", "\r")
|
||||
.replace("\\\\", "\\")
|
||||
.replace("\\/", "/")
|
||||
.replace("\\b", "\b")
|
||||
.replace("\\f", "\f");
|
||||
}
|
||||
String replaceUnicodeCharacters(String value)
|
||||
{
|
||||
Matcher unicodeMatcher = UNICODE_REGEX.matcher(value);
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value) {
|
||||
return value instanceof String || value instanceof Character || value instanceof URL || value instanceof URI || value instanceof Enum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context) {
|
||||
context.write('"');
|
||||
escapeUnicode(value.toString(), context);
|
||||
context.write('"');
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void escapeUnicode(String in, WriterContext context) {
|
||||
for (int i = 0; i < in.length(); i++) {
|
||||
int codePoint = in.codePointAt(i);
|
||||
if (codePoint < specialCharacterEscapes.length && specialCharacterEscapes[codePoint] != null) {
|
||||
context.write(specialCharacterEscapes[codePoint]);
|
||||
} else {
|
||||
context.write(in.charAt(i));
|
||||
}
|
||||
while (unicodeMatcher.find())
|
||||
{
|
||||
value = value.replace(unicodeMatcher.group(), new String(Character.toChars(Integer.parseInt(unicodeMatcher.group(1), 16))));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private StringValueReaderWriter() {}
|
||||
String replaceSpecialCharacters(String s)
|
||||
{
|
||||
for (int i = 0; i < s.length() - 1; i++)
|
||||
{
|
||||
char ch = s.charAt(i);
|
||||
char next = s.charAt(i + 1);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "string";
|
||||
}
|
||||
if (ch == '\\' && next == '\\')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else if (ch == '\\' && !(next == 'b' || next == 'f' || next == 'n' || next == 't' || next == 'r' || next == '"' || next == '\\'))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return s.replace("\\n", "\n")
|
||||
.replace("\\\"", "\"")
|
||||
.replace("\\t", "\t")
|
||||
.replace("\\r", "\r")
|
||||
.replace("\\\\", "\\")
|
||||
.replace("\\/", "/")
|
||||
.replace("\\b", "\b")
|
||||
.replace("\\f", "\f");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof String || value instanceof Character || value instanceof URL || value instanceof URI || value instanceof Enum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
context.write('"');
|
||||
escapeUnicode(value.toString(), context);
|
||||
context.write('"');
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private void escapeUnicode(String in, WriterContext context)
|
||||
{
|
||||
for (int i = 0; i < in.length(); i++)
|
||||
{
|
||||
int codePoint = in.codePointAt(i);
|
||||
if (codePoint < specialCharacterEscapes.length && specialCharacterEscapes[codePoint] != null)
|
||||
{
|
||||
context.write(specialCharacterEscapes[codePoint]);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.write(in.charAt(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private StringValueReaderWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "string";
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,38 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static dev.plex.toml.ValueWriters.WRITERS;
|
||||
|
||||
class TableArrayValueWriter extends ArrayValueWriter
|
||||
{
|
||||
static final ValueWriter TABLE_ARRAY_VALUE_WRITER = new TableArrayValueWriter();
|
||||
static final ValueWriter TABLE_ARRAY_VALUE_WRITER = new TableArrayValueWriter();
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value) {
|
||||
return isArrayish(value) && !isArrayOfPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object from, WriterContext context) {
|
||||
Collection<?> values = normalize(from);
|
||||
|
||||
WriterContext subContext = context.pushTableFromArray();
|
||||
|
||||
for (Object value : values) {
|
||||
WRITERS.findWriterFor(value).write(value, subContext);
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return isArrayish(value) && !isArrayOfPrimitive(value);
|
||||
}
|
||||
}
|
||||
|
||||
private TableArrayValueWriter() {}
|
||||
@Override
|
||||
public void write(Object from, WriterContext context)
|
||||
{
|
||||
Collection<?> values = normalize(from);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "table-array";
|
||||
}
|
||||
WriterContext subContext = context.pushTableFromArray();
|
||||
|
||||
for (Object value : values)
|
||||
{
|
||||
WRITERS.findWriterFor(value).write(value, subContext);
|
||||
}
|
||||
}
|
||||
|
||||
private TableArrayValueWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "table-array";
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,22 @@ package dev.plex.toml;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import lombok.Getter;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* <p>Provides access to the keys and tables in a TOML data source.</p>
|
||||
@ -64,8 +73,9 @@ public class Toml
|
||||
{
|
||||
try
|
||||
{
|
||||
return read(new InputStreamReader(new FileInputStream(file), "UTF8"));
|
||||
} catch (Exception e)
|
||||
return read(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -105,15 +115,18 @@ public class Toml
|
||||
line = bufferedReader.readLine();
|
||||
}
|
||||
read(w.toString());
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
} finally
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
bufferedReader.close();
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -155,7 +168,7 @@ public class Toml
|
||||
|
||||
public String getString(String key)
|
||||
{
|
||||
return (String) get(key);
|
||||
return (String)get(key);
|
||||
}
|
||||
|
||||
public String getString(String key, String defaultValue)
|
||||
@ -166,7 +179,7 @@ public class Toml
|
||||
|
||||
public Long getLong(String key)
|
||||
{
|
||||
return (Long) get(key);
|
||||
return (Long)get(key);
|
||||
}
|
||||
|
||||
public Long getLong(String key, Long defaultValue)
|
||||
@ -183,7 +196,7 @@ public class Toml
|
||||
public <T> List<T> getList(String key)
|
||||
{
|
||||
@SuppressWarnings("unchecked")
|
||||
List<T> list = (List<T>) get(key);
|
||||
List<T> list = (List<T>)get(key);
|
||||
|
||||
return list;
|
||||
}
|
||||
@ -203,7 +216,7 @@ public class Toml
|
||||
|
||||
public Boolean getBoolean(String key)
|
||||
{
|
||||
return (Boolean) get(key);
|
||||
return (Boolean)get(key);
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key, Boolean defaultValue)
|
||||
@ -214,7 +227,7 @@ public class Toml
|
||||
|
||||
public Date getDate(String key)
|
||||
{
|
||||
return (Date) get(key);
|
||||
return (Date)get(key);
|
||||
}
|
||||
|
||||
public Date getDate(String key, Date defaultValue)
|
||||
@ -225,7 +238,7 @@ public class Toml
|
||||
|
||||
public Double getDouble(String key)
|
||||
{
|
||||
return (Double) get(key);
|
||||
return (Double)get(key);
|
||||
}
|
||||
|
||||
public Double getDouble(String key, Double defaultValue)
|
||||
@ -241,7 +254,7 @@ public class Toml
|
||||
@SuppressWarnings("unchecked")
|
||||
public Toml getTable(String key)
|
||||
{
|
||||
Map<String, Object> map = (Map<String, Object>) get(key);
|
||||
Map<String, Object> map = (Map<String, Object>)get(key);
|
||||
|
||||
return map != null ? new Toml(null, map) : null;
|
||||
}
|
||||
@ -253,7 +266,7 @@ public class Toml
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Toml> getTables(String key)
|
||||
{
|
||||
List<Map<String, Object>> tableArray = (List<Map<String, Object>>) get(key);
|
||||
List<Map<String, Object>> tableArray = (List<Map<String, Object>>)get(key);
|
||||
|
||||
if (tableArray == null)
|
||||
{
|
||||
@ -386,17 +399,20 @@ public class Toml
|
||||
if (Map.class.isAssignableFrom(entryClass))
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), getTable(entry.getKey())));
|
||||
} else if (List.class.isAssignableFrom(entryClass))
|
||||
}
|
||||
else if (List.class.isAssignableFrom(entryClass))
|
||||
{
|
||||
List<?> value = (List<?>) entry.getValue();
|
||||
List<?> value = (List<?>)entry.getValue();
|
||||
if (!value.isEmpty() && value.get(0) instanceof Map)
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), getTables(entry.getKey())));
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), value));
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
@ -450,21 +466,21 @@ public class Toml
|
||||
|
||||
for (dev.plex.toml.Keys.Key k : keys)
|
||||
{
|
||||
if (k.index == -1 && current instanceof Map && ((Map<String, Object>) current).containsKey(k.path))
|
||||
if (k.index == -1 && current instanceof Map && ((Map<String, Object>)current).containsKey(k.path))
|
||||
{
|
||||
return ((Map<String, Object>) current).get(k.path);
|
||||
return ((Map<String, Object>)current).get(k.path);
|
||||
}
|
||||
|
||||
current = ((Map<String, Object>) current).get(k.name);
|
||||
current = ((Map<String, Object>)current).get(k.name);
|
||||
|
||||
if (k.index > -1 && current != null)
|
||||
{
|
||||
if (k.index >= ((List<?>) current).size())
|
||||
if (k.index >= ((List<?>)current).size())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
current = ((List<?>) current).get(k.index);
|
||||
current = ((List<?>)current).get(k.index);
|
||||
}
|
||||
|
||||
if (current == null)
|
||||
|
@ -2,62 +2,87 @@ package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class TomlParser {
|
||||
class TomlParser
|
||||
{
|
||||
|
||||
static dev.plex.toml.Results run(String tomlString) {
|
||||
final dev.plex.toml.Results results = new dev.plex.toml.Results();
|
||||
|
||||
if (tomlString.isEmpty()) {
|
||||
return results;
|
||||
}
|
||||
|
||||
AtomicInteger index = new AtomicInteger();
|
||||
boolean inComment = false;
|
||||
AtomicInteger line = new AtomicInteger(1);
|
||||
dev.plex.toml.Identifier identifier = null;
|
||||
Object value = null;
|
||||
|
||||
for (int i = index.get(); i < tomlString.length(); i = index.incrementAndGet()) {
|
||||
char c = tomlString.charAt(i);
|
||||
|
||||
if (results.errors.hasErrors()) {
|
||||
break;
|
||||
}
|
||||
static dev.plex.toml.Results run(String tomlString)
|
||||
{
|
||||
final dev.plex.toml.Results results = new dev.plex.toml.Results();
|
||||
|
||||
if (c == '#' && !inComment) {
|
||||
inComment = true;
|
||||
} else if (!Character.isWhitespace(c) && !inComment && identifier == null) {
|
||||
dev.plex.toml.Identifier id = dev.plex.toml.IdentifierConverter.IDENTIFIER_CONVERTER.convert(tomlString, index, new dev.plex.toml.Context(null, line, results.errors));
|
||||
|
||||
if (id != dev.plex.toml.Identifier.INVALID) {
|
||||
if (id.isKey()) {
|
||||
identifier = id;
|
||||
} else if (id.isTable()) {
|
||||
results.startTables(id, line);
|
||||
} else if (id.isTableArray()) {
|
||||
results.startTableArray(id, line);
|
||||
}
|
||||
if (tomlString.isEmpty())
|
||||
{
|
||||
return results;
|
||||
}
|
||||
} else if (c == '\n') {
|
||||
inComment = false;
|
||||
identifier = null;
|
||||
value = null;
|
||||
line.incrementAndGet();
|
||||
} else if (!inComment && identifier != null && identifier.isKey() && value == null && !Character.isWhitespace(c)) {
|
||||
value = ValueReaders.VALUE_READERS.convert(tomlString, index, new dev.plex.toml.Context(identifier, line, results.errors));
|
||||
|
||||
if (value instanceof dev.plex.toml.Results.Errors) {
|
||||
results.errors.add((dev.plex.toml.Results.Errors) value);
|
||||
} else {
|
||||
results.addValue(identifier.getName(), value, line);
|
||||
|
||||
AtomicInteger index = new AtomicInteger();
|
||||
boolean inComment = false;
|
||||
AtomicInteger line = new AtomicInteger(1);
|
||||
dev.plex.toml.Identifier identifier = null;
|
||||
Object value = null;
|
||||
|
||||
for (int i = index.get(); i < tomlString.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = tomlString.charAt(i);
|
||||
|
||||
if (results.errors.hasErrors())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '#' && !inComment)
|
||||
{
|
||||
inComment = true;
|
||||
}
|
||||
else if (!Character.isWhitespace(c) && !inComment && identifier == null)
|
||||
{
|
||||
dev.plex.toml.Identifier id = dev.plex.toml.IdentifierConverter.IDENTIFIER_CONVERTER.convert(tomlString, index, new dev.plex.toml.Context(null, line, results.errors));
|
||||
|
||||
if (id != dev.plex.toml.Identifier.INVALID)
|
||||
{
|
||||
if (id.isKey())
|
||||
{
|
||||
identifier = id;
|
||||
}
|
||||
else if (id.isTable())
|
||||
{
|
||||
results.startTables(id, line);
|
||||
}
|
||||
else if (id.isTableArray())
|
||||
{
|
||||
results.startTableArray(id, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (c == '\n')
|
||||
{
|
||||
inComment = false;
|
||||
identifier = null;
|
||||
value = null;
|
||||
line.incrementAndGet();
|
||||
}
|
||||
else if (!inComment && identifier != null && identifier.isKey() && value == null && !Character.isWhitespace(c))
|
||||
{
|
||||
value = ValueReaders.VALUE_READERS.convert(tomlString, index, new dev.plex.toml.Context(identifier, line, results.errors));
|
||||
|
||||
if (value instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
results.errors.add((dev.plex.toml.Results.Errors)value);
|
||||
}
|
||||
else
|
||||
{
|
||||
results.addValue(identifier.getName(), value, line);
|
||||
}
|
||||
}
|
||||
else if (value != null && !inComment && !Character.isWhitespace(c))
|
||||
{
|
||||
results.errors.invalidTextAfterIdentifier(identifier, c, line.get());
|
||||
}
|
||||
}
|
||||
} else if (value != null && !inComment && !Character.isWhitespace(c)) {
|
||||
results.errors.invalidTextAfterIdentifier(identifier, c, line.get());
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private TomlParser() {}
|
||||
private TomlParser()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import static dev.plex.toml.ValueWriters.WRITERS;
|
||||
|
||||
/**
|
||||
@ -112,7 +117,8 @@ public class TomlWriter
|
||||
write(from, output, null);
|
||||
|
||||
return output.toString();
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -131,7 +137,8 @@ public class TomlWriter
|
||||
try
|
||||
{
|
||||
write(from, outputStream, target);
|
||||
} finally
|
||||
}
|
||||
finally
|
||||
{
|
||||
outputStream.close();
|
||||
}
|
||||
@ -146,7 +153,7 @@ public class TomlWriter
|
||||
*/
|
||||
public void write(Object from, OutputStream target, @Nullable File file) throws IOException
|
||||
{
|
||||
OutputStreamWriter writer = new OutputStreamWriter(target, "UTF-8");
|
||||
OutputStreamWriter writer = new OutputStreamWriter(target, StandardCharsets.UTF_8);
|
||||
write(from, writer, file);
|
||||
writer.flush();
|
||||
}
|
||||
@ -170,7 +177,8 @@ public class TomlWriter
|
||||
context.file = file;
|
||||
}
|
||||
valueWriter.write(from, context);
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("An object of class " + from.getClass().getSimpleName() + " cannot produce valid TOML. Please pass in a Map or a custom type.");
|
||||
}
|
||||
|
@ -2,19 +2,20 @@ package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
interface ValueReader {
|
||||
interface ValueReader
|
||||
{
|
||||
|
||||
/**
|
||||
* @param s must already have been trimmed
|
||||
*/
|
||||
boolean canRead(String s);
|
||||
|
||||
/**
|
||||
* Partial validation. Stops after type terminator, rather than at EOI.
|
||||
*
|
||||
* @param s must already have been validated by {@link #canRead(String)}
|
||||
* @param index where to start in s
|
||||
* @return a value or a {@link dev.plex.toml.Results.Errors}
|
||||
*/
|
||||
Object read(String s, AtomicInteger index, dev.plex.toml.Context context);
|
||||
/**
|
||||
* @param s must already have been trimmed
|
||||
*/
|
||||
boolean canRead(String s);
|
||||
|
||||
/**
|
||||
* Partial validation. Stops after type terminator, rather than at EOI.
|
||||
*
|
||||
* @param s must already have been validated by {@link #canRead(String)}
|
||||
* @param index where to start in s
|
||||
* @return a value or a {@link dev.plex.toml.Results.Errors}
|
||||
*/
|
||||
Object read(String s, AtomicInteger index, dev.plex.toml.Context context);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static dev.plex.toml.ArrayValueReader.ARRAY_VALUE_READER;
|
||||
import static dev.plex.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER;
|
||||
import static dev.plex.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
|
||||
@ -10,26 +9,32 @@ import static dev.plex.toml.MultilineLiteralStringValueReader.MULTILINE_LITERAL_
|
||||
import static dev.plex.toml.MultilineStringValueReader.MULTILINE_STRING_VALUE_READER;
|
||||
import static dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER;
|
||||
|
||||
class ValueReaders {
|
||||
|
||||
static final ValueReaders VALUE_READERS = new ValueReaders();
|
||||
|
||||
Object convert(String value, AtomicInteger index, dev.plex.toml.Context context) {
|
||||
String substring = value.substring(index.get());
|
||||
for (dev.plex.toml.ValueReader valueParser : READERS) {
|
||||
if (valueParser.canRead(substring)) {
|
||||
return valueParser.read(value, index, context);
|
||||
}
|
||||
class ValueReaders
|
||||
{
|
||||
|
||||
static final ValueReaders VALUE_READERS = new ValueReaders();
|
||||
|
||||
Object convert(String value, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
String substring = value.substring(index.get());
|
||||
for (dev.plex.toml.ValueReader valueParser : READERS)
|
||||
{
|
||||
if (valueParser.canRead(substring))
|
||||
{
|
||||
return valueParser.read(value, index, context);
|
||||
}
|
||||
}
|
||||
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), substring, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), substring, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
private ValueReaders() {}
|
||||
|
||||
private static final dev.plex.toml.ValueReader[] READERS = {
|
||||
MULTILINE_STRING_VALUE_READER, MULTILINE_LITERAL_STRING_VALUE_READER, LITERAL_STRING_VALUE_READER, STRING_VALUE_READER_WRITER, DATE_VALUE_READER_WRITER, NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER, BOOLEAN_VALUE_READER_WRITER, ARRAY_VALUE_READER, InlineTableValueReader.INLINE_TABLE_VALUE_READER
|
||||
};
|
||||
|
||||
private ValueReaders()
|
||||
{
|
||||
}
|
||||
|
||||
private static final dev.plex.toml.ValueReader[] READERS = {
|
||||
MULTILINE_STRING_VALUE_READER, MULTILINE_LITERAL_STRING_VALUE_READER, LITERAL_STRING_VALUE_READER, STRING_VALUE_READER_WRITER, DATE_VALUE_READER_WRITER, NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER, BOOLEAN_VALUE_READER_WRITER, ARRAY_VALUE_READER, InlineTableValueReader.INLINE_TABLE_VALUE_READER
|
||||
};
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
interface ValueWriter {
|
||||
boolean canWrite(Object value);
|
||||
interface ValueWriter
|
||||
{
|
||||
boolean canWrite(Object value);
|
||||
|
||||
void write(Object value, WriterContext context);
|
||||
void write(Object value, WriterContext context);
|
||||
|
||||
boolean isPrimitiveType();
|
||||
boolean isPrimitiveType();
|
||||
}
|
||||
|
@ -1,28 +1,35 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
class ValueWriters {
|
||||
class ValueWriters
|
||||
{
|
||||
|
||||
static final ValueWriters WRITERS = new ValueWriters();
|
||||
static final ValueWriters WRITERS = new ValueWriters();
|
||||
|
||||
ValueWriter findWriterFor(Object value) {
|
||||
for (ValueWriter valueWriter : VALUE_WRITERS) {
|
||||
if (valueWriter.canWrite(value)) {
|
||||
return valueWriter;
|
||||
}
|
||||
ValueWriter findWriterFor(Object value)
|
||||
{
|
||||
for (ValueWriter valueWriter : VALUE_WRITERS)
|
||||
{
|
||||
if (valueWriter.canWrite(value))
|
||||
{
|
||||
return valueWriter;
|
||||
}
|
||||
}
|
||||
|
||||
return ObjectValueWriter.OBJECT_VALUE_WRITER;
|
||||
}
|
||||
|
||||
return ObjectValueWriter.OBJECT_VALUE_WRITER;
|
||||
}
|
||||
private ValueWriters()
|
||||
{
|
||||
}
|
||||
|
||||
private ValueWriters() {}
|
||||
|
||||
private static dev.plex.toml.DateValueReaderWriter getPlatformSpecificDateConverter() {
|
||||
String specificationVersion = Runtime.class.getPackage().getSpecificationVersion();
|
||||
return specificationVersion != null && specificationVersion.startsWith("1.6") ? dev.plex.toml.DateValueReaderWriter.DATE_PARSER_JDK_6 : dev.plex.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
|
||||
}
|
||||
private static dev.plex.toml.DateValueReaderWriter getPlatformSpecificDateConverter()
|
||||
{
|
||||
String specificationVersion = Runtime.class.getPackage().getSpecificationVersion();
|
||||
return specificationVersion != null && specificationVersion.startsWith("1.6") ? dev.plex.toml.DateValueReaderWriter.DATE_PARSER_JDK_6 : dev.plex.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
|
||||
}
|
||||
|
||||
private static final ValueWriter[] VALUE_WRITERS = {
|
||||
StringValueReaderWriter.STRING_VALUE_READER_WRITER, NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER, dev.plex.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER, getPlatformSpecificDateConverter(),
|
||||
MapValueWriter.MAP_VALUE_WRITER, dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER, TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER
|
||||
};
|
||||
private static final ValueWriter[] VALUE_WRITERS = {
|
||||
StringValueReaderWriter.STRING_VALUE_READER_WRITER, NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER, dev.plex.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER, getPlatformSpecificDateConverter(),
|
||||
MapValueWriter.MAP_VALUE_WRITER, dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER, TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER
|
||||
};
|
||||
}
|
||||
|
@ -68,7 +68,8 @@ class WriterContext
|
||||
}
|
||||
|
||||
return this;
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -90,7 +91,8 @@ class WriterContext
|
||||
empty = false;
|
||||
|
||||
return this;
|
||||
} catch (IOException e)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -113,7 +115,8 @@ class WriterContext
|
||||
if (isArrayOfTable)
|
||||
{
|
||||
write("[[").write(key).write("]]\n");
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
write('[').write(key).write("]\n");
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
public class RandomUtil
|
||||
{
|
||||
|
@ -3,7 +3,6 @@ package dev.plex.util;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.reflect.ClassPath;
|
||||
import dev.plex.Plex;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
@ -2,6 +2,7 @@ package dev.plex;
|
||||
|
||||
import dev.plex.admin.Admin;
|
||||
import dev.plex.admin.AdminList;
|
||||
import dev.plex.api.plugin.PlexPlugin;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.cache.PlayerCache;
|
||||
import dev.plex.config.Config;
|
||||
@ -10,7 +11,6 @@ import dev.plex.handlers.ListenerHandler;
|
||||
import dev.plex.listener.impl.ChatListener;
|
||||
import dev.plex.module.ModuleManager;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.api.plugin.PlexPlugin;
|
||||
import dev.plex.punishment.PunishmentManager;
|
||||
import dev.plex.rank.RankManager;
|
||||
import dev.plex.services.ServiceManager;
|
||||
@ -23,7 +23,11 @@ import dev.plex.storage.player.MongoPlayerData;
|
||||
import dev.plex.storage.player.SQLPlayerData;
|
||||
import dev.plex.storage.punishment.SQLNotes;
|
||||
import dev.plex.storage.punishment.SQLPunishment;
|
||||
import dev.plex.util.*;
|
||||
import dev.plex.util.BuildInfo;
|
||||
import dev.plex.util.BungeeUtil;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.UpdateChecker;
|
||||
import dev.plex.world.CustomWorld;
|
||||
import java.io.File;
|
||||
import lombok.Getter;
|
||||
|
@ -3,7 +3,6 @@ package dev.plex.cache;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.storage.StorageType;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -88,7 +88,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
|
||||
getMap().register("plex", this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public PlexCommand()
|
||||
{
|
||||
this(true);
|
||||
|
@ -1,6 +1,9 @@
|
||||
package dev.plex.command.impl;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import dev.plex.api.event.AdminAddEvent;
|
||||
import dev.plex.api.event.AdminRemoveEvent;
|
||||
import dev.plex.api.event.AdminSetRankEvent;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
@ -9,9 +12,6 @@ import dev.plex.command.annotation.System;
|
||||
import dev.plex.command.exception.ConsoleOnlyException;
|
||||
import dev.plex.command.exception.PlayerNotFoundException;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.api.event.AdminAddEvent;
|
||||
import dev.plex.api.event.AdminRemoveEvent;
|
||||
import dev.plex.api.event.AdminSetRankEvent;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
|
@ -1,12 +1,12 @@
|
||||
package dev.plex.command.impl;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.exception.CommandFailException;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.List;
|
||||
|
@ -1,12 +1,12 @@
|
||||
package dev.plex.command.impl;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.exception.CommandFailException;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.List;
|
||||
|
@ -9,11 +9,10 @@ import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.TimeUtils;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import dev.plex.util.TimeUtils;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -1,11 +1,11 @@
|
||||
package dev.plex.command.impl;
|
||||
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.exception.CommandFailException;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.Arrays;
|
||||
|
@ -14,6 +14,9 @@ import dev.plex.util.BungeeUtil;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.TimeUtils;
|
||||
import dev.plex.util.WebUtils;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -22,10 +25,6 @@ import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
@CommandParameters(name = "kick", description = "Kicks a player", usage = "/<command> <player>")
|
||||
@CommandPermissions(level = Rank.ADMIN, permission = "plex.kick", source = RequiredCommandSource.ANY)
|
||||
public class KickCMD extends PlexCommand
|
||||
|
@ -9,11 +9,10 @@ import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.TimeUtils;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import dev.plex.util.TimeUtils;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -9,12 +9,11 @@ import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.TimeUtils;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import dev.plex.util.TimeUtils;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.title.Title;
|
||||
|
@ -1,12 +1,12 @@
|
||||
package dev.plex.command.impl;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.exception.CommandFailException;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.List;
|
||||
|
@ -1,12 +1,12 @@
|
||||
package dev.plex.command.impl;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.exception.CommandFailException;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.api.event.GameModeUpdateEvent;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.List;
|
||||
|
@ -1,9 +1,9 @@
|
||||
package dev.plex.listener.impl;
|
||||
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.api.event.AdminAddEvent;
|
||||
import dev.plex.api.event.AdminRemoveEvent;
|
||||
import dev.plex.api.event.AdminSetRankEvent;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.listener.PlexListener;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
@ -17,7 +17,7 @@ public class AdminListener extends PlexListener
|
||||
public void onAdminAdd(AdminAddEvent event)
|
||||
{
|
||||
String userSender = event.getSender().getName();
|
||||
PlexPlayer target = (PlexPlayer) event.getPlexPlayer();
|
||||
PlexPlayer target = (PlexPlayer)event.getPlexPlayer();
|
||||
if (target.getRankFromString().isAtLeast(Rank.ADMIN))
|
||||
{
|
||||
PlexUtils.broadcast(messageComponent("adminReadded", userSender, target.getName(), target.getRankFromString().getReadable()));
|
||||
@ -35,7 +35,7 @@ public class AdminListener extends PlexListener
|
||||
public void onAdminRemove(AdminRemoveEvent event)
|
||||
{
|
||||
String userSender = event.getSender().getName();
|
||||
PlexPlayer target = (PlexPlayer) event.getPlexPlayer();
|
||||
PlexPlayer target = (PlexPlayer)event.getPlexPlayer();
|
||||
target.setAdminActive(false);
|
||||
DataUtils.update(target);
|
||||
PlexUtils.broadcast(messageComponent("adminRemoved", userSender, target.getName()));
|
||||
@ -45,8 +45,8 @@ public class AdminListener extends PlexListener
|
||||
public void onAdminSetRank(AdminSetRankEvent event)
|
||||
{
|
||||
String userSender = event.getSender().getName();
|
||||
PlexPlayer target = (PlexPlayer) event.getPlexPlayer();
|
||||
Rank newRank = (Rank) event.getRank();
|
||||
PlexPlayer target = (PlexPlayer)event.getPlexPlayer();
|
||||
Rank newRank = (Rank)event.getRank();
|
||||
target.setRank(newRank.name().toLowerCase());
|
||||
DataUtils.update(target);
|
||||
PlexUtils.broadcast(messageComponent("adminSetRank", userSender, target.getName(), newRank.getReadable()));
|
||||
|
@ -5,7 +5,6 @@ import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.minimessage.SafeMiniMessage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.kyori.adventure.inventory.Book;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
|
@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class ChatListener extends PlexListener
|
||||
{
|
||||
private final static TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig.builder().match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]").replacement((matchResult, builder) -> Component.empty().content(matchResult.group()).clickEvent(ClickEvent.openUrl(matchResult.group()))).build();
|
||||
|
||||
@EventHandler
|
||||
public void onChat(AsyncChatEvent event)
|
||||
{
|
||||
@ -34,6 +35,7 @@ public class ChatListener extends PlexListener
|
||||
public static class ChatHandlerImpl implements IChatHandler
|
||||
{
|
||||
private final PlexChatRenderer renderer = new PlexChatRenderer();
|
||||
|
||||
@Override
|
||||
public void doChat(AsyncChatEvent event)
|
||||
{
|
||||
@ -44,7 +46,8 @@ public class ChatListener extends PlexListener
|
||||
{
|
||||
renderer.hasPrefix = true;
|
||||
renderer.prefix = prefix;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.hasPrefix = false;
|
||||
renderer.prefix = null;
|
||||
|
@ -1,9 +1,9 @@
|
||||
package dev.plex.listener.impl;
|
||||
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.api.event.AdminAddEvent;
|
||||
import dev.plex.api.event.AdminRemoveEvent;
|
||||
import dev.plex.api.event.AdminSetRankEvent;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.listener.PlexListener;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -25,7 +25,7 @@ public class TabListener extends PlexListener
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onAdminAdd(AdminAddEvent event)
|
||||
{
|
||||
PlexPlayer plexPlayer = (PlexPlayer) event.getPlexPlayer();
|
||||
PlexPlayer plexPlayer = (PlexPlayer)event.getPlexPlayer();
|
||||
Player player = event.getPlexPlayer().getPlayer();
|
||||
if (player == null)
|
||||
{
|
||||
@ -37,7 +37,7 @@ public class TabListener extends PlexListener
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onAdminRemove(AdminRemoveEvent event)
|
||||
{
|
||||
PlexPlayer plexPlayer = (PlexPlayer) event.getPlexPlayer();
|
||||
PlexPlayer plexPlayer = (PlexPlayer)event.getPlexPlayer();
|
||||
Player player = event.getPlexPlayer().getPlayer();
|
||||
if (player == null)
|
||||
{
|
||||
@ -49,7 +49,7 @@ public class TabListener extends PlexListener
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onAdminSetRank(AdminSetRankEvent event)
|
||||
{
|
||||
PlexPlayer plexPlayer = (PlexPlayer) event.getPlexPlayer();
|
||||
PlexPlayer plexPlayer = (PlexPlayer)event.getPlexPlayer();
|
||||
Player player = event.getPlexPlayer().getPlayer();
|
||||
if (player == null)
|
||||
{
|
||||
|
@ -21,7 +21,7 @@ import org.bukkit.inventory.meta.SkullMeta;
|
||||
public class PunishmentMenu extends AbstractMenu
|
||||
{
|
||||
|
||||
private List<Inventory> inventories = Lists.newArrayList();
|
||||
private final List<Inventory> inventories = Lists.newArrayList();
|
||||
|
||||
public PunishmentMenu()
|
||||
{
|
||||
|
@ -17,7 +17,6 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
@Getter
|
||||
@ -124,7 +123,8 @@ public class ModuleManager
|
||||
try
|
||||
{
|
||||
Plex.get().getServer().getCommandMap().getCommand(plexCommand.getName()).unregister(Plex.get().getServer().getCommandMap());
|
||||
} catch (Exception ignored)
|
||||
}
|
||||
catch (Exception ignored)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -14,6 +14,10 @@ import dev.plex.punishment.extra.Note;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.storage.StorageType;
|
||||
import dev.plex.util.adapter.ZonedDateTimeSerializer;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@ -23,11 +27,6 @@ import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity(value = "players", useDiscriminator = false)
|
||||
@ -119,11 +118,13 @@ public class PlexPlayer implements IPlexPlayer
|
||||
if (player.isOp())
|
||||
{
|
||||
return Rank.OP;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
return Rank.NONOP;
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
return Rank.valueOf(rank.toUpperCase());
|
||||
}
|
||||
|
@ -6,11 +6,10 @@ import dev.plex.Plex;
|
||||
import dev.plex.util.MojangUtils;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.TimeUtils;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
import dev.plex.util.adapter.ZonedDateTimeDeserializer;
|
||||
import dev.plex.util.adapter.ZonedDateTimeSerializer;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
@ -10,6 +10,7 @@ import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.storage.StorageType;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.TimeUtils;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -21,8 +22,6 @@ import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import dev.plex.util.TimeUtils;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
@ -2,10 +2,9 @@ package dev.plex.punishment.extra;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.plex.util.adapter.ZonedDateTimeSerializer;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
import dev.plex.util.adapter.ZonedDateTimeSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
|
@ -30,7 +30,7 @@ public enum Rank implements IRank
|
||||
private String prefix;
|
||||
|
||||
@Getter
|
||||
private NamedTextColor color;
|
||||
private final NamedTextColor color;
|
||||
|
||||
Rank(int level, String loginMessage, String readable, String prefix, NamedTextColor color)
|
||||
{
|
||||
|
@ -26,7 +26,7 @@ public enum Title
|
||||
private String prefix;
|
||||
|
||||
@Getter
|
||||
private NamedTextColor color;
|
||||
private final NamedTextColor color;
|
||||
|
||||
Title(int level, String loginMessage, String readable, String prefix, NamedTextColor color)
|
||||
{
|
||||
|
@ -7,8 +7,8 @@ import lombok.Setter;
|
||||
@Getter
|
||||
public abstract class AbstractService implements IService, PlexBase
|
||||
{
|
||||
private boolean asynchronous;
|
||||
private boolean repeating;
|
||||
private final boolean asynchronous;
|
||||
private final boolean repeating;
|
||||
|
||||
@Setter
|
||||
private int taskId;
|
||||
|
@ -7,7 +7,6 @@ import dev.plex.cache.PlayerCache;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.storage.StorageType;
|
||||
import dev.plex.util.PlexLog;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -38,7 +37,8 @@ public class SQLPlayerData
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
return set.next();
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -53,7 +53,8 @@ public class SQLPlayerData
|
||||
statement.setString(1, username);
|
||||
ResultSet set = statement.executeQuery();
|
||||
return set.next();
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -104,7 +105,8 @@ public class SQLPlayerData
|
||||
plexPlayer.setCommandSpy(commandspy);
|
||||
}
|
||||
return plexPlayer;
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -153,7 +155,8 @@ public class SQLPlayerData
|
||||
return plexPlayer;
|
||||
}
|
||||
return null;
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -216,11 +219,13 @@ public class SQLPlayerData
|
||||
plexPlayer.setCommandSpy(commandspy);
|
||||
}
|
||||
return plexPlayer;
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
} else if (Plex.get().getStorageType() == StorageType.SQLITE)
|
||||
}
|
||||
else if (Plex.get().getStorageType() == StorageType.SQLITE)
|
||||
{
|
||||
PlexLog.warn("Querying a user by IP running SQLite can cause performance issues! Please try to switch to a remote DB ASAP!");
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
@ -260,7 +265,8 @@ public class SQLPlayerData
|
||||
plexPlayer.setCommandSpy(commandspy);
|
||||
}
|
||||
return plexPlayer;
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -290,7 +296,8 @@ public class SQLPlayerData
|
||||
statement.setBoolean(9, player.isCommandSpy());
|
||||
statement.setString(10, player.getUuid().toString());
|
||||
statement.executeUpdate();
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -318,7 +325,8 @@ public class SQLPlayerData
|
||||
statement.setBoolean(9, player.isVanished());
|
||||
statement.setBoolean(10, player.isCommandSpy());
|
||||
statement.execute();
|
||||
} catch (SQLException throwables)
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
|
@ -4,12 +4,13 @@ import com.google.common.collect.Lists;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.punishment.extra.Note;
|
||||
import dev.plex.util.TimeUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.*;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
@ -6,7 +6,6 @@ import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.TimeUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -47,7 +46,8 @@ public class SQLPunishment
|
||||
punishment.setIp(set.getString("ip"));
|
||||
punishments.add(punishment);
|
||||
}
|
||||
} catch (SQLException e)
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return punishments;
|
||||
@ -76,7 +76,8 @@ public class SQLPunishment
|
||||
punishment.setIp(set.getString("ip"));
|
||||
punishments.add(punishment);
|
||||
}
|
||||
} catch (SQLException e)
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -103,7 +104,8 @@ public class SQLPunishment
|
||||
statement.setLong(9, punishment.getEndDate().toInstant().toEpochMilli());
|
||||
PlexLog.debug("Executing punishment");
|
||||
statement.execute();
|
||||
} catch (SQLException e)
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -126,7 +128,8 @@ public class SQLPunishment
|
||||
statement1.setString(3, uuid.toString());
|
||||
statement1.setString(4, PunishmentType.TEMPBAN.name());
|
||||
statement1.executeUpdate();
|
||||
} catch (SQLException e)
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -151,7 +154,8 @@ public class SQLPunishment
|
||||
statement1.setString(3, uuid.toString());
|
||||
statement1.setString(4, PunishmentType.TEMPBAN.name());
|
||||
statement1.executeUpdate();
|
||||
} catch (SQLException e)
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
@ -2,6 +2,7 @@ package dev.plex.util;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import dev.plex.Plex;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
@ -9,8 +10,6 @@ import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
@ -4,6 +4,14 @@ import dev.plex.Plex;
|
||||
import dev.plex.PlexBase;
|
||||
import dev.plex.cache.PlayerCache;
|
||||
import dev.plex.storage.StorageType;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Month;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
@ -17,15 +25,6 @@ import org.bukkit.command.PluginCommandYamlParser;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Month;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlexUtils implements PlexBase
|
||||
{
|
||||
private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();
|
||||
@ -71,18 +70,21 @@ public class PlexUtils implements PlexBase
|
||||
if (Plex.get().getStorageType() == StorageType.MARIADB)
|
||||
{
|
||||
PlexLog.log("Successfully enabled MySQL!");
|
||||
} else if (Plex.get().getStorageType() == StorageType.SQLITE)
|
||||
}
|
||||
else if (Plex.get().getStorageType() == StorageType.SQLITE)
|
||||
{
|
||||
PlexLog.log("Successfully enabled SQLite!");
|
||||
}
|
||||
} catch (SQLException e)
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
if (Plex.get().getMongoConnection().getDatastore() != null)
|
||||
{
|
||||
PlexLog.log("Successfully enabled MongoDB!");
|
||||
}
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Plex.get().getMongoConnection().getDatastore() != null)
|
||||
{
|
||||
@ -172,8 +174,9 @@ public class PlexUtils implements PlexBase
|
||||
{
|
||||
try
|
||||
{
|
||||
return ((TextComponent) component).content();
|
||||
} catch (Exception e)
|
||||
return ((TextComponent)component).content();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
PlexLog.warn("Unable to get text of component", e.getLocalizedMessage());
|
||||
return "";
|
||||
@ -192,7 +195,8 @@ public class PlexUtils implements PlexBase
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
} catch (Exception e)
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
PlexLog.warn("Unable to get text of components", e.getLocalizedMessage());
|
||||
return "";
|
||||
|
@ -5,7 +5,6 @@ import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import dev.plex.Plex;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
@ -13,7 +12,8 @@ import java.time.ZonedDateTime;
|
||||
|
||||
public class ZonedDateTimeDeserializer implements JsonDeserializer<ZonedDateTime>
|
||||
{
|
||||
private static String TIMEZONE = Plex.get().config.getString("server.timezone");
|
||||
private static final String TIMEZONE = Plex.get().config.getString("server.timezone");
|
||||
|
||||
@Override
|
||||
public ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
|
@ -5,13 +5,13 @@ import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import dev.plex.Plex;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
public class ZonedDateTimeSerializer implements JsonSerializer<ZonedDateTime>
|
||||
{
|
||||
private static String TIMEZONE = Plex.get().config.getString("server.timezone");
|
||||
private static final String TIMEZONE = Plex.get().config.getString("server.timezone");
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ZonedDateTime src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
|
@ -5,7 +5,7 @@ import org.bukkit.event.Listener;
|
||||
|
||||
public abstract class AbstractMenu implements Listener
|
||||
{
|
||||
private String name;
|
||||
private final String name;
|
||||
|
||||
public AbstractMenu(String name)
|
||||
{
|
||||
|
@ -1,6 +1,7 @@
|
||||
package dev.plex.util.minimessage;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.List;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.Context;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
@ -11,8 +12,6 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SafeMiniMessage
|
||||
{
|
||||
public static final MiniMessage MINI_MESSAGE = MiniMessage.builder().tags(new SafeMiniMessageTagResolver()).build();
|
||||
|
Loading…
Reference in New Issue
Block a user