mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 19:50:29 +00:00
Added support for whitespace around table and table array name segments
This commit is contained in:
parent
0f198e5a5e
commit
0dc5f6d206
5 changed files with 69 additions and 10 deletions
|
@ -100,18 +100,36 @@ class Keys {
|
||||||
char[] chars = line.toCharArray();
|
char[] chars = line.toCharArray();
|
||||||
boolean quoted = false;
|
boolean quoted = false;
|
||||||
boolean terminated = false;
|
boolean terminated = false;
|
||||||
|
int endIndex = -1;
|
||||||
|
boolean preKey = true;
|
||||||
|
|
||||||
for (int i = 2; i < chars.length; i++) {
|
for (int i = 2; i < chars.length; i++) {
|
||||||
char c = chars[i];
|
char c = chars[i];
|
||||||
if (c == '"' && chars[i - 1] != '\\') {
|
if (c == '"' && chars[i - 1] != '\\') {
|
||||||
|
if (!quoted && i > 1 && chars [i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
quoted = !quoted;
|
quoted = !quoted;
|
||||||
} else if (!quoted && c == ']') {
|
} else if (!quoted && c == ']') {
|
||||||
if (chars.length > i + 1 && chars[i + 1] == ']') {
|
if (chars.length > i + 1 && chars[i + 1] == ']') {
|
||||||
terminated = true;
|
terminated = true;
|
||||||
|
endIndex = i + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if (!quoted && c == '.') {
|
||||||
|
preKey = true;
|
||||||
|
} else if (!quoted && Character.isWhitespace(c)) {
|
||||||
|
if (preKey && i > 2 && chars[i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!preKey && chars.length > i + 1 && chars[i + 1] != '.' && chars[i + 1] != ']' && !Character.isWhitespace(chars[i + 1])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
|
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
|
||||||
break;
|
break;
|
||||||
|
} else {
|
||||||
|
preKey = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
|
@ -119,12 +137,11 @@ class Keys {
|
||||||
|
|
||||||
String tableName = sb.toString();
|
String tableName = sb.toString();
|
||||||
|
|
||||||
if (!terminated || tableName.isEmpty() || !isComment(line.substring(tableName.length() + 4))) {
|
if (!terminated || tableName.isEmpty() || !isComment(line.substring(endIndex + 1))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName = StringConverter.STRING_PARSER.replaceUnicodeCharacters(tableName);
|
return StringConverter.STRING_PARSER.replaceUnicodeCharacters(tableName);
|
||||||
return tableName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,19 +153,34 @@ class Keys {
|
||||||
char[] chars = line.toCharArray();
|
char[] chars = line.toCharArray();
|
||||||
boolean quoted = false;
|
boolean quoted = false;
|
||||||
boolean terminated = false;
|
boolean terminated = false;
|
||||||
|
int endIndex = -1;
|
||||||
|
boolean preKey = true;
|
||||||
|
|
||||||
for (int i = 1; i < chars.length; i++) {
|
for (int i = 1; i < chars.length; i++) {
|
||||||
char c = chars[i];
|
char c = chars[i];
|
||||||
if (c == '"' && chars[i - 1] != '\\') {
|
if (c == '"' && chars[i - 1] != '\\') {
|
||||||
if (!quoted && i > 1 && chars [i - 1] != '.') {
|
if (!quoted && i > 1 && chars [i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
quoted = !quoted;
|
quoted = !quoted;
|
||||||
} else if (!quoted && c == ']') {
|
} else if (!quoted && c == ']') {
|
||||||
terminated = true;
|
terminated = true;
|
||||||
|
endIndex = i;
|
||||||
break;
|
break;
|
||||||
|
} else if (!quoted && c == '.') {
|
||||||
|
preKey = true;
|
||||||
|
} else if (!quoted && Character.isWhitespace(c)) {
|
||||||
|
if (preKey && i > 1 && chars[i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!preKey && chars.length > i + 1 && chars[i + 1] != '.' && chars[i + 1] != ']' && !Character.isWhitespace(chars[i + 1])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
|
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
|
||||||
break;
|
break;
|
||||||
|
} else if (!quoted) {
|
||||||
|
preKey = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
|
@ -156,12 +188,11 @@ class Keys {
|
||||||
|
|
||||||
String tableName = sb.toString();
|
String tableName = sb.toString();
|
||||||
|
|
||||||
if (!terminated || !isComment(line.substring(tableName.length() + 2))) {
|
if (!terminated || !isComment(line.substring(endIndex + 1))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName = StringConverter.STRING_PARSER.replaceUnicodeCharacters(tableName);
|
return StringConverter.STRING_PARSER.replaceUnicodeCharacters(tableName);
|
||||||
return tableName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Keys() {}
|
private Keys() {}
|
||||||
|
|
|
@ -29,9 +29,9 @@ class Results {
|
||||||
stack.pop();
|
stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] tableParts = tableName.split("\\.");
|
Keys.Key[] tableParts = Keys.split(tableName);
|
||||||
for (int i = 0; i < tableParts.length; i++) {
|
for (int i = 0; i < tableParts.length; i++) {
|
||||||
String tablePart = tableParts[i];
|
String tablePart = tableParts[i].name;
|
||||||
Container currentContainer = stack.peek();
|
Container currentContainer = stack.peek();
|
||||||
|
|
||||||
if (currentContainer.get(tablePart) instanceof Container.TableArray) {
|
if (currentContainer.get(tablePart) instanceof Container.TableArray) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.moandjiezana.toml;
|
package com.moandjiezana.toml;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
@ -9,6 +11,13 @@ public class BareKeysTest {
|
||||||
@Rule
|
@Rule
|
||||||
public final ExpectedException exception = ExpectedException.none();
|
public final ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_ignore_spaces_around_key_segments() throws Exception {
|
||||||
|
Toml toml = new Toml().parse("[ a . b . c ] \n key = \"a\"");
|
||||||
|
|
||||||
|
assertEquals("a", toml.getString("a.b.c.key"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_fail_when_characters_outside_accept_range_are_used_in_table_name() throws Exception {
|
public void should_fail_when_characters_outside_accept_range_are_used_in_table_name() throws Exception {
|
||||||
exception.expect(IllegalStateException.class);
|
exception.expect(IllegalStateException.class);
|
||||||
|
|
|
@ -60,12 +60,19 @@ public class QuotedKeysTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_support_table_array_index_with_quoted_key() throws Exception {
|
public void should_support_table_array_index_with_quoted_key() throws Exception {
|
||||||
Toml toml = new Toml().parse("[[dog.\" type\"]] \n name = \"type0\" \n [[dog.\" type\"]] \n name = \"type1\"");
|
Toml toml = new Toml().parse("[[ dog. \" type\" ]] \n name = \"type0\" \n [[dog.\" type\"]] \n name = \"type1\"");
|
||||||
|
|
||||||
assertEquals("type0", toml.getString("dog.\" type\"[0].name"));
|
assertEquals("type0", toml.getString("dog.\" type\"[0].name"));
|
||||||
assertEquals("type1", toml.getString("dog.\" type\"[1].name"));
|
assertEquals("type1", toml.getString("dog.\" type\"[1].name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_support_table_array_index_with_dot_in_quoted_key() throws Exception {
|
||||||
|
Toml toml = new Toml().parse("[[ dog. \"a.type\" ]] \n name = \"type0\"");
|
||||||
|
|
||||||
|
assertEquals("type0", toml.getString("dog.\"a.type\"[0].name"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_support_quoted_key_containing_square_brackets() throws Exception {
|
public void should_support_quoted_key_containing_square_brackets() throws Exception {
|
||||||
Toml toml = new Toml().parse("[dog.\" type[abc]\"] \n name = \"type0\" \n [dog.\" type[1]\"] \n \"name[]\" = \"type1\"");
|
Toml toml = new Toml().parse("[dog.\" type[abc]\"] \n name = \"type0\" \n [dog.\" type[1]\"] \n \"name[]\" = \"type1\"");
|
||||||
|
@ -88,6 +95,13 @@ public class QuotedKeysTest {
|
||||||
assertEquals(1, toml.getLong("\"abc.def\".key").intValue());
|
assertEquals(1, toml.getLong("\"abc.def\".key").intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_support_whitespace_around_key_segments() throws Exception {
|
||||||
|
Toml toml = new Toml().parse("[ dog. \"type\". breed ] \n name = \"type0\"");
|
||||||
|
|
||||||
|
assertEquals("type0", toml.getString("dog.\"type\".breed.name"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void should_fail_on_malformed_quoted_key() throws Exception {
|
public void should_fail_on_malformed_quoted_key() throws Exception {
|
||||||
new Toml().parse("k\"ey\" = 1");
|
new Toml().parse("k\"ey\" = 1");
|
||||||
|
|
|
@ -88,6 +88,11 @@ public class TableArrayTest {
|
||||||
assertEquals("plantain", bananaVariety);
|
assertEquals("plantain", bananaVariety);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void should_fail_on_empty_table_array_name() {
|
||||||
|
new Toml().parse("[[]]");
|
||||||
|
}
|
||||||
|
|
||||||
private File file(String fileName) {
|
private File file(String fileName) {
|
||||||
return new File(getClass().getResource(fileName + ".toml").getFile());
|
return new File(getClass().getResource(fileName + ".toml").getFile());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue