1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
35
36
37
38 public class JavaBeanLoader<T> {
39 private Yaml loader;
40
41 public JavaBeanLoader(TypeDescription typeDescription) {
42 this(typeDescription, BeanAccess.DEFAULT);
43 }
44
45 public JavaBeanLoader(TypeDescription typeDescription, BeanAccess beanAccess) {
46 this(new LoaderOptions(typeDescription), beanAccess);
47 }
48
49 public JavaBeanLoader(LoaderOptions options, BeanAccess beanAccess) {
50 if (options == null) {
51 throw new NullPointerException("LoaderOptions must be provided.");
52 }
53 if (options.getRootTypeDescription() == null) {
54 throw new NullPointerException("TypeDescription must be provided.");
55 }
56 Constructor constructor = new Constructor(options.getRootTypeDescription());
57 loader = new Yaml(constructor, options, new Representer(), new DumperOptions(),
58 new Resolver());
59 loader.setBeanAccess(beanAccess);
60 }
61
62 public <S extends T> JavaBeanLoader(Class<S> clazz, BeanAccess beanAccess) {
63 this(new TypeDescription(clazz), beanAccess);
64 }
65
66 public <S extends T> JavaBeanLoader(Class<S> clazz) {
67 this(clazz, BeanAccess.DEFAULT);
68 }
69
70
71
72
73
74
75
76
77
78 @SuppressWarnings("unchecked")
79 public T load(String yaml) {
80 return (T) loader.load(new StringReader(yaml));
81 }
82
83
84
85
86
87
88
89
90
91 @SuppressWarnings("unchecked")
92 public T load(InputStream io) {
93 return (T) loader.load(new UnicodeReader(io));
94 }
95
96
97
98
99
100
101
102
103
104 @SuppressWarnings("unchecked")
105 public T load(Reader io) {
106 return (T) loader.load(io);
107 }
108
109 }