• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.pyyaml;
15 
16 import java.io.IOException;
17 import java.io.Reader;
18 import java.util.Iterator;
19 import org.yaml.snakeyaml.Yaml;
20 import org.yaml.snakeyaml.composer.Composer;
21 import org.yaml.snakeyaml.error.YAMLException;
22 
23 public class CanonicalLoader extends Yaml {
24 
25   @Override
load(Reader yaml)26   public Object load(Reader yaml) {
27     try {
28       int ch = yaml.read();
29       StringBuilder buffer = new StringBuilder();
30       while (ch != -1) {
31         buffer.append((char) ch);
32         ch = yaml.read();
33       }
34       Composer composer = new Composer(
35           new CanonicalParser(buffer.toString().replace(System.lineSeparator(), "\n")), resolver);
36       constructor.setComposer(composer);
37       return constructor.getSingleData(Object.class);
38     } catch (IOException e) {
39       throw new YAMLException(e);
40     }
41   }
42 
loadAll(Reader yaml)43   public Iterable<Object> loadAll(Reader yaml) {
44     try {
45       int ch = yaml.read();
46       StringBuilder buffer = new StringBuilder();
47       while (ch != -1) {
48         buffer.append((char) ch);
49         ch = yaml.read();
50       }
51       Composer composer = new Composer(new CanonicalParser(buffer.toString()), resolver);
52       this.constructor.setComposer(composer);
53       Iterator<Object> result = new Iterator<Object>() {
54         public boolean hasNext() {
55           return constructor.checkData();
56         }
57 
58         public Object next() {
59           return constructor.getData();
60         }
61 
62         public void remove() {
63           throw new UnsupportedOperationException();
64         }
65       };
66       return new YamlIterable(result);
67     } catch (IOException e) {
68       throw new YAMLException(e);
69     }
70   }
71 
72   private class YamlIterable implements Iterable<Object> {
73 
74     private final Iterator<Object> iterator;
75 
YamlIterable(Iterator<Object> iterator)76     public YamlIterable(Iterator<Object> iterator) {
77       this.iterator = iterator;
78     }
79 
iterator()80     public Iterator<Object> iterator() {
81       return iterator;
82     }
83 
84   }
85 }
86