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