1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
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
71
72
73
74
75
76
77 @SuppressWarnings("unchecked")
78 public T load(String yaml) {
79 return (T) loader.load(new StringReader(yaml));
80 }
81
82
83
84
85
86
87
88
89
90 @SuppressWarnings("unchecked")
91 public T load(InputStream io) {
92 return (T) loader.load(new UnicodeReader(io));
93 }
94
95
96
97
98
99
100
101
102
103 @SuppressWarnings("unchecked")
104 public T load(Reader io) {
105 return (T) loader.load(io);
106 }
107
108 }