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.yaml.snakeyaml;
    18
    19  import java.io.InputStream;
    20  import java.io.Reader;
    21  import java.io.StringReader;
    22
    23  import org.yaml.snakeyaml.constructor.Constructor;
    24  import org.yaml.snakeyaml.introspector.BeanAccess;
    25  import org.yaml.snakeyaml.reader.UnicodeReader;
    26  import org.yaml.snakeyaml.representer.Representer;
    27  import org.yaml.snakeyaml.resolver.Resolver;
    28
    29  /**
    30   * Convenience utility to parse JavaBeans. When the YAML document contains a
    31   * global tag with the class definition like '!!com.package.MyBean' it is
    32   * ignored in favour of the runtime class <code>T</code>.
    33   *
    34   * @deprecated use Yaml.loadAs() methods instead
    35   * @see http://www.artima.com/weblogs/viewpost.jsp?thread=208860
    36   */
    37  public class JavaBeanLoader<T> {
    38      private Yaml loader;
    39
    40      public JavaBeanLoader(TypeDescription typeDescription) {
    41          this(typeDescription, BeanAccess.DEFAULT);
    42      }
    43
    44      public JavaBeanLoader(TypeDescription typeDescription, BeanAccess beanAccess) {
    45          this(new LoaderOptions(typeDescription), beanAccess);
    46      }
    47
    48      public JavaBeanLoader(LoaderOptions options, BeanAccess beanAccess) {
    49          if (options == null) {
    50              throw new NullPointerException("LoaderOptions must be provided.");
    51          }
    52          if (options.getRootTypeDescription() == null) {
    53              throw new NullPointerException("TypeDescription must be provided.");
    54          }
    55          Constructor constructor = new Constructor(options.getRootTypeDescription());
    56          loader = new Yaml(constructor, options, new Representer(), new DumperOptions(),
    57                  new Resolver());
    58          loader.setBeanAccess(beanAccess);
    59      }
    60
    61      public <S extends T> JavaBeanLoader(Class<S> clazz, BeanAccess beanAccess) {
    62          this(new TypeDescription(clazz), beanAccess);
    63      }
    64
    65      public <S extends T> JavaBeanLoader(Class<S> clazz) {
    66          this(clazz, BeanAccess.DEFAULT);
    67      }
    68
    69      /**
    70       * Parse the first YAML document in a stream and produce the corresponding
    71       * JavaBean.
    72       *
    73       * @param yaml
    74       *            YAML document
    75       * @return parsed JavaBean
    76       */
    77      @SuppressWarnings("unchecked")
    78      public T load(String yaml) {
    79          return (T) loader.load(new StringReader(yaml));
    80      }
    81
    82      /**
    83       * Parse the first YAML document in a stream and produce the corresponding
    84       * JavaBean.
    85       *
    86       * @param io
    87       *            data to load from (BOM is respected and removed)
    88       * @return parsed JavaBean
    89       */
    90      @SuppressWarnings("unchecked")
    91      public T load(InputStream io) {
    92          return (T) loader.load(new UnicodeReader(io));
    93      }
    94
    95      /**
    96       * Parse the first YAML document in a stream and produce the corresponding
    97       * Java object.
    98       *
    99       * @param io
    100      *            data to load from (BOM must not be present)
    101      * @return parsed JavaBean
    102      */
    103     @SuppressWarnings("unchecked")
    104     public T load(Reader io) {
    105         return (T) loader.load(io);
    106     }
    107
    108 }