1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml;
17
18 import java.util.Map;
19 import java.util.TimeZone;
20
21 import org.yaml.snakeyaml.emitter.Emitter;
22 import org.yaml.snakeyaml.emitter.ScalarAnalysis;
23 import org.yaml.snakeyaml.error.YAMLException;
24 import org.yaml.snakeyaml.nodes.Tag;
25
26 public class DumperOptions {
27
28
29
30
31
32
33
34
35
36
37
38 public enum ScalarStyle {
39 DOUBLE_QUOTED(Character.valueOf('"')), SINGLE_QUOTED(Character.valueOf('\'')), LITERAL(
40 Character.valueOf('|')), FOLDED(Character.valueOf('>')), PLAIN(null);
41 private Character styleChar;
42
43 private ScalarStyle(Character style) {
44 this.styleChar = style;
45 }
46
47 public Character getChar() {
48 return styleChar;
49 }
50
51 @Override
52 public String toString() {
53 return "Scalar style: '" + styleChar + "'";
54 }
55
56 public static ScalarStyle createStyle(Character style) {
57 if (style == null) {
58 return PLAIN;
59 } else {
60 switch (style) {
61 case '"':
62 return DOUBLE_QUOTED;
63 case '\'':
64 return SINGLE_QUOTED;
65 case '|':
66 return LITERAL;
67 case '>':
68 return FOLDED;
69 default:
70 throw new YAMLException("Unknown scalar style character: " + style);
71 }
72 }
73 }
74 }
75
76
77
78
79
80
81
82
83
84 public enum FlowStyle {
85 FLOW(Boolean.TRUE), BLOCK(Boolean.FALSE), AUTO(null);
86
87 private Boolean styleBoolean;
88
89 private FlowStyle(Boolean flowStyle) {
90 styleBoolean = flowStyle;
91 }
92
93 public Boolean getStyleBoolean() {
94 return styleBoolean;
95 }
96
97 @Override
98 public String toString() {
99 return "Flow style: '" + styleBoolean + "'";
100 }
101 }
102
103
104
105
106 public enum LineBreak {
107 WIN("\r\n"), MAC("\r"), UNIX("\n");
108
109 private String lineBreak;
110
111 private LineBreak(String lineBreak) {
112 this.lineBreak = lineBreak;
113 }
114
115 public String getString() {
116 return lineBreak;
117 }
118
119 @Override
120 public String toString() {
121 return "Line break: " + name();
122 }
123
124 public static LineBreak getPlatformLineBreak() {
125 String platformLineBreak = System.getProperty("line.separator");
126 for (LineBreak lb : values()) {
127 if (lb.lineBreak.equals(platformLineBreak)) {
128 return lb;
129 }
130 }
131 return LineBreak.UNIX;
132 }
133 }
134
135
136
137
138 public enum Version {
139 V1_0(new Integer[] { 1, 0 }), V1_1(new Integer[] { 1, 1 });
140
141 private Integer[] version;
142
143 private Version(Integer[] version) {
144 this.version = version;
145 }
146
147 public Integer[] getArray() {
148 return version;
149 }
150
151 public String getRepresentation() {
152 return version[0] + "." + version[1];
153 }
154
155 @Override
156 public String toString() {
157 return "Version: " + getRepresentation();
158 }
159 }
160
161 private ScalarStyle defaultStyle = ScalarStyle.PLAIN;
162 private FlowStyle defaultFlowStyle = FlowStyle.AUTO;
163 private boolean canonical = false;
164 private boolean allowUnicode = true;
165 private boolean allowReadOnlyProperties = false;
166 private int indent = 2;
167 private int bestWidth = 80;
168 private LineBreak lineBreak = LineBreak.UNIX;
169 private boolean explicitStart = false;
170 private boolean explicitEnd = false;
171 private TimeZone timeZone = null;
172
173
174
175
176 private Tag explicitRoot = null;
177 private Version version = null;
178 private Map<String, String> tags = null;
179 private Boolean prettyFlow = false;
180
181 public boolean isAllowUnicode() {
182 return allowUnicode;
183 }
184
185
186
187
188
189
190
191
192
193 public void setAllowUnicode(boolean allowUnicode) {
194 this.allowUnicode = allowUnicode;
195 }
196
197 public ScalarStyle getDefaultScalarStyle() {
198 return defaultStyle;
199 }
200
201
202
203
204
205
206
207
208 public void setDefaultScalarStyle(ScalarStyle defaultStyle) {
209 if (defaultStyle == null) {
210 throw new NullPointerException("Use ScalarStyle enum.");
211 }
212 this.defaultStyle = defaultStyle;
213 }
214
215 public void setIndent(int indent) {
216 if (indent < Emitter.MIN_INDENT) {
217 throw new YAMLException("Indent must be at least " + Emitter.MIN_INDENT);
218 }
219 if (indent > Emitter.MAX_INDENT) {
220 throw new YAMLException("Indent must be at most " + Emitter.MAX_INDENT);
221 }
222 this.indent = indent;
223 }
224
225 public int getIndent() {
226 return this.indent;
227 }
228
229 public void setVersion(Version version) {
230 this.version = version;
231 }
232
233 public Version getVersion() {
234 return this.version;
235 }
236
237
238
239
240
241
242
243 public void setCanonical(boolean canonical) {
244 this.canonical = canonical;
245 }
246
247 public boolean isCanonical() {
248 return this.canonical;
249 }
250
251
252
253
254
255
256
257
258 public void setPrettyFlow(boolean prettyFlow) {
259 this.prettyFlow = prettyFlow;
260 }
261
262 public boolean isPrettyFlow() {
263 return this.prettyFlow;
264 }
265
266
267
268
269
270
271
272
273
274 public void setWidth(int bestWidth) {
275 this.bestWidth = bestWidth;
276 }
277
278 public int getWidth() {
279 return this.bestWidth;
280 }
281
282 public LineBreak getLineBreak() {
283 return lineBreak;
284 }
285
286 public void setDefaultFlowStyle(FlowStyle defaultFlowStyle) {
287 if (defaultFlowStyle == null) {
288 throw new NullPointerException("Use FlowStyle enum.");
289 }
290 this.defaultFlowStyle = defaultFlowStyle;
291 }
292
293 public FlowStyle getDefaultFlowStyle() {
294 return defaultFlowStyle;
295 }
296
297
298
299
300 public Tag getExplicitRoot() {
301 return explicitRoot;
302 }
303
304
305
306
307
308
309
310 public void setExplicitRoot(String expRoot) {
311 setExplicitRoot(new Tag(expRoot));
312 }
313
314
315
316
317
318
319
320 public void setExplicitRoot(Tag expRoot) {
321 if (expRoot == null) {
322 throw new NullPointerException("Root tag must be specified.");
323 }
324 this.explicitRoot = expRoot;
325 }
326
327
328
329
330
331
332 public void setLineBreak(LineBreak lineBreak) {
333 if (lineBreak == null) {
334 throw new NullPointerException("Specify line break.");
335 }
336 this.lineBreak = lineBreak;
337 }
338
339 public boolean isExplicitStart() {
340 return explicitStart;
341 }
342
343 public void setExplicitStart(boolean explicitStart) {
344 this.explicitStart = explicitStart;
345 }
346
347 public boolean isExplicitEnd() {
348 return explicitEnd;
349 }
350
351 public void setExplicitEnd(boolean explicitEnd) {
352 this.explicitEnd = explicitEnd;
353 }
354
355 public Map<String, String> getTags() {
356 return tags;
357 }
358
359
360 public void setTags(Map<String, String> tags) {
361 this.tags = tags;
362 }
363
364
365
366
367
368
369
370
371
372
373
374 public ScalarStyle calculateScalarStyle(ScalarAnalysis analysis, ScalarStyle style) {
375 return style;
376 }
377
378
379
380
381
382
383
384 public boolean isAllowReadOnlyProperties() {
385 return allowReadOnlyProperties;
386 }
387
388
389
390
391
392
393
394
395
396 public void setAllowReadOnlyProperties(boolean allowReadOnlyProperties) {
397 this.allowReadOnlyProperties = allowReadOnlyProperties;
398 }
399
400 public TimeZone getTimeZone() {
401 return timeZone;
402 }
403
404
405
406
407
408 public void setTimeZone(TimeZone timeZone) {
409 this.timeZone = timeZone;
410 }
411 }