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