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