View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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
    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  }