1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.pyyaml;
18
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.util.Iterator;
22
23 import org.yaml.snakeyaml.Yaml;
24 import org.yaml.snakeyaml.composer.Composer;
25 import org.yaml.snakeyaml.error.YAMLException;
26
27 public class CanonicalLoader extends Yaml {
28 @Override
29 public Object load(Reader yaml) {
30 try {
31 int ch = yaml.read();
32 StringBuilder buffer = new StringBuilder();
33 while (ch != -1) {
34 buffer.append((char) ch);
35 ch = yaml.read();
36 }
37 Composer composer = new Composer(new CanonicalParser(buffer.toString()), resolver);
38 constructor.setComposer(composer);
39 return constructor.getSingleData(Object.class);
40 } catch (IOException e) {
41 throw new YAMLException(e);
42 }
43 }
44
45 public Iterable<Object> loadAll(Reader yaml) {
46 try {
47 int ch = yaml.read();
48 StringBuilder buffer = new StringBuilder();
49 while (ch != -1) {
50 buffer.append((char) ch);
51 ch = yaml.read();
52 }
53 Composer composer = new Composer(new CanonicalParser(buffer.toString()), resolver);
54 this.constructor.setComposer(composer);
55 Iterator<Object> result = new Iterator<Object>() {
56 public boolean hasNext() {
57 return constructor.checkData();
58 }
59
60 public Object next() {
61 return constructor.getData();
62 }
63
64 public void remove() {
65 throw new UnsupportedOperationException();
66 }
67 };
68 return new YamlIterable(result);
69 } catch (IOException e) {
70 throw new YAMLException(e);
71 }
72 }
73
74 private class YamlIterable implements Iterable<Object> {
75 private Iterator<Object> iterator;
76
77 public YamlIterable(Iterator<Object> iterator) {
78 this.iterator = iterator;
79 }
80
81 public Iterator<Object> iterator() {
82 return iterator;
83 }
84
85 }
86 }