1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml; 15 16 import java.util.Map; 17 import java.util.TimeZone; 18 import org.yaml.snakeyaml.emitter.Emitter; 19 import org.yaml.snakeyaml.error.YAMLException; 20 import org.yaml.snakeyaml.serializer.AnchorGenerator; 21 import org.yaml.snakeyaml.serializer.NumberAnchorGenerator; 22 23 public class DumperOptions { 24 25 /** 26 * YAML provides a rich set of scalar styles. Block scalar styles include the literal style and 27 * the folded style; flow scalar styles include the plain style and two quoted styles, the 28 * single-quoted style and the double-quoted style. These styles offer a range of trade-offs 29 * between expressive power and readability. 30 * 31 * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar Styles</a> 32 * @see <a href="http://yaml.org/spec/1.1/#id858081">2.3. Scalars</a> 33 */ 34 public enum ScalarStyle { 35 DOUBLE_QUOTED('"'), SINGLE_QUOTED('\''), LITERAL('|'), FOLDED('>'), PLAIN(null); 36 37 private final Character styleChar; 38 ScalarStyle(Character style)39 ScalarStyle(Character style) { 40 this.styleChar = style; 41 } 42 getChar()43 public Character getChar() { 44 return styleChar; 45 } 46 47 @Override toString()48 public String toString() { 49 return "Scalar style: '" + styleChar + "'"; 50 } 51 createStyle(Character style)52 public static ScalarStyle createStyle(Character style) { 53 if (style == null) { 54 return PLAIN; 55 } else { 56 switch (style) { 57 case '"': 58 return DOUBLE_QUOTED; 59 case '\'': 60 return SINGLE_QUOTED; 61 case '|': 62 return LITERAL; 63 case '>': 64 return FOLDED; 65 default: 66 throw new YAMLException("Unknown scalar style character: " + style); 67 } 68 } 69 } 70 } 71 72 /** 73 * Block styles use indentation to denote nesting and scope within the document. In contrast, flow 74 * styles rely on explicit indicators to denote nesting and scope. 75 * 76 * @see <a href="http://www.yaml.org/spec/current.html#id2509255">3.2.3.1. Node Styles 77 * (http://yaml.org/spec/1.1)</a> 78 */ 79 public enum FlowStyle { 80 FLOW(Boolean.TRUE), BLOCK(Boolean.FALSE), AUTO(null); 81 82 private final Boolean styleBoolean; 83 FlowStyle(Boolean flowStyle)84 FlowStyle(Boolean flowStyle) { 85 styleBoolean = flowStyle; 86 } 87 88 /* 89 * Convenience for legacy constructors that took {@link Boolean} arguments since replaced by 90 * {@link FlowStyle}. Introduced in v1.22 but only to support that for backwards compatibility. 91 * 92 * @deprecated Since restored in v1.22. Use the {@link FlowStyle} constants in your code 93 * instead. 94 */ 95 @Deprecated fromBoolean(Boolean flowStyle)96 public static FlowStyle fromBoolean(Boolean flowStyle) { 97 return flowStyle == null ? AUTO : flowStyle ? FLOW : BLOCK; 98 } 99 getStyleBoolean()100 public Boolean getStyleBoolean() { 101 return styleBoolean; 102 } 103 104 @Override toString()105 public String toString() { 106 return "Flow style: '" + styleBoolean + "'"; 107 } 108 } 109 110 /** 111 * Platform dependent line break. 112 */ 113 public enum LineBreak { 114 WIN("\r\n"), MAC("\r"), UNIX("\n"); 115 116 private final String lineBreak; 117 LineBreak(String lineBreak)118 LineBreak(String lineBreak) { 119 this.lineBreak = lineBreak; 120 } 121 getString()122 public String getString() { 123 return lineBreak; 124 } 125 126 @Override toString()127 public String toString() { 128 return "Line break: " + name(); 129 } 130 getPlatformLineBreak()131 public static LineBreak getPlatformLineBreak() { 132 String platformLineBreak = System.getProperty("line.separator"); 133 for (LineBreak lb : values()) { 134 if (lb.lineBreak.equals(platformLineBreak)) { 135 return lb; 136 } 137 } 138 return LineBreak.UNIX; 139 } 140 } 141 142 /** 143 * Specification version. Currently supported 1.0 and 1.1 144 */ 145 public enum Version { 146 V1_0(new Integer[] {1, 0}), V1_1(new Integer[] {1, 1}); 147 148 private final Integer[] version; 149 Version(Integer[] version)150 Version(Integer[] version) { 151 this.version = version; 152 } 153 major()154 public int major() { 155 return version[0]; 156 } 157 minor()158 public int minor() { 159 return version[1]; 160 } 161 getRepresentation()162 public String getRepresentation() { 163 return version[0] + "." + version[1]; 164 } 165 166 @Override toString()167 public String toString() { 168 return "Version: " + getRepresentation(); 169 } 170 } 171 172 public enum NonPrintableStyle { 173 /** 174 * Transform String to binary if it contains non-printable characters 175 */ 176 BINARY, 177 /** 178 * Escape non-printable characters 179 */ 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 boolean indentWithIndicator = false; 191 private int bestWidth = 80; 192 private boolean splitLines = true; 193 private LineBreak lineBreak = LineBreak.UNIX; 194 private boolean explicitStart = false; 195 private boolean explicitEnd = false; 196 private TimeZone timeZone = null; 197 private int maxSimpleKeyLength = 128; 198 private boolean processComments = false; 199 private NonPrintableStyle nonPrintableStyle = NonPrintableStyle.BINARY; 200 201 private Version version = null; 202 private Map<String, String> tags = null; 203 private Boolean prettyFlow = false; 204 private AnchorGenerator anchorGenerator = new NumberAnchorGenerator(0); 205 isAllowUnicode()206 public boolean isAllowUnicode() { 207 return allowUnicode; 208 } 209 210 /** 211 * Specify whether to emit non-ASCII printable Unicode characters. The default value is true. When 212 * set to false then printable non-ASCII characters (Cyrillic, Chinese etc) will be not printed 213 * but escaped (to support ASCII terminals) 214 * 215 * @param allowUnicode if allowUnicode is false then all non-ASCII characters are escaped 216 */ setAllowUnicode(boolean allowUnicode)217 public void setAllowUnicode(boolean allowUnicode) { 218 this.allowUnicode = allowUnicode; 219 } 220 getDefaultScalarStyle()221 public ScalarStyle getDefaultScalarStyle() { 222 return defaultStyle; 223 } 224 225 /** 226 * Set default style for scalars. See YAML 1.1 specification, 2.3 Scalars 227 * (http://yaml.org/spec/1.1/#id858081) 228 * 229 * @param defaultStyle set the style for all scalars 230 */ setDefaultScalarStyle(ScalarStyle defaultStyle)231 public void setDefaultScalarStyle(ScalarStyle defaultStyle) { 232 if (defaultStyle == null) { 233 throw new NullPointerException("Use ScalarStyle enum."); 234 } 235 this.defaultStyle = defaultStyle; 236 } 237 setIndent(int indent)238 public void setIndent(int indent) { 239 if (indent < Emitter.MIN_INDENT) { 240 throw new YAMLException("Indent must be at least " + Emitter.MIN_INDENT); 241 } 242 if (indent > Emitter.MAX_INDENT) { 243 throw new YAMLException("Indent must be at most " + Emitter.MAX_INDENT); 244 } 245 this.indent = indent; 246 } 247 getIndent()248 public int getIndent() { 249 return this.indent; 250 } 251 252 /** 253 * Set number of white spaces to use for the sequence indicator '-' 254 * 255 * @param indicatorIndent value to be used as indent 256 */ setIndicatorIndent(int indicatorIndent)257 public void setIndicatorIndent(int indicatorIndent) { 258 if (indicatorIndent < 0) { 259 throw new YAMLException("Indicator indent must be non-negative."); 260 } 261 if (indicatorIndent > Emitter.MAX_INDENT - 1) { 262 throw new YAMLException( 263 "Indicator indent must be at most Emitter.MAX_INDENT-1: " + (Emitter.MAX_INDENT - 1)); 264 } 265 this.indicatorIndent = indicatorIndent; 266 } 267 getIndicatorIndent()268 public int getIndicatorIndent() { 269 return this.indicatorIndent; 270 } 271 getIndentWithIndicator()272 public boolean getIndentWithIndicator() { 273 return indentWithIndicator; 274 } 275 276 /** 277 * Set to true to add the indent for sequences to the general indent 278 * 279 * @param indentWithIndicator - true when indent for sequences is added to general 280 */ setIndentWithIndicator(boolean indentWithIndicator)281 public void setIndentWithIndicator(boolean indentWithIndicator) { 282 this.indentWithIndicator = indentWithIndicator; 283 } 284 setVersion(Version version)285 public void setVersion(Version version) { 286 this.version = version; 287 } 288 getVersion()289 public Version getVersion() { 290 return this.version; 291 } 292 293 /** 294 * Force the emitter to produce a canonical YAML document. 295 * 296 * @param canonical true produce canonical YAML document 297 */ setCanonical(boolean canonical)298 public void setCanonical(boolean canonical) { 299 this.canonical = canonical; 300 } 301 isCanonical()302 public boolean isCanonical() { 303 return this.canonical; 304 } 305 306 /** 307 * Force the emitter to produce a pretty YAML document when using the flow style. 308 * 309 * @param prettyFlow true produce pretty flow YAML document 310 */ setPrettyFlow(boolean prettyFlow)311 public void setPrettyFlow(boolean prettyFlow) { 312 this.prettyFlow = prettyFlow; 313 } 314 isPrettyFlow()315 public boolean isPrettyFlow() { 316 return this.prettyFlow; 317 } 318 319 /** 320 * Specify the preferred width to emit scalars. When the scalar representation takes more then the 321 * preferred with the scalar will be split into a few lines. The default is 80. 322 * 323 * @param bestWidth the preferred width for scalars. 324 */ setWidth(int bestWidth)325 public void setWidth(int bestWidth) { 326 this.bestWidth = bestWidth; 327 } 328 getWidth()329 public int getWidth() { 330 return this.bestWidth; 331 } 332 333 /** 334 * Specify whether to split lines exceeding preferred width for scalars. The default is true. 335 * 336 * @param splitLines whether to split lines exceeding preferred width for scalars. 337 */ setSplitLines(boolean splitLines)338 public void setSplitLines(boolean splitLines) { 339 this.splitLines = splitLines; 340 } 341 getSplitLines()342 public boolean getSplitLines() { 343 return this.splitLines; 344 } 345 getLineBreak()346 public LineBreak getLineBreak() { 347 return lineBreak; 348 } 349 setDefaultFlowStyle(FlowStyle defaultFlowStyle)350 public void setDefaultFlowStyle(FlowStyle defaultFlowStyle) { 351 if (defaultFlowStyle == null) { 352 throw new NullPointerException("Use FlowStyle enum."); 353 } 354 this.defaultFlowStyle = defaultFlowStyle; 355 } 356 getDefaultFlowStyle()357 public FlowStyle getDefaultFlowStyle() { 358 return defaultFlowStyle; 359 } 360 361 /** 362 * Specify the line break to separate the lines. It is platform specific: Windows - "\r\n", old 363 * MacOS - "\r", Unix - "\n". The default value is the one for Unix. 364 * 365 * @param lineBreak to be used for the input 366 */ setLineBreak(LineBreak lineBreak)367 public void setLineBreak(LineBreak lineBreak) { 368 if (lineBreak == null) { 369 throw new NullPointerException("Specify line break."); 370 } 371 this.lineBreak = lineBreak; 372 } 373 isExplicitStart()374 public boolean isExplicitStart() { 375 return explicitStart; 376 } 377 setExplicitStart(boolean explicitStart)378 public void setExplicitStart(boolean explicitStart) { 379 this.explicitStart = explicitStart; 380 } 381 isExplicitEnd()382 public boolean isExplicitEnd() { 383 return explicitEnd; 384 } 385 setExplicitEnd(boolean explicitEnd)386 public void setExplicitEnd(boolean explicitEnd) { 387 this.explicitEnd = explicitEnd; 388 } 389 getTags()390 public Map<String, String> getTags() { 391 return tags; 392 } 393 setTags(Map<String, String> tags)394 public void setTags(Map<String, String> tags) { 395 this.tags = tags; 396 } 397 398 /** 399 * Report whether read-only JavaBean properties (the ones without setters) should be included in 400 * the YAML document 401 * 402 * @return false when read-only JavaBean properties are not emitted 403 */ isAllowReadOnlyProperties()404 public boolean isAllowReadOnlyProperties() { 405 return allowReadOnlyProperties; 406 } 407 408 /** 409 * Set to true to include read-only JavaBean properties (the ones without setters) in the YAML 410 * document. By default these properties are not included to be able to parse later the same 411 * JavaBean. 412 * 413 * @param allowReadOnlyProperties - true to dump read-only JavaBean properties 414 */ setAllowReadOnlyProperties(boolean allowReadOnlyProperties)415 public void setAllowReadOnlyProperties(boolean allowReadOnlyProperties) { 416 this.allowReadOnlyProperties = allowReadOnlyProperties; 417 } 418 getTimeZone()419 public TimeZone getTimeZone() { 420 return timeZone; 421 } 422 423 /** 424 * Set the timezone to be used for Date. If set to <code>null</code> UTC is used. 425 * 426 * @param timeZone for created Dates or null to use UTC 427 */ setTimeZone(TimeZone timeZone)428 public void setTimeZone(TimeZone timeZone) { 429 this.timeZone = timeZone; 430 } 431 432 getAnchorGenerator()433 public AnchorGenerator getAnchorGenerator() { 434 return anchorGenerator; 435 } 436 setAnchorGenerator(AnchorGenerator anchorGenerator)437 public void setAnchorGenerator(AnchorGenerator anchorGenerator) { 438 this.anchorGenerator = anchorGenerator; 439 } 440 getMaxSimpleKeyLength()441 public int getMaxSimpleKeyLength() { 442 return maxSimpleKeyLength; 443 } 444 445 /** 446 * Define max key length to use simple key (without '?') More info 447 * https://yaml.org/spec/1.1/#id934537 448 * 449 * @param maxSimpleKeyLength - the limit after which the key gets explicit key indicator '?' 450 */ setMaxSimpleKeyLength(int maxSimpleKeyLength)451 public void setMaxSimpleKeyLength(int maxSimpleKeyLength) { 452 if (maxSimpleKeyLength > 1024) { 453 throw new YAMLException( 454 "The simple key must not span more than 1024 stream characters. See https://yaml.org/spec/1.1/#id934537"); 455 } 456 this.maxSimpleKeyLength = maxSimpleKeyLength; 457 } 458 459 /** 460 * Set the comment processing. By default comments are ignored. 461 * 462 * @param processComments <code>true</code> to process; <code>false</code> to ignore 463 */ 464 setProcessComments(boolean processComments)465 public void setProcessComments(boolean processComments) { 466 this.processComments = processComments; 467 } 468 isProcessComments()469 public boolean isProcessComments() { 470 return processComments; 471 } 472 getNonPrintableStyle()473 public NonPrintableStyle getNonPrintableStyle() { 474 return this.nonPrintableStyle; 475 } 476 477 /** 478 * When String contains non-printable characters SnakeYAML convert it to binary data with the 479 * !!binary tag. Set this to ESCAPE to keep the !!str tag and escape the non-printable chars with 480 * \\x or \\u 481 * 482 * @param style ESCAPE to force SnakeYAML to keep !!str tag for non-printable data 483 */ setNonPrintableStyle(NonPrintableStyle style)484 public void setNonPrintableStyle(NonPrintableStyle style) { 485 this.nonPrintableStyle = style; 486 } 487 } 488