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