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.nodes; 15 16 import org.yaml.snakeyaml.DumperOptions; 17 import org.yaml.snakeyaml.error.Mark; 18 19 /** 20 * Represents a scalar node. 21 * <p> 22 * Scalar nodes form the leaves in the node graph. 23 * </p> 24 */ 25 public class ScalarNode extends Node { 26 27 private final DumperOptions.ScalarStyle style; 28 private final String value; 29 ScalarNode(Tag tag, String value, Mark startMark, Mark endMark, DumperOptions.ScalarStyle style)30 public ScalarNode(Tag tag, String value, Mark startMark, Mark endMark, 31 DumperOptions.ScalarStyle style) { 32 this(tag, true, value, startMark, endMark, style); 33 } 34 ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark, DumperOptions.ScalarStyle style)35 public ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark, 36 DumperOptions.ScalarStyle style) { 37 super(tag, startMark, endMark); 38 if (value == null) { 39 throw new NullPointerException("value in a Node is required."); 40 } 41 this.value = value; 42 if (style == null) { 43 throw new NullPointerException("Scalar style must be provided."); 44 } 45 this.style = style; 46 this.resolved = resolved; 47 } 48 49 /* 50 * Existed in older versions but replaced with {@link DumperOptions.ScalarStyle}-based 51 * constructor. Restored in v1.22 for backwards compatibility. 52 * 53 * @deprecated Since restored in v1.22. Use {@link ScalarNode#ScalarNode(Tag, String, Mark, Mark, 54 * org.yaml.snakeyaml.DumperOptions.ScalarStyle) }. 55 */ 56 @Deprecated ScalarNode(Tag tag, String value, Mark startMark, Mark endMark, Character style)57 public ScalarNode(Tag tag, String value, Mark startMark, Mark endMark, Character style) { 58 this(tag, value, startMark, endMark, DumperOptions.ScalarStyle.createStyle(style)); 59 } 60 61 /* 62 * Existed in older versions but replaced with {@link DumperOptions.ScalarStyle}-based 63 * constructor. Restored in v1.22 for backwards compatibility. 64 * 65 * @deprecated Since restored in v1.22. Use {@link ScalarNode#ScalarNode(Tag, boolean, String, 66 * Mark, Mark, org.yaml.snakeyaml.DumperOptions.ScalarStyle) }. 67 */ 68 @Deprecated ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark, Character style)69 public ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark, 70 Character style) { 71 this(tag, resolved, value, startMark, endMark, DumperOptions.ScalarStyle.createStyle(style)); 72 } 73 74 /** 75 * Get scalar style of this node. 76 * 77 * @see org.yaml.snakeyaml.events.ScalarEvent 78 * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar Styles</a> 79 * @return style of this scalar node 80 * @deprecated use getScalarStyle instead 81 */ 82 @Deprecated getStyle()83 public Character getStyle() { 84 return style.getChar(); 85 } 86 87 /** 88 * Get scalar style of this node. 89 * 90 * @see org.yaml.snakeyaml.events.ScalarEvent 91 * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar Styles</a> 92 * @return style of this scalar node 93 */ getScalarStyle()94 public DumperOptions.ScalarStyle getScalarStyle() { 95 return style; 96 } 97 98 @Override getNodeId()99 public NodeId getNodeId() { 100 return NodeId.scalar; 101 } 102 103 /** 104 * Value of this scalar. 105 * 106 * @return Scalar's value. 107 */ getValue()108 public String getValue() { 109 return value; 110 } 111 toString()112 public String toString() { 113 return "<" + this.getClass().getName() + " (tag=" + getTag() + ", value=" + getValue() + ")>"; 114 } 115 isPlain()116 public boolean isPlain() { 117 return style == DumperOptions.ScalarStyle.PLAIN; 118 } 119 } 120