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.StringWriter;
19 import java.io.Writer;
20
21 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
22 import org.yaml.snakeyaml.introspector.BeanAccess;
23 import org.yaml.snakeyaml.nodes.Tag;
24 import org.yaml.snakeyaml.representer.Representer;
25
26
27
28
29
30
31 public class JavaBeanDumper {
32 private boolean useGlobalTag;
33 private FlowStyle flowStyle;
34 private DumperOptions options;
35 private Representer representer;
36 private final BeanAccess beanAccess;
37
38
39
40
41
42
43
44 public JavaBeanDumper(boolean useGlobalTag, BeanAccess beanAccess) {
45 this.useGlobalTag = useGlobalTag;
46 this.beanAccess = beanAccess;
47 this.flowStyle = FlowStyle.BLOCK;
48 }
49
50 public JavaBeanDumper(boolean useGlobalTag) {
51 this(useGlobalTag, BeanAccess.DEFAULT);
52 }
53
54 public JavaBeanDumper(BeanAccess beanAccess) {
55 this(false, beanAccess);
56 }
57
58
59
60
61 public JavaBeanDumper() {
62 this(BeanAccess.DEFAULT);
63 }
64
65 public JavaBeanDumper(Representer representer, DumperOptions options) {
66 if (representer == null) {
67 throw new NullPointerException("Representer must be provided.");
68 }
69 if (options == null) {
70 throw new NullPointerException("DumperOptions must be provided.");
71 }
72 this.options = options;
73 this.representer = representer;
74 this.beanAccess = null;
75
76 }
77
78
79
80
81
82
83
84
85
86 public void dump(Object data, Writer output) {
87 DumperOptions doptions;
88 if (this.options == null) {
89 doptions = new DumperOptions();
90 if (!useGlobalTag) {
91 doptions.setExplicitRoot(Tag.MAP);
92 }
93 doptions.setDefaultFlowStyle(flowStyle);
94 } else {
95 doptions = this.options;
96 }
97 Representer repr;
98 if (this.representer == null) {
99 repr = new Representer();
100 repr.getPropertyUtils().setBeanAccess(beanAccess);
101 } else {
102 repr = this.representer;
103 }
104 Yaml dumper = new Yaml(repr, doptions);
105 dumper.dump(data, output);
106 }
107
108
109
110
111
112
113
114
115 public String dump(Object data) {
116 StringWriter buffer = new StringWriter();
117 dump(data, buffer);
118 return buffer.toString();
119 }
120
121 public boolean isUseGlobalTag() {
122 return useGlobalTag;
123 }
124
125 public void setUseGlobalTag(boolean useGlobalTag) {
126 this.useGlobalTag = useGlobalTag;
127 }
128
129 public FlowStyle getFlowStyle() {
130 return flowStyle;
131 }
132
133 public void setFlowStyle(FlowStyle flowStyle) {
134 this.flowStyle = flowStyle;
135 }
136 }