mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-06-01 01:11:36 +00:00
Renamed ValueParser to ValueConverter and associated changes
This commit is contained in:
parent
c841c4e1fe
commit
697349e9a2
|
@ -1,6 +1,6 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -9,20 +9,20 @@ import org.parboiled.Parboiled;
|
|||
import org.parboiled.parserunners.BasicParseRunner;
|
||||
import org.parboiled.support.ParsingResult;
|
||||
|
||||
class ArrayParser implements ValueParser {
|
||||
class ArrayConverter implements ValueConverter {
|
||||
|
||||
static final ArrayParser ARRAY_PARSER = new ArrayParser();
|
||||
static final ArrayConverter ARRAY_PARSER = new ArrayConverter();
|
||||
|
||||
private static final List<Object> INVALID_ARRAY = new ArrayList<Object>();
|
||||
private static final ValueConverter VALUE_ANALYSIS = new ValueConverter();
|
||||
private static final ValueConverters VALUE_ANALYSIS = new ValueConverters();
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
public boolean canConvert(String s) {
|
||||
return s.startsWith("[");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
public Object convert(String s) {
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
ParsingResult<List<Object>> parsingResult = new BasicParseRunner<List<Object>>(parser.Array()).run(s);
|
||||
List<Object> tokens = parsingResult.resultValue;
|
||||
|
@ -67,5 +67,5 @@ class ArrayParser implements ValueParser {
|
|||
return values.isEmpty() || values.get(0).getClass().isAssignableFrom(o.getClass()) || o.getClass().isAssignableFrom(values.get(0).getClass());
|
||||
}
|
||||
|
||||
private ArrayParser() {}
|
||||
private ArrayConverter() {}
|
||||
}
|
|
@ -1,23 +1,23 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.parboiled.Parboiled;
|
||||
import org.parboiled.parserunners.BasicParseRunner;
|
||||
|
||||
class BooleanParser implements ValueParser {
|
||||
class BooleanConverter implements ValueConverter {
|
||||
|
||||
static final BooleanParser BOOLEAN_PARSER = new BooleanParser();
|
||||
static final BooleanConverter BOOLEAN_PARSER = new BooleanConverter();
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
public boolean canConvert(String s) {
|
||||
return s.startsWith("true") || s.startsWith("false");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
public Object convert(String s) {
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
|
||||
List<String> resultValue = new BasicParseRunner<List<String>>(parser.Boolean()).run(s).resultValue;
|
||||
|
@ -29,5 +29,5 @@ class BooleanParser implements ValueParser {
|
|||
return Boolean.valueOf(resultValue.get(0));
|
||||
}
|
||||
|
||||
private BooleanParser() {}
|
||||
private BooleanConverter() {}
|
||||
}
|
|
@ -1,25 +1,25 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class DateParser implements ValueParser {
|
||||
class DateConverter implements ValueConverter {
|
||||
|
||||
static final DateParser DATE_PARSER = new DateParser();
|
||||
static final DateConverter DATE_PARSER = new DateConverter();
|
||||
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]Z)(.*)");
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
public boolean canConvert(String s) {
|
||||
Matcher matcher = DATE_REGEX.matcher(s);
|
||||
|
||||
return matcher.matches() && ValueParserUtils.isComment(matcher.group(2));
|
||||
return matcher.matches() && ValueConverterUtils.isComment(matcher.group(2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
public Object convert(String s) {
|
||||
Matcher matcher = DATE_REGEX.matcher(s);
|
||||
matcher.matches();
|
||||
s = matcher.group(1).replace("Z", "+00:00");
|
||||
|
@ -33,5 +33,5 @@ class DateParser implements ValueParser {
|
|||
}
|
||||
}
|
||||
|
||||
private DateParser() {}
|
||||
private DateConverter() {}
|
||||
}
|
|
@ -3,26 +3,26 @@ package com.moandjiezana.toml;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class FloatParser implements ValueParser {
|
||||
class FloatConverter implements ValueConverter {
|
||||
|
||||
public static final FloatParser FLOAT_PARSER = new FloatParser();
|
||||
public static final FloatConverter FLOAT_PARSER = new FloatConverter();
|
||||
private static final Pattern FLOAT_REGEX = Pattern.compile("(-?\\d+\\.\\d+)(.*)");
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
public boolean canConvert(String s) {
|
||||
Matcher matcher = FLOAT_REGEX.matcher(s);
|
||||
|
||||
return matcher.matches() && ValueParserUtils.isComment(matcher.group(2));
|
||||
return matcher.matches() && ValueConverterUtils.isComment(matcher.group(2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
public Object convert(String s) {
|
||||
Matcher matcher = FLOAT_REGEX.matcher(s);
|
||||
matcher.matches();
|
||||
|
||||
return Double.valueOf(matcher.group(1));
|
||||
}
|
||||
|
||||
private FloatParser() {}
|
||||
private FloatConverter() {}
|
||||
|
||||
}
|
|
@ -5,26 +5,26 @@ import java.util.List;
|
|||
import org.parboiled.Parboiled;
|
||||
import org.parboiled.parserunners.BasicParseRunner;
|
||||
|
||||
class IntegerParser implements ValueParser {
|
||||
static final IntegerParser INTEGER_PARSER = new IntegerParser();
|
||||
class IntegerConverter implements ValueConverter {
|
||||
static final IntegerConverter INTEGER_PARSER = new IntegerConverter();
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
public boolean canConvert(String s) {
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
return new BasicParseRunner<Object>(parser.Integer()).run(s).resultValue != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
public Object convert(String s) {
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
List<String> resultValue = new BasicParseRunner<List<String>>(parser.Integer()).run(s).resultValue;
|
||||
|
||||
if (resultValue == null) {
|
||||
return ValueParserUtils.INVALID;
|
||||
return ValueConverterUtils.INVALID;
|
||||
}
|
||||
|
||||
return Long.valueOf(resultValue.get(0));
|
||||
}
|
||||
|
||||
private IntegerParser() {}
|
||||
private IntegerConverter() {}
|
||||
}
|
|
@ -5,26 +5,26 @@ import java.util.List;
|
|||
import org.parboiled.Parboiled;
|
||||
import org.parboiled.parserunners.BasicParseRunner;
|
||||
|
||||
class LiteralStringParser implements ValueParser {
|
||||
class LiteralStringConverter implements ValueConverter {
|
||||
|
||||
static final LiteralStringParser LITERAL_STRING_PARSER = new LiteralStringParser();
|
||||
static final LiteralStringConverter LITERAL_STRING_PARSER = new LiteralStringConverter();
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
public boolean canConvert(String s) {
|
||||
return s.startsWith("'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
public Object convert(String s) {
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
List<Object> resultValue = new BasicParseRunner<List<Object>>(parser.LiteralString()).run(s).resultValue;
|
||||
|
||||
if (resultValue == null) {
|
||||
return ValueParserUtils.INVALID;
|
||||
return ValueConverterUtils.INVALID;
|
||||
}
|
||||
|
||||
return resultValue.get(0);
|
||||
}
|
||||
|
||||
private LiteralStringParser() {}
|
||||
private LiteralStringConverter() {}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
||||
|
||||
class MultilineStringConverter implements ValueConverter {
|
||||
|
||||
static final MultilineStringConverter MULTILINE_STRING_PARSER = new MultilineStringConverter();
|
||||
|
||||
@Override
|
||||
public boolean canConvert(String s) {
|
||||
return s.startsWith("\"\"\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(String s) {
|
||||
int terminator = s.indexOf("\"\"\"", 3);
|
||||
|
||||
if (terminator == -1) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
if (!ValueConverterUtils.isComment(s.substring(terminator + 3))) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
s = s.substring(2, terminator + 1);
|
||||
s = s.replaceAll("\\\\\\s+", "");
|
||||
|
||||
return StringConverter.STRING_PARSER.convert(s);
|
||||
}
|
||||
|
||||
private MultilineStringConverter() {}
|
||||
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
|
||||
class MultilineStringParser implements ValueParser {
|
||||
|
||||
static final MultilineStringParser MULTILINE_STRING_PARSER = new MultilineStringParser();
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
return s.startsWith("\"\"\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
int terminator = s.indexOf("\"\"\"", 3);
|
||||
|
||||
if (terminator == -1) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
if (!ValueParserUtils.isComment(s.substring(terminator + 3))) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
s = s.substring(2, terminator + 1);
|
||||
s = s.replaceAll("\\\\\\s+", "");
|
||||
|
||||
return StringParser.STRING_PARSER.parse(s);
|
||||
}
|
||||
|
||||
private MultilineStringParser() {}
|
||||
|
||||
}
|
|
@ -1,23 +1,23 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
import static com.moandjiezana.toml.ValueParserUtils.isComment;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.isComment;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class StringParser implements ValueParser {
|
||||
class StringConverter implements ValueConverter {
|
||||
|
||||
static final StringParser STRING_PARSER = new StringParser();
|
||||
static final StringConverter STRING_PARSER = new StringConverter();
|
||||
private static final Pattern UNICODE_REGEX = Pattern.compile("\\\\u(.*)");
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
public boolean canConvert(String s) {
|
||||
return s.startsWith("\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String value) {
|
||||
public Object convert(String value) {
|
||||
int stringTerminator = -1;
|
||||
char[] chars = value.toCharArray();
|
||||
|
||||
|
@ -73,5 +73,5 @@ class StringParser implements ValueParser {
|
|||
.replace("\\f", "\f");
|
||||
}
|
||||
|
||||
private StringParser() {}
|
||||
private StringConverter() {}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -12,7 +12,7 @@ import org.parboiled.support.ParsingResult;
|
|||
class TomlParser {
|
||||
private static final Pattern MULTILINE_ARRAY_REGEX = Pattern.compile("\\s*\\[([^\\]]*)");
|
||||
private static final Pattern MULTILINE_ARRAY_REGEX_END = Pattern.compile("\\s*\\]");
|
||||
private static final ValueConverter VALUE_ANALYSIS = new ValueConverter();
|
||||
private static final ValueConverters VALUE_ANALYSIS = new ValueConverters();
|
||||
|
||||
private final Results results = new Results();
|
||||
|
||||
|
|
|
@ -1,28 +1,14 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ArrayParser.ARRAY_PARSER;
|
||||
import static com.moandjiezana.toml.BooleanParser.BOOLEAN_PARSER;
|
||||
import static com.moandjiezana.toml.DateParser.DATE_PARSER;
|
||||
import static com.moandjiezana.toml.FloatParser.FLOAT_PARSER;
|
||||
import static com.moandjiezana.toml.IntegerParser.INTEGER_PARSER;
|
||||
import static com.moandjiezana.toml.LiteralStringParser.LITERAL_STRING_PARSER;
|
||||
import static com.moandjiezana.toml.MultilineStringParser.MULTILINE_STRING_PARSER;
|
||||
import static com.moandjiezana.toml.StringParser.STRING_PARSER;
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
interface ValueConverter {
|
||||
|
||||
class ValueConverter {
|
||||
/**
|
||||
* @param s must already have been trimmed
|
||||
*/
|
||||
boolean canConvert(String s);
|
||||
|
||||
private static final ValueParser[] PARSERS = {
|
||||
MULTILINE_STRING_PARSER, LITERAL_STRING_PARSER, STRING_PARSER, DATE_PARSER, INTEGER_PARSER, FLOAT_PARSER, BOOLEAN_PARSER, ARRAY_PARSER
|
||||
};
|
||||
|
||||
public Object convert(String value) {
|
||||
for (ValueParser valueParser : PARSERS) {
|
||||
if (valueParser.canParse(value)) {
|
||||
return valueParser.parse(value);
|
||||
}
|
||||
}
|
||||
|
||||
return INVALID;
|
||||
}
|
||||
/**
|
||||
* @param s must already have been validated by {@link #canConvert(String)}
|
||||
*/
|
||||
Object convert(String s);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
class ValueParserUtils {
|
||||
class ValueConverterUtils {
|
||||
static final Object INVALID = new Object();
|
||||
|
||||
static boolean isComment(String line) {
|
||||
|
@ -21,5 +21,5 @@ class ValueParserUtils {
|
|||
return false;
|
||||
}
|
||||
|
||||
private ValueParserUtils() {}
|
||||
private ValueConverterUtils() {}
|
||||
}
|
28
src/main/java/com/moandjiezana/toml/ValueConverters.java
Normal file
28
src/main/java/com/moandjiezana/toml/ValueConverters.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ArrayConverter.ARRAY_PARSER;
|
||||
import static com.moandjiezana.toml.BooleanConverter.BOOLEAN_PARSER;
|
||||
import static com.moandjiezana.toml.DateConverter.DATE_PARSER;
|
||||
import static com.moandjiezana.toml.FloatConverter.FLOAT_PARSER;
|
||||
import static com.moandjiezana.toml.IntegerConverter.INTEGER_PARSER;
|
||||
import static com.moandjiezana.toml.LiteralStringConverter.LITERAL_STRING_PARSER;
|
||||
import static com.moandjiezana.toml.MultilineStringConverter.MULTILINE_STRING_PARSER;
|
||||
import static com.moandjiezana.toml.StringConverter.STRING_PARSER;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
||||
|
||||
class ValueConverters {
|
||||
|
||||
private static final ValueConverter[] PARSERS = {
|
||||
MULTILINE_STRING_PARSER, LITERAL_STRING_PARSER, STRING_PARSER, DATE_PARSER, INTEGER_PARSER, FLOAT_PARSER, BOOLEAN_PARSER, ARRAY_PARSER
|
||||
};
|
||||
|
||||
public Object convert(String value) {
|
||||
for (ValueConverter valueParser : PARSERS) {
|
||||
if (valueParser.canConvert(value)) {
|
||||
return valueParser.convert(value);
|
||||
}
|
||||
}
|
||||
|
||||
return INVALID;
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
interface ValueParser {
|
||||
|
||||
/**
|
||||
* @param s must already have been trimmed
|
||||
*/
|
||||
boolean canParse(String s);
|
||||
|
||||
/**
|
||||
* @param s must already have been validated by {@link #canParse(String)}
|
||||
*/
|
||||
Object parse(String s);
|
||||
}
|
Loading…
Reference in a new issue