mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-20 15:04:28 +00:00
Improved comment handling
This commit is contained in:
parent
055480ee0b
commit
329b56e7d3
2 changed files with 71 additions and 27 deletions
|
@ -72,14 +72,15 @@ public class RegexParser {
|
|||
if (!multiline && MULTILINE_ARRAY_REGEX.matcher(pair[1].trim()).matches()) {
|
||||
multiline = true;
|
||||
key = pair[0].trim();
|
||||
multilineBuilder.append(pair[1].trim());
|
||||
multilineBuilder.append(removeComment(pair[1]));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (multiline) {
|
||||
multilineBuilder.append(line);
|
||||
if (MULTILINE_ARRAY_REGEX_END.matcher(line).matches()) {
|
||||
String lineWithoutComment = removeComment(line);
|
||||
multilineBuilder.append(lineWithoutComment);
|
||||
if (MULTILINE_ARRAY_REGEX_END.matcher(lineWithoutComment).matches()) {
|
||||
multiline = false;
|
||||
value = multilineBuilder.toString();
|
||||
multilineBuilder.delete(0, multilineBuilder.length() - 1);
|
||||
|
@ -96,7 +97,7 @@ public class RegexParser {
|
|||
continue;
|
||||
}
|
||||
|
||||
ValueAnalysis lineAnalysis = new ValueAnalysis(value.trim());
|
||||
ValueAnalysis lineAnalysis = new ValueAnalysis(value);
|
||||
|
||||
Object convertedValue = lineAnalysis.getValue();
|
||||
|
||||
|
@ -143,4 +144,22 @@ public class RegexParser {
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String removeComment(String line) {
|
||||
line = line.trim();
|
||||
if (line.startsWith("\"")) {
|
||||
int startOfComment = line.indexOf('#', line.lastIndexOf('"'));
|
||||
if (startOfComment > -1) {
|
||||
return line.substring(0, startOfComment - 1).trim();
|
||||
}
|
||||
} else {
|
||||
int startOfComment = line.indexOf('#');
|
||||
if (startOfComment > -1) {
|
||||
return line.substring(0, startOfComment - 1).trim();
|
||||
}
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,12 +16,13 @@ class ValueAnalysis {
|
|||
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)(.*)");
|
||||
private static final Pattern LIST_REGEX = Pattern.compile("(\\[(.*)\\])(.*)");
|
||||
private static final Pattern UNICODE_REGEX = Pattern.compile("\\\\u(.*)");
|
||||
private static final Pattern RESERVED_CHARACTER_REGEX = Pattern.compile("\\\\[^bfntr\"/\\\\]");
|
||||
|
||||
private final String rawValue;
|
||||
private Matcher chosenMatcher;
|
||||
|
||||
public ValueAnalysis(String value) {
|
||||
this.rawValue = value;
|
||||
this.rawValue = value.trim();
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
|
@ -30,7 +31,7 @@ class ValueAnalysis {
|
|||
|
||||
private Object convert(String value) {
|
||||
if (isString(value)) {
|
||||
return convertString(chosenMatcher.group(1));
|
||||
return convertString(value);
|
||||
} else if (isInteger(value)) {
|
||||
return Long.valueOf(chosenMatcher.group(1));
|
||||
} else if (isFloat(value)) {
|
||||
|
@ -67,15 +68,7 @@ class ValueAnalysis {
|
|||
}
|
||||
|
||||
private boolean isString(String value) {
|
||||
Matcher matcher = STRING_REGEX.matcher(value);
|
||||
if (matcher.matches()) {
|
||||
if (isComment(matcher.group(2))) {
|
||||
chosenMatcher = matcher;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return value.startsWith("\"");
|
||||
}
|
||||
|
||||
private boolean isFloat(String value) {
|
||||
|
@ -203,13 +196,53 @@ class ValueAnalysis {
|
|||
}
|
||||
|
||||
private Object convertString(String value) {
|
||||
Matcher matcher = UNICODE_REGEX.matcher(value);
|
||||
int stringTerminator = -1;
|
||||
int startOfComment = -1;
|
||||
char[] chars = value.toCharArray();
|
||||
|
||||
while (matcher.find()) {
|
||||
value = value.replace(matcher.group(), new String(Character.toChars(Integer.parseInt(matcher.group().substring(2), 16))));
|
||||
for (int i = 1; i < chars.length; i++) {
|
||||
char ch = chars[i];
|
||||
if (ch == '"' && chars[i - 1] != '\\') {
|
||||
stringTerminator = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
value = value.replace("\\n", "\n")
|
||||
if (stringTerminator == -1) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
value = value.substring(1, stringTerminator);
|
||||
value = replaceUnicodeCharacters(value);
|
||||
|
||||
chars = value.toCharArray();
|
||||
for (int i = 0; i < chars.length - 1; i++) {
|
||||
char ch = chars[i];
|
||||
char next = chars[i + 1];
|
||||
|
||||
if (ch == '\\' && next == '\\') {
|
||||
i++;
|
||||
} else if (ch == '\\' && !(next == 'b' || next == 'f' || next == 'n' || next == 't' || next == 'r' || next == '"' || next == '/' || next == '\\')) {
|
||||
return INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
value = replaceSpecialCharacters(value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private String replaceUnicodeCharacters(String value) {
|
||||
Matcher unicodeMatcher = UNICODE_REGEX.matcher(value);
|
||||
|
||||
while (unicodeMatcher.find()) {
|
||||
value = value.replace(unicodeMatcher.group(), new String(Character.toChars(Integer.parseInt(unicodeMatcher.group(1), 16))));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private String replaceSpecialCharacters(String value) {
|
||||
return value.replace("\\n", "\n")
|
||||
.replace("\\\"", "\"")
|
||||
.replace("\\t", "\t")
|
||||
.replace("\\r", "\r")
|
||||
|
@ -217,13 +250,5 @@ class ValueAnalysis {
|
|||
.replace("\\/", "/")
|
||||
.replace("\\b", "\b")
|
||||
.replace("\\f", "\f");
|
||||
|
||||
if (value.contains("\\")) {
|
||||
// results.errors.append(sc + " is a reserved special character and cannot be used!\n");
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue