adding simple block parsing to yaml tiny

main 5.1.0
Jörg Prante 1 month ago
parent 0667b90ac4
commit 1330905066

@ -695,7 +695,7 @@ public abstract class Ordering<T> implements Comparator<T> {
* only when the resulting list may need further modification, or may contain {@code null}. The
* input is not modified. The returned list has random access.
*
* <p>Unlike {@link java.util.Sets#newTreeSet(Iterable)}, this method does not discard elements that are
* <p>Tthis method does not discard elements that are
* duplicates according to the comparator. The sort performed is <i>stable</i>, meaning that such
* elements will appear in the returned list in the same order they appeared in {@code elements}.
*
@ -716,7 +716,7 @@ public abstract class Ordering<T> implements Comparator<T> {
* Returns an <b>immutable</b> list containing {@code elements} sorted by this ordering. The input
* is not modified.
*
* <p>Unlike {@link java.util.Sets#newTreeSet(Iterable)}, this method does not discard elements that are
* <p>This method does not discard elements that are
* duplicates according to the comparator. The sort performed is <i>stable</i>, meaning that such
* elements will appear in the returned list in the same order they appeared in {@code elements}.
*
@ -735,10 +735,6 @@ public abstract class Ordering<T> implements Comparator<T> {
* Returns {@code true} if each element in {@code iterable} after the first is greater than or
* equal to the element that preceded it, according to this ordering. Note that this is always
* true when the iterable has fewer than two elements.
*
* <p><b>Java 8 users:</b> Use the equivalent {@link java.util.Comparators#isInOrder(Iterable, Comparator)}
* instead, since the rest of {@code Ordering} is mostly obsolete (as explained in the class
* documentation).
*/
public boolean isOrdered(Iterable<? extends T> iterable) {
Iterator<? extends T> it = iterable.iterator();
@ -759,10 +755,6 @@ public abstract class Ordering<T> implements Comparator<T> {
* Returns {@code true} if each element in {@code iterable} after the first is <i>strictly</i>
* greater than the element that preceded it, according to this ordering. Note that this is always
* true when the iterable has fewer than two elements.
*
* <p><b>Java 8 users:</b> Use the equivalent {@link java.util.Comparators#isInStrictOrder(Iterable,
* Comparator)} instead, since the rest of {@code Ordering} is mostly obsolete (as explained in
* the class documentation).
*/
public boolean isStrictlyOrdered(Iterable<? extends T> iterable) {
Iterator<? extends T> it = iterable.iterator();

@ -1,4 +1,4 @@
This is a modfied work of
This is a modified work of
https://github.com/Kahsolt/IfYaml

@ -5,5 +5,6 @@ module org.xbib.datastructures.yaml.tiny {
exports org.xbib.datastructures.yaml.tiny;
requires org.xbib.datastructures.api;
requires org.xbib.datastructures.tiny;
requires java.logging;
provides DataStructure with Yaml;
}

@ -53,13 +53,14 @@ public class Lexer {
return null;
}
boolean isPipe = false;
int indent;
switch (token.getType()) {
case DOCUMENT_START:
case DOCUMENT_END:
return token;
case VALUE:
List<String> values = new ArrayList<>();
int indent = token.getDepth();
indent = token.getDepth();
do {
if (token.getType() == TokenType.COMMENT) {
tokens.add(token);
@ -71,7 +72,7 @@ public class Lexer {
} while ((token = nextToken()) != null && prevToken != null && token.getDepth() > prevToken.getDepth());
nextToken = token;
tokens.add(values.size() == 1
? new Token(TokenType.VALUE_LINE, indent, values.get(0))
? new Token(TokenType.VALUE_LINE, indent, values.getFirst())
: new Token(TokenType.VALUE_MULTILINE, indent, String.join(" ", values)));
return next();
case PIPE:
@ -130,7 +131,16 @@ public class Lexer {
if (isEOL()) {
return nextToken();
}
if (isHash()) {
if (isText()) {
indent = index;
value = extractText();
if (isColon() && hasRightGap()) {
read(2);
return new Token(TokenType.KEY, indent, value);
} else {
return new Token(TokenType.VALUE, indent, value);
}
} else if (isHash()) {
indent = index;
read();
value = new String(line).substring(index);
@ -154,15 +164,6 @@ public class Lexer {
} else if (isDocumentEnd()) {
read(3);
return new Token(TokenType.DOCUMENT_END, 0);
} else if (isText()) {
indent = index;
value = extractText();
if (isColon() && hasRightGap()) {
read(2);
return new Token(TokenType.KEY, indent, value);
} else {
return new Token(TokenType.VALUE, indent, value);
}
}
throw new YamlException("Syntax error: (" + lineno + ":" + index + ":" + new String(line));
}

@ -2,10 +2,10 @@ package org.xbib.datastructures.yaml.tiny;
import org.xbib.datastructures.api.Node;
import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ValueNode implements org.xbib.datastructures.api.ValueNode {
@ -15,8 +15,6 @@ public class ValueNode implements org.xbib.datastructures.api.ValueNode {
private TextType type;
private List<String> comments;
public ValueNode(Node<?> parent) {
this(parent, "");
}
@ -46,6 +44,27 @@ public class ValueNode implements org.xbib.datastructures.api.ValueNode {
return value;
}
public void add(Object value) {
if (value == null) {
return;
}
if (this.value == null) {
this.value = new StringBuilder();
((StringBuilder) this.value).append(value);
} else {
if (this.value instanceof StringBuilder) {
((StringBuilder) this.value).append(value);
} else if (this.value instanceof String) {
String v = this.value.toString();
this.value = new StringBuilder();
((StringBuilder) this.value).append(v);
((StringBuilder) this.value).append(value);
} else {
throw new IllegalArgumentException("unexpected value class " + this.value.getClass());
}
}
}
public void setType(TextType type) {
this.type = type;
}
@ -55,14 +74,7 @@ public class ValueNode implements org.xbib.datastructures.api.ValueNode {
}
public List<String> getComments() {
return comments != null ? comments : Collections.emptyList();
}
public void addComments(Collection<String> comments) {
if (this.comments == null) {
this.comments = new ArrayList<>();
}
this.comments.addAll(comments);
return Collections.emptyList();
}
public enum TextType {

@ -8,11 +8,15 @@ import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.xbib.datastructures.yaml.tiny.TokenType.ITEM;
public class YamlParser implements Parser {
private static final Logger logger = Logger.getLogger(YamlParser.class.getName());
private final List<Token> comments;
private Lexer lexer;
@ -38,27 +42,21 @@ public class YamlParser implements Parser {
if (token.getDepth() < depth) {
return new ValueNode(parent);
}
switch (token.getType()) {
case DOCUMENT_START:
case DOCUMENT_END:
return switch (token.getType()) {
case DOCUMENT_START, DOCUMENT_END -> {
token = lexer.next();
return parseNode(parent, depth);
case COMMENT:
yield parseNode(parent, depth);
}
case COMMENT -> {
stashComments();
return parseNode(parent, depth);
case ITEM:
return parseListNode(parent, token.getDepth());
case KEY:
return parseHashNode(parent, token.getDepth());
case VALUE:
case VALUE_LINE:
case VALUE_MULTILINE:
case VALUE_TEXT_PIPE:
case VALUE_TEXT_ANGLE:
return parseValueNode(parent, token.getDepth());
default:
return null;
}
yield parseNode(parent, depth);
}
case ITEM -> parseListNode(parent, token.getDepth());
case KEY -> parseHashNode(parent, token.getDepth());
case VALUE, VALUE_LINE, VALUE_MULTILINE -> parseValueNode(parent, token.getDepth());
case VALUE_TEXT_PIPE, VALUE_TEXT_ANGLE -> parseBlock(parent, token.getDepth());
default -> throw new IllegalArgumentException("unexpected: " + token.getType());
};
}
private ListNode parseListNode(Node<?> parent, int indent) throws IOException {
@ -92,35 +90,42 @@ public class YamlParser implements Parser {
private ValueNode parseValueNode(Node<?> parent, int indent) throws IOException {
ValueNode node = new ValueNode(parent);
node.addComments(collectComments(indent));
switch (token.getType()) {
case VALUE_LINE:
case VALUE_LINE -> {
node.setType(ValueNode.TextType.LINE);
node.set(token.getValue());
token = lexer.next();
break;
case VALUE_MULTILINE:
}
case VALUE_MULTILINE -> {
node.setType(ValueNode.TextType.MULTILINE);
node.set(token.getValue());
token = lexer.next();
break;
case VALUE_TEXT_PIPE:
node.setType(ValueNode.TextType.TEXT);
node.set(token.getValue());
token = lexer.next();
break;
case VALUE_TEXT_ANGLE:
node.setType(ValueNode.TextType.TEXT_ANGLE);
node.set(token.getValue());
token = lexer.next();
break;
default:
}
default -> {
node.set("");
break;
}
}
return node;
}
private ValueNode parseBlock(Node<?> parent, int indent) throws IOException {
ValueNode node = new ValueNode(parent);
do {
switch (token.getType()) {
case VALUE_TEXT_PIPE -> {
node.setType(ValueNode.TextType.TEXT);
node.add(token.getValue());
}
case VALUE_TEXT_ANGLE -> {
node.setType(ValueNode.TextType.TEXT_ANGLE);
node.add(token.getValue());
}
}
token = lexer.next();
} while (token != null && token.getDepth() >= indent);
return node;
}
private List<String> collectComments(int indent) {
ListIterator<Token> iter = comments.listIterator(comments.size());
while (iter.hasPrevious()) {

@ -1,5 +1,6 @@
package org.xbib.datastructures.yaml.tiny.test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.xbib.datastructures.api.Node;
import org.xbib.datastructures.api.Parser;
@ -12,8 +13,6 @@ import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -21,26 +20,30 @@ public class YamlParserTest {
@Test
public void parseList() throws Exception {
Reader reader = new StringReader("test:\n- a\n- b\n");
// exact spacing (we have no space management yet)
String s = "test: \n - a\n - b\n";
Reader reader = new StringReader(s);
Parser parser = new YamlParser();
Node<?> node = parser.parse(reader);
Yaml yaml = new Yaml(node);
StringWriter writer = new StringWriter();
yaml.createGenerator(node).generate(writer);
String s1 = writer.toString();
assertEquals(s, s1);
}
@Test
public void example() throws Exception {
InputStream inputStream = YamlTest.class.getResourceAsStream("/org/xbib/datastructures/yaml/test/example.yml");
InputStream inputStream = getClass().getResourceAsStream("example.yml");
if (inputStream != null) {
roundTrip(inputStream);
}
}
@Disabled("yaml space management")
@Test
public void test() throws Exception {
InputStream inputStream = YamlTest.class.getResourceAsStream("/org/xbib/datastructures/yaml/test/test.yml");
InputStream inputStream = getClass().getResourceAsStream("test.yml");
if (inputStream != null) {
roundTrip(inputStream);
}
@ -48,16 +51,18 @@ public class YamlParserTest {
@Test
public void hippie() throws Exception {
InputStream inputStream = YamlTest.class.getResourceAsStream("/org/xbib/datastructures/yaml/test/hippie.yml");
InputStream inputStream = getClass().getResourceAsStream("hippie.yml");
if (inputStream != null) {
roundTrip(inputStream);
}
}
@Test
public void interlibrary() throws Exception {
InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("user.home") + "/.config/interlibrary/test.yaml"));
roundTrip(inputStream);
public void sshKey() throws Exception {
InputStream inputStream = getClass().getResourceAsStream("key.yaml");
if (inputStream != null) {
roundTrip(inputStream);
}
}
private static void roundTrip(InputStream inputStream) throws Exception {

@ -1,6 +1,7 @@
package org.xbib.datastructures.yaml.tiny.test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.xbib.datastructures.api.Builder;
import org.xbib.datastructures.api.DataStructure;
@ -19,6 +20,8 @@ import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.*;
@ -30,7 +33,7 @@ public class YamlTest {
@BeforeEach
public void setup() throws IOException {
InputStream inputStream = YamlTest.class.getResourceAsStream("/org/xbib/datastructures/yaml/test/test.yml");
InputStream inputStream = YamlTest.class.getResourceAsStream("test.yml");
if (inputStream != null) {
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
Parser parser = new YamlParser();
@ -52,9 +55,10 @@ public class YamlTest {
valueNode = (ValueNode) hsnode.get("multiline");
assertEquals("line 1 line 2 line 3", valueNode.get());
valueNode = (ValueNode) hsnode.get("text");
assertEquals("def func(x) do\n # do something\n print x = x * 2\nend", valueNode.get());
assertEquals("def func(x) do\n # do something\n print x = x * 2\nend", valueNode.get().toString());
hsnode = (MapNode) node;
lsnode = (ListNode) hsnode.get("hash1");
assertNotNull(lsnode);
hsnode = (MapNode) lsnode.get(0);
lsnode = (ListNode) hsnode.get("hash2");
valueNode = (ValueNode) lsnode.get(0);
@ -101,6 +105,7 @@ public class YamlTest {
assertEquals("line2", yaml.getString("list.1"));
}
@Disabled("yaml space management")
@Test
public void testGenerator() throws IOException {
Writer writer = new FileWriter("build/test.yml");
@ -132,7 +137,7 @@ public class YamlTest {
public void testStructure() throws IOException {
DataStructure structure = new Yaml();
Parser parser = structure.createParser();
InputStream inputStream = YamlTest.class.getResourceAsStream("/org/xbib/datastructures/yaml/test/example.yml");
InputStream inputStream = YamlTest.class.getResourceAsStream("example.yml");
if (inputStream == null) {
fail();
}

@ -0,0 +1,5 @@
handlers=java.util.logging.ConsoleHandler
.level=ALL
java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] %5$s %6$s%n
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

@ -0,0 +1,28 @@
ssh-key: |
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEAssBRe91wZ0TJBIWK2V1NH/ourcFPb0cA4ln32a3j5QITMS3zhs/o
euh8jPJ9eca93B+mfep5ly/UjwmDctGbwX54sJngE4Vuv5FgqctR8oHTxV+V18UdolBSsy
yiAVycGUexN2yz7P5JBzwfOG3WEwNe4dNVzmFj51nXAlaX+MB+wLfrZfU1vQpqmU8Esiu+
Hdab948qhmGlMepBw+M4Z7wVfgfz855ywxgL3NrVk2WhXxE9ng/jTLjHKkxKE/3sM/81wt
bvjejPvEMeFPD2XXBZSPi7TpgOiMBWDPXUwbrDYH6S6J2HAvYgwDm1pdQZGrOLrTBVJJE/
DX1KpYZJzQAAA9DSZOo10mTqNQAAAAdzc2gtcnNhAAABAQCywFF73XBnRMkEhYrZXU0f+i
6twU9vRwDiWffZrePlAhMxLfOGz+h66HyM8n15xr3cH6Z96nmXL9SPCYNy0ZvBfniwmeBP
hW6/kWCpy1HygdPFX5XXxR2iUFKzLKIBXJwZR7E3bLPs/kkHPB84bdYTA17h01XOYWPnWd
cCVpf4wH7At+tl9TW9CmqZTwSyK74d1pv3jyqGYaVN6kHD4zhnvBV+B/PznnLDGAvc2tWT
ZaFfET2eD+NMuMcqTEoT/ewz/zXC1u+N6M+8Qx4U8PZdcFlI+LtOmA6IwFYM9dTBusNgfp
LonYcC9iDAObWl1Bkas4itMFUkkT8NfUqlhknNAAAAAwEAAQAAAQBN1kUlROX/cgp+t5Ag
2uoMtKrC6tymPir6ZebxmTEVtfOZhML4v2wiqT4jOiy9bHecdQPQ7NuJpEBREPl2dCP4/B
OeA0OUHSx+qtWG2oySp0oKNndPf/xJg+SfNR5OrX8j2v4mfmVTG9+9EMcfkWSY3uzgNWC1
/967DXn9AKwomx8yszA7YY0vKanLPx5C14WtzMPSbfwYZoKV4ddBHAF/7JHXAXxMisc9Ud
kziaS8SV4YJt7gSYKKMvzOEj+uiyk9DKoYWf6t++SQ93CUnZKLfhwYTUx/rsYt6ubblQeP
IHI/j8LKiVz6nvyDt2NXSJ2Z2j0s6roREYgnLaqbjlPZAAAAgQC6DgqTehb2XTrg69D8lt
Lyo5sutB3bTIHyg6GBSBW2qYh3D2PQaWdrcYe7WYGtp6OGmTgGcXX0DBCCTtrwTdxsPVeL
XhC/HBY271v9T18Ur4h310iJWVPJ8I7TgJuaSsfui/04NcqcW5XwFy6DHDQKxNwDhEscwg
wIaUrd8UYfkAAAAIEA552RMzZ5OCAEryh1OXnV3EeqhumsCbET6dpDOpSQnHss7u3CZ8d6
2LwHQJ/fjwDcrMYwEUwDkNoZjhEmj1e5LVTLjRS02VBgjg7RnphpuaphPZ+CDNlq3Om5C9
xW96+4eC9/T7SRaspF3FxhgtPUMI1beu1QnpL0jduT/GQSqaMAAACBAMWR+CLktU6cTBH7
RnfnB2K7E8slA8/hSGUZJ35JXJj5XujQgaf2d8hi4Lmt8smBojaERlCxxx3B9hWVYRkwCM
C8YRNCLnBgR2CCp27D0wuadL9aFITlx91GPytF9BKxzy949VaF6SEw9M86oouj362u/BvP
CO7Hnjlg77HRNFXPAAAAFWxrYW1pcmVkZHlAdm13YXJlLmNvbQECAwQF
-----END OPENSSH PRIVATE KEY-----

@ -13,17 +13,7 @@ test {
testLogging {
events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
}
jvmArgs '--add-exports=java.base/jdk.internal.ref=ALL-UNNAMED',
'--add-exports=java.base/sun.nio.ch=ALL-UNNAMED',
'--add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
'--add-opens=java.base/java.lang=ALL-UNNAMED',
'--add-opens=java.base/java.lang.reflect=ALL-UNNAMED',
'--add-opens=java.base/java.io=ALL-UNNAMED',
'--add-opens=java.base/java.util=ALL-UNNAMED'
systemProperty 'java.util.logging.config.file', 'src/test/resources/logging.properties'
afterSuite { desc, result ->
if (!desc.parent) {
println "\nTest result: ${result.resultType}"

@ -1,4 +0,0 @@
/**
* YAML settings with the datastructures package.
*/
package org.xbib.settings.datastructures.yaml;

@ -22,20 +22,22 @@ public class YamlSettingsTest {
Map<String, Object> settingsMap = new HashMap<>();
settingsMap.put("map", map);
SettingsLoader settingsLoader = new YamlSettingsLoader();
Settings settings = Settings.settingsBuilder()
try (Settings settings = Settings.settingsBuilder()
.put(settingsLoader.load(settingsMap))
.build();
assertEquals("{map.hello=world}", settings.getAsMap().toString());
.build()) {
assertEquals("{map.hello=world}", settings.getAsMap().toString());
}
}
@Test
public void testMapSettingsFromReader() throws IOException {
Map<String, Object> map = Map.of("map", Map.of("hello", "world"));
SettingsLoader settingsLoader = new YamlSettingsLoader();
Settings settings = Settings.settingsBuilder()
try (Settings settings = Settings.settingsBuilder()
.put(settingsLoader.load(map))
.build();
assertEquals("{map.hello=world}", settings.getAsMap().toString());
.build()) {
assertEquals("{map.hello=world}", settings.getAsMap().toString());
}
}
@Test
@ -46,6 +48,14 @@ public class YamlSettingsTest {
assertEquals("{a.b=c}", flatMap.toString());
}
@Test
public void testMultiLineString() throws IOException {
String s = "test: >\n this is a\n multiline\n string\na: b\b";
SettingsLoader loader = new YamlSettingsLoader();
Map<String, String> map = loader.load(s);
assertEquals("{test=this is a multiline string, a=b}", map.toString());
}
@Test
public void testLoadFromMap() throws IOException {
Map<String, Object> map = new LinkedHashMap<>();

@ -1,4 +0,0 @@
/**
* Testing YAML settings with the datastructures package.
*/
package org.xbib.settings.datastructures.yaml.test;
Loading…
Cancel
Save