Add 'Yaml.parse()' method which return Events to support low level YAML processing
diff --git a/README b/README
index b32734e..55b1208 100644
--- a/README
+++ b/README
@@ -105,9 +105,10 @@
== History ==
'''1.2 (in Mercurial)'''
+ * Add 'parse()' method which return Events to support low level YAML processing
* Add 'compose' methods which return Nodes
- * Rename LineBreak.LINUX to LineBreak.UNIX
- * Refactor: rename enums in DumperOptions to make the names consistent
+ * Rename `LineBreak`.LINUX to `LineBreak`.UNIX
+ * Refactor: rename enums in `DumperOptions` to make the names consistent
* Add possibility to parse all scalars as Strings
* Respect `DumperOptions` with a custom Representer
* Represent TAB as '\t' instead of '(9' in an error message
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ba95b2f..2caf7dd 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -5,6 +5,9 @@
</properties>
<body>
<release version="1.2-SNAPSHOT" date="in Mercurial" description="development">
+ <action dev="py4fun" type="add">
+ Add 'Yaml.parse()' method which return Events to support low level YAML processing (2009-04-20)
+ </action>
<action dev="py4fun" type="add" due-to="Bob Jalex" issue="5">
Introduce LineBreak.getPlatformLineBreak (2009-04-18)
</action>
diff --git a/src/main/java/org/yaml/snakeyaml/Loader.java b/src/main/java/org/yaml/snakeyaml/Loader.java
index c66475d..bbfd9d7 100644
--- a/src/main/java/org/yaml/snakeyaml/Loader.java
+++ b/src/main/java/org/yaml/snakeyaml/Loader.java
@@ -9,7 +9,9 @@
import org.yaml.snakeyaml.constructor.BaseConstructor;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.error.YAMLException;
+import org.yaml.snakeyaml.events.Event;
import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.parser.Parser;
import org.yaml.snakeyaml.parser.ParserImpl;
import org.yaml.snakeyaml.reader.Reader;
import org.yaml.snakeyaml.resolver.Resolver;
@@ -137,4 +139,41 @@
throw new YAMLException("Loader cannot be shared.");
}
}
+
+ /**
+ * Parse a YAML stream and produce parsing events.
+ *
+ * @param yaml
+ * - YAML document(s)
+ * @return parsed events
+ */
+ public Iterable<Event> parse(java.io.Reader yaml) {
+ final Parser parser = new ParserImpl(new Reader(yaml));
+ Iterator<Event> result = new Iterator<Event>() {
+ public boolean hasNext() {
+ return parser.peekEvent() != null;
+ }
+
+ public Event next() {
+ return parser.getEvent();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ return new EventIterable(result);
+ }
+
+ private class EventIterable implements Iterable<Event> {
+ private Iterator<Event> iterator;
+
+ public EventIterable(Iterator<Event> iterator) {
+ this.iterator = iterator;
+ }
+
+ public Iterator<Event> iterator() {
+ return iterator;
+ }
+ }
}
diff --git a/src/main/java/org/yaml/snakeyaml/Yaml.java b/src/main/java/org/yaml/snakeyaml/Yaml.java
index 4e21736..4275936 100644
--- a/src/main/java/org/yaml/snakeyaml/Yaml.java
+++ b/src/main/java/org/yaml/snakeyaml/Yaml.java
@@ -12,6 +12,7 @@
import java.util.List;
import java.util.regex.Pattern;
+import org.yaml.snakeyaml.events.Event;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.reader.UnicodeReader;
import org.yaml.snakeyaml.resolver.Resolver;
@@ -308,4 +309,15 @@
public void setName(String name) {
this.name = name;
}
+
+ /**
+ * Parse a YAML stream and produce parsing events.
+ *
+ * @param yaml
+ * - YAML document(s)
+ * @return parsed events
+ */
+ public Iterable<Event> parse(java.io.Reader yaml) {
+ return loader.parse(yaml);
+ }
}
diff --git a/src/test/java/org/yaml/snakeyaml/YamlComposeTest.java b/src/test/java/org/yaml/snakeyaml/YamlComposeTest.java
new file mode 100644
index 0000000..0683711
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/YamlComposeTest.java
@@ -0,0 +1,89 @@
+/*
+ * See LICENSE file in distribution for copyright and licensing information.
+ */
+package org.yaml.snakeyaml;
+
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+
+import junit.framework.TestCase;
+
+import org.yaml.snakeyaml.nodes.MappingNode;
+import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.nodes.NodeId;
+
+public class YamlComposeTest extends TestCase {
+
+ public void testComposeFromString() {
+ Yaml yaml = new Yaml();
+ MappingNode node = (MappingNode) yaml.compose("abc: 56");
+ assertEquals("abc", node.getValue().get(0)[0].getValue());
+ assertEquals("56", node.getValue().get(0)[1].getValue());
+ }
+
+ public void testComposeFromString2() {
+ try {
+ Yaml yaml = new Yaml();
+ yaml.compose("abc: 56\n---\n123\n---\n456");
+ fail("YAML contans more then one ducument.");
+ } catch (Exception e) {
+ assertEquals("expected a single document in the stream; but found another document", e
+ .getMessage());
+ }
+ }
+
+ public void testComposeFromReader() {
+ Yaml yaml = new Yaml();
+ MappingNode node = (MappingNode) yaml.compose(new StringReader("abc: 56"));
+ assertEquals("abc", node.getValue().get(0)[0].getValue());
+ assertEquals("56", node.getValue().get(0)[1].getValue());
+ }
+
+ public void testComposeFromInputStream() {
+ Yaml yaml = new Yaml();
+ MappingNode node = (MappingNode) yaml
+ .compose(new ByteArrayInputStream("abc: 56".getBytes()));
+ assertEquals("abc", node.getValue().get(0)[0].getValue());
+ assertEquals("56", node.getValue().get(0)[1].getValue());
+ }
+
+ public void testComposeAllFromString() {
+ Yaml yaml = new Yaml();
+ boolean first = true;
+ for (Node node : yaml.composeAll("abc: 56\n---\n123\n---\n456")) {
+ if (first) {
+ assertEquals(NodeId.mapping, node.getNodeId());
+ } else {
+ assertEquals(NodeId.scalar, node.getNodeId());
+ }
+ first = false;
+ }
+ }
+
+ public void testComposeAllFromReader() {
+ Yaml yaml = new Yaml();
+ boolean first = true;
+ for (Node node : yaml.composeAll(new StringReader("abc: 56\n---\n123\n---\n456"))) {
+ if (first) {
+ assertEquals(NodeId.mapping, node.getNodeId());
+ } else {
+ assertEquals(NodeId.scalar, node.getNodeId());
+ }
+ first = false;
+ }
+ }
+
+ public void testComposeAllFromInputStream() {
+ Yaml yaml = new Yaml();
+ boolean first = true;
+ for (Node node : yaml.composeAll(new ByteArrayInputStream("abc: 56\n---\n123\n---\n456"
+ .getBytes()))) {
+ if (first) {
+ assertEquals(NodeId.mapping, node.getNodeId());
+ } else {
+ assertEquals(NodeId.scalar, node.getNodeId());
+ }
+ first = false;
+ }
+ }
+}
diff --git a/src/test/java/org/yaml/snakeyaml/YamlParseTest.java b/src/test/java/org/yaml/snakeyaml/YamlParseTest.java
new file mode 100644
index 0000000..441ab4c
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/YamlParseTest.java
@@ -0,0 +1,45 @@
+/*
+ * See LICENSE file in distribution for copyright and licensing information.
+ */
+package org.yaml.snakeyaml;
+
+import java.io.StringReader;
+
+import junit.framework.TestCase;
+
+import org.yaml.snakeyaml.events.Event;
+import org.yaml.snakeyaml.events.StreamEndEvent;
+import org.yaml.snakeyaml.events.StreamStartEvent;
+
+public class YamlParseTest extends TestCase {
+
+ public void testParse() {
+ Yaml yaml = new Yaml();
+ Event e = null;
+ int counter = 0;
+ for (Event event : yaml.parse(new StringReader("abc: 56"))) {
+ if (e == null) {
+ assertTrue(event instanceof StreamStartEvent);
+ }
+ e = event;
+ counter++;
+ }
+ assertTrue(e instanceof StreamEndEvent);
+ assertEquals(8, counter);
+ }
+
+ public void testParseManyDocuments() {
+ Yaml yaml = new Yaml();
+ Event e = null;
+ int counter = 0;
+ for (Event event : yaml.parse(new StringReader("abc: 56\n---\n4\n---\nqwe\n"))) {
+ if (e == null) {
+ assertTrue(event instanceof StreamStartEvent);
+ }
+ e = event;
+ counter++;
+ }
+ assertTrue(e instanceof StreamEndEvent);
+ assertEquals(14, counter);
+ }
+}
diff --git a/src/test/java/org/yaml/snakeyaml/YamlTest.java b/src/test/java/org/yaml/snakeyaml/YamlTest.java
index 070e349..686b8fb 100644
--- a/src/test/java/org/yaml/snakeyaml/YamlTest.java
+++ b/src/test/java/org/yaml/snakeyaml/YamlTest.java
@@ -1,14 +1,10 @@
+/*
+ * See LICENSE file in distribution for copyright and licensing information.
+ */
package org.yaml.snakeyaml;
-import java.io.ByteArrayInputStream;
-import java.io.StringReader;
-
import junit.framework.TestCase;
-import org.yaml.snakeyaml.nodes.MappingNode;
-import org.yaml.snakeyaml.nodes.Node;
-import org.yaml.snakeyaml.nodes.NodeId;
-
public class YamlTest extends TestCase {
public void testSetNoName() {
@@ -22,77 +18,4 @@
assertEquals("REST", yaml.getName());
assertEquals("REST", yaml.toString());
}
-
- public void testComposeFromString() {
- Yaml yaml = new Yaml();
- MappingNode node = (MappingNode) yaml.compose("abc: 56");
- assertEquals("abc", node.getValue().get(0)[0].getValue());
- assertEquals("56", node.getValue().get(0)[1].getValue());
- }
-
- public void testComposeFromString2() {
- try {
- Yaml yaml = new Yaml();
- yaml.compose("abc: 56\n---\n123\n---\n456");
- fail("YAML contans more then one ducument.");
- } catch (Exception e) {
- assertEquals("expected a single document in the stream; but found another document", e
- .getMessage());
- }
- }
-
- public void testComposeFromReader() {
- Yaml yaml = new Yaml();
- MappingNode node = (MappingNode) yaml.compose(new StringReader("abc: 56"));
- assertEquals("abc", node.getValue().get(0)[0].getValue());
- assertEquals("56", node.getValue().get(0)[1].getValue());
- }
-
- public void testComposeFromInputStream() {
- Yaml yaml = new Yaml();
- MappingNode node = (MappingNode) yaml
- .compose(new ByteArrayInputStream("abc: 56".getBytes()));
- assertEquals("abc", node.getValue().get(0)[0].getValue());
- assertEquals("56", node.getValue().get(0)[1].getValue());
- }
-
- public void testComposeAllFromString() {
- Yaml yaml = new Yaml();
- boolean first = true;
- for (Node node : yaml.composeAll("abc: 56\n---\n123\n---\n456")) {
- if (first) {
- assertEquals(NodeId.mapping, node.getNodeId());
- } else {
- assertEquals(NodeId.scalar, node.getNodeId());
- }
- first = false;
- }
- }
-
- public void testComposeAllFromReader() {
- Yaml yaml = new Yaml();
- boolean first = true;
- for (Node node : yaml.composeAll(new StringReader("abc: 56\n---\n123\n---\n456"))) {
- if (first) {
- assertEquals(NodeId.mapping, node.getNodeId());
- } else {
- assertEquals(NodeId.scalar, node.getNodeId());
- }
- first = false;
- }
- }
-
- public void testComposeAllFromInputStream() {
- Yaml yaml = new Yaml();
- boolean first = true;
- for (Node node : yaml.composeAll(new ByteArrayInputStream("abc: 56\n---\n123\n---\n456"
- .getBytes()))) {
- if (first) {
- assertEquals(NodeId.mapping, node.getNodeId());
- } else {
- assertEquals(NodeId.scalar, node.getNodeId());
- }
- first = false;
- }
- }
}