aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/yaml/snakeyaml/DumperOptions.java
blob: 48b5e096ca13de3ec9fd7d8a8234d1a80d8380da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml;

import java.util.Map;
import java.util.TimeZone;
import org.yaml.snakeyaml.emitter.Emitter;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.serializer.AnchorGenerator;
import org.yaml.snakeyaml.serializer.NumberAnchorGenerator;

public class DumperOptions {

  /**
   * YAML provides a rich set of scalar styles. Block scalar styles include the literal style and
   * the folded style; flow scalar styles include the plain style and two quoted styles, the
   * single-quoted style and the double-quoted style. These styles offer a range of trade-offs
   * between expressive power and readability.
   *
   * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar Styles</a>
   * @see <a href="http://yaml.org/spec/1.1/#id858081">2.3. Scalars</a>
   */
  public enum ScalarStyle {
    DOUBLE_QUOTED('"'), SINGLE_QUOTED('\''), LITERAL('|'), FOLDED('>'), PLAIN(null);

    private final Character styleChar;

    ScalarStyle(Character style) {
      this.styleChar = style;
    }

    public Character getChar() {
      return styleChar;
    }

    @Override
    public String toString() {
      return "Scalar style: '" + styleChar + "'";
    }

    public static ScalarStyle createStyle(Character style) {
      if (style == null) {
        return PLAIN;
      } else {
        switch (style) {
          case '"':
            return DOUBLE_QUOTED;
          case '\'':
            return SINGLE_QUOTED;
          case '|':
            return LITERAL;
          case '>':
            return FOLDED;
          default:
            throw new YAMLException("Unknown scalar style character: " + style);
        }
      }
    }
  }

  /**
   * Block styles use indentation to denote nesting and scope within the document. In contrast, flow
   * styles rely on explicit indicators to denote nesting and scope.
   *
   * @see <a href="http://www.yaml.org/spec/current.html#id2509255">3.2.3.1. Node Styles
   *      (http://yaml.org/spec/1.1)</a>
   */
  public enum FlowStyle {
    FLOW(Boolean.TRUE), BLOCK(Boolean.FALSE), AUTO(null);

    private final Boolean styleBoolean;

    FlowStyle(Boolean flowStyle) {
      styleBoolean = flowStyle;
    }

    /*
     * Convenience for legacy constructors that took {@link Boolean} arguments since replaced by
     * {@link FlowStyle}. Introduced in v1.22 but only to support that for backwards compatibility.
     *
     * @deprecated Since restored in v1.22. Use the {@link FlowStyle} constants in your code
     * instead.
     */
    @Deprecated
    public static FlowStyle fromBoolean(Boolean flowStyle) {
      return flowStyle == null ? AUTO : flowStyle ? FLOW : BLOCK;
    }

    public Boolean getStyleBoolean() {
      return styleBoolean;
    }

    @Override
    public String toString() {
      return "Flow style: '" + styleBoolean + "'";
    }
  }

  /**
   * Platform dependent line break.
   */
  public enum LineBreak {
    WIN("\r\n"), MAC("\r"), UNIX("\n");

    private final String lineBreak;

    LineBreak(String lineBreak) {
      this.lineBreak = lineBreak;
    }

    public String getString() {
      return lineBreak;
    }

    @Override
    public String toString() {
      return "Line break: " + name();
    }

    public static LineBreak getPlatformLineBreak() {
      String platformLineBreak = System.getProperty("line.separator");
      for (LineBreak lb : values()) {
        if (lb.lineBreak.equals(platformLineBreak)) {
          return lb;
        }
      }
      return LineBreak.UNIX;
    }
  }

  /**
   * Specification version. Currently supported 1.0 and 1.1
   */
  public enum Version {
    V1_0(new Integer[] {1, 0}), V1_1(new Integer[] {1, 1});

    private final Integer[] version;

    Version(Integer[] version) {
      this.version = version;
    }

    public int major() {
      return version[0];
    }

    public int minor() {
      return version[1];
    }

    public String getRepresentation() {
      return version[0] + "." + version[1];
    }

    @Override
    public String toString() {
      return "Version: " + getRepresentation();
    }
  }

  public enum NonPrintableStyle {
    /**
     * Transform String to binary if it contains non-printable characters
     */
    BINARY,
    /**
     * Escape non-printable characters
     */
    ESCAPE
  }

  private ScalarStyle defaultStyle = ScalarStyle.PLAIN;
  private FlowStyle defaultFlowStyle = FlowStyle.AUTO;
  private boolean canonical = false;
  private boolean allowUnicode = true;
  private boolean allowReadOnlyProperties = false;
  private int indent = 2;
  private int indicatorIndent = 0;
  private boolean indentWithIndicator = false;
  private int bestWidth = 80;
  private boolean splitLines = true;
  private LineBreak lineBreak = LineBreak.UNIX;
  private boolean explicitStart = false;
  private boolean explicitEnd = false;
  private TimeZone timeZone = null;
  private int maxSimpleKeyLength = 128;
  private boolean processComments = false;
  private NonPrintableStyle nonPrintableStyle = NonPrintableStyle.BINARY;

  private Version version = null;
  private Map<String, String> tags = null;
  private Boolean prettyFlow = false;
  private AnchorGenerator anchorGenerator = new NumberAnchorGenerator(0);

  public boolean isAllowUnicode() {
    return allowUnicode;
  }

  /**
   * Specify whether to emit non-ASCII printable Unicode characters. The default value is true. When
   * set to false then printable non-ASCII characters (Cyrillic, Chinese etc) will be not printed
   * but escaped (to support ASCII terminals)
   *
   * @param allowUnicode if allowUnicode is false then all non-ASCII characters are escaped
   */
  public void setAllowUnicode(boolean allowUnicode) {
    this.allowUnicode = allowUnicode;
  }

  public ScalarStyle getDefaultScalarStyle() {
    return defaultStyle;
  }

  /**
   * Set default style for scalars. See YAML 1.1 specification, 2.3 Scalars
   * (http://yaml.org/spec/1.1/#id858081)
   *
   * @param defaultStyle set the style for all scalars
   */
  public void setDefaultScalarStyle(ScalarStyle defaultStyle) {
    if (defaultStyle == null) {
      throw new NullPointerException("Use ScalarStyle enum.");
    }
    this.defaultStyle = defaultStyle;
  }

  public void setIndent(int indent) {
    if (indent < Emitter.MIN_INDENT) {
      throw new YAMLException("Indent must be at least " + Emitter.MIN_INDENT);
    }
    if (indent > Emitter.MAX_INDENT) {
      throw new YAMLException("Indent must be at most " + Emitter.MAX_INDENT);
    }
    this.indent = indent;
  }

  public int getIndent() {
    return this.indent;
  }

  /**
   * Set number of white spaces to use for the sequence indicator '-'
   *
   * @param indicatorIndent value to be used as indent
   */
  public void setIndicatorIndent(int indicatorIndent) {
    if (indicatorIndent < 0) {
      throw new YAMLException("Indicator indent must be non-negative.");
    }
    if (indicatorIndent > Emitter.MAX_INDENT - 1) {
      throw new YAMLException(
          "Indicator indent must be at most Emitter.MAX_INDENT-1: " + (Emitter.MAX_INDENT - 1));
    }
    this.indicatorIndent = indicatorIndent;
  }

  public int getIndicatorIndent() {
    return this.indicatorIndent;
  }

  public boolean getIndentWithIndicator() {
    return indentWithIndicator;
  }

  /**
   * Set to true to add the indent for sequences to the general indent
   *
   * @param indentWithIndicator - true when indent for sequences is added to general
   */
  public void setIndentWithIndicator(boolean indentWithIndicator) {
    this.indentWithIndicator = indentWithIndicator;
  }

  public void setVersion(Version version) {
    this.version = version;
  }

  public Version getVersion() {
    return this.version;
  }

  /**
   * Force the emitter to produce a canonical YAML document.
   *
   * @param canonical true produce canonical YAML document
   */
  public void setCanonical(boolean canonical) {
    this.canonical = canonical;
  }

  public boolean isCanonical() {
    return this.canonical;
  }

  /**
   * Force the emitter to produce a pretty YAML document when using the flow style.
   *
   * @param prettyFlow true produce pretty flow YAML document
   */
  public void setPrettyFlow(boolean prettyFlow) {
    this.prettyFlow = prettyFlow;
  }

  public boolean isPrettyFlow() {
    return this.prettyFlow;
  }

  /**
   * Specify the preferred width to emit scalars. When the scalar representation takes more then the
   * preferred with the scalar will be split into a few lines. The default is 80.
   *
   * @param bestWidth the preferred width for scalars.
   */
  public void setWidth(int bestWidth) {
    this.bestWidth = bestWidth;
  }

  public int getWidth() {
    return this.bestWidth;
  }

  /**
   * Specify whether to split lines exceeding preferred width for scalars. The default is true.
   *
   * @param splitLines whether to split lines exceeding preferred width for scalars.
   */
  public void setSplitLines(boolean splitLines) {
    this.splitLines = splitLines;
  }

  public boolean getSplitLines() {
    return this.splitLines;
  }

  public LineBreak getLineBreak() {
    return lineBreak;
  }

  public void setDefaultFlowStyle(FlowStyle defaultFlowStyle) {
    if (defaultFlowStyle == null) {
      throw new NullPointerException("Use FlowStyle enum.");
    }
    this.defaultFlowStyle = defaultFlowStyle;
  }

  public FlowStyle getDefaultFlowStyle() {
    return defaultFlowStyle;
  }

  /**
   * Specify the line break to separate the lines. It is platform specific: Windows - "\r\n", old
   * MacOS - "\r", Unix - "\n". The default value is the one for Unix.
   *
   * @param lineBreak to be used for the input
   */
  public void setLineBreak(LineBreak lineBreak) {
    if (lineBreak == null) {
      throw new NullPointerException("Specify line break.");
    }
    this.lineBreak = lineBreak;
  }

  public boolean isExplicitStart() {
    return explicitStart;
  }

  public void setExplicitStart(boolean explicitStart) {
    this.explicitStart = explicitStart;
  }

  public boolean isExplicitEnd() {
    return explicitEnd;
  }

  public void setExplicitEnd(boolean explicitEnd) {
    this.explicitEnd = explicitEnd;
  }

  public Map<String, String> getTags() {
    return tags;
  }

  public void setTags(Map<String, String> tags) {
    this.tags = tags;
  }

  /**
   * Report whether read-only JavaBean properties (the ones without setters) should be included in
   * the YAML document
   *
   * @return false when read-only JavaBean properties are not emitted
   */
  public boolean isAllowReadOnlyProperties() {
    return allowReadOnlyProperties;
  }

  /**
   * Set to true to include read-only JavaBean properties (the ones without setters) in the YAML
   * document. By default these properties are not included to be able to parse later the same
   * JavaBean.
   *
   * @param allowReadOnlyProperties - true to dump read-only JavaBean properties
   */
  public void setAllowReadOnlyProperties(boolean allowReadOnlyProperties) {
    this.allowReadOnlyProperties = allowReadOnlyProperties;
  }

  public TimeZone getTimeZone() {
    return timeZone;
  }

  /**
   * Set the timezone to be used for Date. If set to <code>null</code> UTC is used.
   *
   * @param timeZone for created Dates or null to use UTC
   */
  public void setTimeZone(TimeZone timeZone) {
    this.timeZone = timeZone;
  }


  public AnchorGenerator getAnchorGenerator() {
    return anchorGenerator;
  }

  public void setAnchorGenerator(AnchorGenerator anchorGenerator) {
    this.anchorGenerator = anchorGenerator;
  }

  public int getMaxSimpleKeyLength() {
    return maxSimpleKeyLength;
  }

  /**
   * Define max key length to use simple key (without '?') More info
   * https://yaml.org/spec/1.1/#id934537
   *
   * @param maxSimpleKeyLength - the limit after which the key gets explicit key indicator '?'
   */
  public void setMaxSimpleKeyLength(int maxSimpleKeyLength) {
    if (maxSimpleKeyLength > 1024) {
      throw new YAMLException(
          "The simple key must not span more than 1024 stream characters. See https://yaml.org/spec/1.1/#id934537");
    }
    this.maxSimpleKeyLength = maxSimpleKeyLength;
  }

  /**
   * Set the comment processing. By default comments are ignored.
   *
   * @param processComments <code>true</code> to process; <code>false</code> to ignore
   */

  public void setProcessComments(boolean processComments) {
    this.processComments = processComments;
  }

  public boolean isProcessComments() {
    return processComments;
  }

  public NonPrintableStyle getNonPrintableStyle() {
    return this.nonPrintableStyle;
  }

  /**
   * When String contains non-printable characters SnakeYAML convert it to binary data with the
   * !!binary tag. Set this to ESCAPE to keep the !!str tag and escape the non-printable chars with
   * \\x or \\u
   *
   * @param style ESCAPE to force SnakeYAML to keep !!str tag for non-printable data
   */
  public void setNonPrintableStyle(NonPrintableStyle style) {
    this.nonPrintableStyle = style;
  }
}