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 | 3 | public JavaBeanDumper(boolean useGlobalTag, BeanAccess beanAccess) { |
45 | 3 | this.useGlobalTag = useGlobalTag; |
46 | 3 | this.beanAccess = beanAccess; |
47 | 3 | this.flowStyle = FlowStyle.BLOCK; |
48 | 3 | } |
49 | |
|
50 | |
public JavaBeanDumper(boolean useGlobalTag) { |
51 | 0 | this(useGlobalTag, BeanAccess.DEFAULT); |
52 | 0 | } |
53 | |
|
54 | |
public JavaBeanDumper(BeanAccess beanAccess) { |
55 | 3 | this(false, beanAccess); |
56 | 3 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public JavaBeanDumper() { |
62 | 3 | this(BeanAccess.DEFAULT); |
63 | 3 | } |
64 | |
|
65 | 2 | public JavaBeanDumper(Representer representer, DumperOptions options) { |
66 | 2 | if (representer == null) { |
67 | 1 | throw new NullPointerException("Representer must be provided."); |
68 | |
} |
69 | 1 | if (options == null) { |
70 | 1 | throw new NullPointerException("DumperOptions must be provided."); |
71 | |
} |
72 | 0 | this.options = options; |
73 | 0 | this.representer = representer; |
74 | 0 | this.beanAccess = null; |
75 | |
|
76 | 0 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public void dump(Object data, Writer output) { |
87 | |
DumperOptions doptions; |
88 | 6 | if (this.options == null) { |
89 | 6 | doptions = new DumperOptions(); |
90 | 6 | if (!useGlobalTag) { |
91 | 5 | doptions.setExplicitRoot(Tag.MAP); |
92 | |
} |
93 | 6 | doptions.setDefaultFlowStyle(flowStyle); |
94 | |
} else { |
95 | 0 | doptions = this.options; |
96 | |
} |
97 | |
Representer repr; |
98 | 6 | if (this.representer == null) { |
99 | 6 | repr = new Representer(); |
100 | 6 | repr.getPropertyUtils().setBeanAccess(beanAccess); |
101 | |
} else { |
102 | 0 | repr = this.representer; |
103 | |
} |
104 | 6 | Yaml dumper = new Yaml(repr, doptions); |
105 | 6 | dumper.dump(data, output); |
106 | 6 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public String dump(Object data) { |
116 | 4 | StringWriter buffer = new StringWriter(); |
117 | 4 | dump(data, buffer); |
118 | 4 | return buffer.toString(); |
119 | |
} |
120 | |
|
121 | |
public boolean isUseGlobalTag() { |
122 | 1 | return useGlobalTag; |
123 | |
} |
124 | |
|
125 | |
public void setUseGlobalTag(boolean useGlobalTag) { |
126 | 1 | this.useGlobalTag = useGlobalTag; |
127 | 1 | } |
128 | |
|
129 | |
public FlowStyle getFlowStyle() { |
130 | 1 | return flowStyle; |
131 | |
} |
132 | |
|
133 | |
public void setFlowStyle(FlowStyle flowStyle) { |
134 | 1 | this.flowStyle = flowStyle; |
135 | 1 | } |
136 | |
} |