1
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.error.YAMLException;
23 import org.yaml.snakeyaml.serializer.AnchorGenerator;
24 import org.yaml.snakeyaml.serializer.NumberAnchorGenerator;
25
26 public class DumperOptions {
27
38 public enum ScalarStyle {
39 DOUBLE_QUOTED('"'), SINGLE_QUOTED('\''), LITERAL(
40 '|'), FOLDED('>'), 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
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
98 @Deprecated
99 public static FlowStyle fromBoolean(Boolean flowStyle) {
100 return flowStyle==null ? AUTO
101 : flowStyle ? FLOW
102 : BLOCK;
103 }
104
105 public Boolean getStyleBoolean() {
106 return styleBoolean;
107 }
108
109 @Override
110 public String toString() {
111 return "Flow style: '" + styleBoolean + "'";
112 }
113 }
114
115
118 public enum LineBreak {
119 WIN("\r\n"), MAC("\r"), UNIX("\n");
120
121 private String lineBreak;
122
123 private LineBreak(String lineBreak) {
124 this.lineBreak = lineBreak;
125 }
126
127 public String getString() {
128 return lineBreak;
129 }
130
131 @Override
132 public String toString() {
133 return "Line break: " + name();
134 }
135
136 public static LineBreak getPlatformLineBreak() {
137 String platformLineBreak = System.getProperty("line.separator");
138 for (LineBreak lb : values()) {
139 if (lb.lineBreak.equals(platformLineBreak)) {
140 return lb;
141 }
142 }
143 return LineBreak.UNIX;
144 }
145 }
146
147
150 public enum Version {
151 V1_0(new Integer[] { 1, 0 }), V1_1(new Integer[] { 1, 1 });
152
153 private Integer[] version;
154
155 private Version(Integer[] version) {
156 this.version = version;
157 }
158
159 public int major() { return version[0]; }
160 public int minor() { return version[1]; }
161
162 public String getRepresentation() {
163 return version[0] + "." + version[1];
164 }
165
166 @Override
167 public String toString() {
168 return "Version: " + getRepresentation();
169 }
170 }
171
172 public enum NonPrintableStyle {
173
176 BINARY,
177
180 ESCAPE
181 }
182
183 private ScalarStyle defaultStyle = ScalarStyle.PLAIN;
184 private FlowStyle defaultFlowStyle = FlowStyle.AUTO;
185 private boolean canonical = false;
186 private boolean allowUnicode = true;
187 private boolean allowReadOnlyProperties = false;
188 private int indent = 2;
189 private int indicatorIndent = 0;
190 private int bestWidth = 80;
191 private boolean splitLines = true;
192 private LineBreak lineBreak = LineBreak.UNIX;
193 private boolean explicitStart = false;
194 private boolean explicitEnd = false;
195 private TimeZone timeZone = null;
196 private int maxSimpleKeyLength = 128;
197 private NonPrintableStyle nonPrintableStyle = NonPrintableStyle.BINARY;
198
199 private Version version = null;
200 private Map<String, String> tags = null;
201 private Boolean prettyFlow = false;
202 private AnchorGenerator anchorGenerator = new NumberAnchorGenerator(0);
203
204 public boolean isAllowUnicode() {
205 return allowUnicode;
206 }
207
208
218 public void setAllowUnicode(boolean allowUnicode) {
219 this.allowUnicode = allowUnicode;
220 }
221
222 public ScalarStyle getDefaultScalarStyle() {
223 return defaultStyle;
224 }
225
226
233 public void setDefaultScalarStyle(ScalarStyle defaultStyle) {
234 if (defaultStyle == null) {
235 throw new NullPointerException("Use ScalarStyle enum.");
236 }
237 this.defaultStyle = defaultStyle;
238 }
239
240 public void setIndent(int indent) {
241 if (indent < Emitter.MIN_INDENT) {
242 throw new YAMLException("Indent must be at least " + Emitter.MIN_INDENT);
243 }
244 if (indent > Emitter.MAX_INDENT) {
245 throw new YAMLException("Indent must be at most " + Emitter.MAX_INDENT);
246 }
247 this.indent = indent;
248 }
249
250 public int getIndent() {
251 return this.indent;
252 }
253
254 public void setIndicatorIndent(int indicatorIndent) {
255 if (indicatorIndent < 0) {
256 throw new YAMLException("Indicator indent must be non-negative.");
257 }
258 if (indicatorIndent > Emitter.MAX_INDENT - 1) {
259 throw new YAMLException("Indicator indent must be at most Emitter.MAX_INDENT-1: " + (Emitter.MAX_INDENT - 1));
260 }
261 this.indicatorIndent = indicatorIndent;
262 }
263
264 public int getIndicatorIndent() {
265 return this.indicatorIndent;
266 }
267
268 public void setVersion(Version version) {
269 this.version = version;
270 }
271
272 public Version getVersion() {
273 return this.version;
274 }
275
276
282 public void setCanonical(boolean canonical) {
283 this.canonical = canonical;
284 }
285
286 public boolean isCanonical() {
287 return this.canonical;
288 }
289
290
297 public void setPrettyFlow(boolean prettyFlow) {
298 this.prettyFlow = prettyFlow;
299 }
300
301 public boolean isPrettyFlow() {
302 return this.prettyFlow;
303 }
304
305
313 public void setWidth(int bestWidth) {
314 this.bestWidth = bestWidth;
315 }
316
317 public int getWidth() {
318 return this.bestWidth;
319 }
320
321
328 public void setSplitLines(boolean splitLines) {
329 this.splitLines = splitLines;
330 }
331
332 public boolean getSplitLines() {
333 return this.splitLines;
334 }
335
336 public LineBreak getLineBreak() {
337 return lineBreak;
338 }
339
340 public void setDefaultFlowStyle(FlowStyle defaultFlowStyle) {
341 if (defaultFlowStyle == null) {
342 throw new NullPointerException("Use FlowStyle enum.");
343 }
344 this.defaultFlowStyle = defaultFlowStyle;
345 }
346
347 public FlowStyle getDefaultFlowStyle() {
348 return defaultFlowStyle;
349 }
350
351
357 public void setLineBreak(LineBreak lineBreak) {
358 if (lineBreak == null) {
359 throw new NullPointerException("Specify line break.");
360 }
361 this.lineBreak = lineBreak;
362 }
363
364 public boolean isExplicitStart() {
365 return explicitStart;
366 }
367
368 public void setExplicitStart(boolean explicitStart) {
369 this.explicitStart = explicitStart;
370 }
371
372 public boolean isExplicitEnd() {
373 return explicitEnd;
374 }
375
376 public void setExplicitEnd(boolean explicitEnd) {
377 this.explicitEnd = explicitEnd;
378 }
379
380 public Map<String, String> getTags() {
381 return tags;
382 }
383
384 public void setTags(Map<String, String> tags) {
385 this.tags = tags;
386 }
387
388
394 public boolean isAllowReadOnlyProperties() {
395 return allowReadOnlyProperties;
396 }
397
398
406 public void setAllowReadOnlyProperties(boolean allowReadOnlyProperties) {
407 this.allowReadOnlyProperties = allowReadOnlyProperties;
408 }
409
410 public TimeZone getTimeZone() {
411 return timeZone;
412 }
413
414
419 public void setTimeZone(TimeZone timeZone) {
420 this.timeZone = timeZone;
421 }
422
423
424 public AnchorGenerator getAnchorGenerator() {
425 return anchorGenerator;
426 }
427
428 public void setAnchorGenerator(AnchorGenerator anchorGenerator) {
429 this.anchorGenerator = anchorGenerator;
430 }
431
432 public int getMaxSimpleKeyLength() {
433 return maxSimpleKeyLength;
434 }
435
436
441 public void setMaxSimpleKeyLength(int maxSimpleKeyLength) {
442 if(maxSimpleKeyLength > 1024) {
443 throw new YAMLException("The simple key must not span more than 1024 stream characters. See https:);
444 }
445 this.maxSimpleKeyLength = maxSimpleKeyLength;
446 }
447
448 public NonPrintableStyle getNonPrintableStyle() {
449 return this.nonPrintableStyle;
450 }
451
452
457 public void setNonPrintableStyle(NonPrintableStyle style) {
458 this.nonPrintableStyle = style;
459 }
460 }
461