| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.yaml.snakeyaml.representer; |
| 17 | |
|
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.HashMap; |
| 20 | |
import java.util.IdentityHashMap; |
| 21 | |
import java.util.LinkedHashMap; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Map; |
| 24 | |
|
| 25 | |
import org.yaml.snakeyaml.DumperOptions.FlowStyle; |
| 26 | |
import org.yaml.snakeyaml.DumperOptions.ScalarStyle; |
| 27 | |
import org.yaml.snakeyaml.error.YAMLException; |
| 28 | |
import org.yaml.snakeyaml.introspector.PropertyUtils; |
| 29 | |
import org.yaml.snakeyaml.nodes.AnchorNode; |
| 30 | |
import org.yaml.snakeyaml.nodes.MappingNode; |
| 31 | |
import org.yaml.snakeyaml.nodes.Node; |
| 32 | |
import org.yaml.snakeyaml.nodes.NodeTuple; |
| 33 | |
import org.yaml.snakeyaml.nodes.ScalarNode; |
| 34 | |
import org.yaml.snakeyaml.nodes.SequenceNode; |
| 35 | |
import org.yaml.snakeyaml.nodes.Tag; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 132938 | public abstract class BaseRepresenter { |
| 41 | 132938 | protected final Map<Class<?>, Represent> representers = new HashMap<Class<?>, Represent>(); |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
protected Represent nullRepresenter; |
| 48 | |
|
| 49 | 132938 | protected final Map<Class<?>, Represent> multiRepresenters = new LinkedHashMap<Class<?>, Represent>(); |
| 50 | |
protected Character defaultScalarStyle; |
| 51 | 132938 | protected FlowStyle defaultFlowStyle = FlowStyle.AUTO; |
| 52 | 132938 | protected final Map<Object, Node> representedObjects = new IdentityHashMap<Object, Node>() { |
| 53 | |
private static final long serialVersionUID = -5576159264232131854L; |
| 54 | |
|
| 55 | 204396 | public Node put(Object key, Node value) { |
| 56 | 71458 | return super.put(key, new AnchorNode(value)); |
| 57 | |
} |
| 58 | |
}; |
| 59 | |
|
| 60 | |
protected Object objectToRepresent; |
| 61 | |
private PropertyUtils propertyUtils; |
| 62 | 132938 | private boolean explicitPropertyUtils = false; |
| 63 | |
|
| 64 | |
public Node represent(Object data) { |
| 65 | 81949 | Node node = representData(data); |
| 66 | 81939 | representedObjects.clear(); |
| 67 | 81939 | objectToRepresent = null; |
| 68 | 81939 | return node; |
| 69 | |
} |
| 70 | |
|
| 71 | |
protected final Node representData(Object data) { |
| 72 | 744884 | objectToRepresent = data; |
| 73 | |
|
| 74 | 744884 | if (representedObjects.containsKey(objectToRepresent)) { |
| 75 | 7133 | Node node = representedObjects.get(objectToRepresent); |
| 76 | 7133 | return node; |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | 737751 | if (data == null) { |
| 81 | 180 | Node node = nullRepresenter.representData(null); |
| 82 | 180 | return node; |
| 83 | |
} |
| 84 | |
|
| 85 | |
Node node; |
| 86 | 737571 | Class<?> clazz = data.getClass(); |
| 87 | 737571 | if (representers.containsKey(clazz)) { |
| 88 | 522394 | Represent representer = representers.get(clazz); |
| 89 | 522394 | node = representer.representData(data); |
| 90 | 522393 | } else { |
| 91 | |
|
| 92 | 215177 | for (Class<?> repr : multiRepresenters.keySet()) { |
| 93 | 612995 | if (repr.isInstance(data)) { |
| 94 | 170147 | Represent representer = multiRepresenters.get(repr); |
| 95 | 170147 | node = representer.representData(data); |
| 96 | 170145 | return node; |
| 97 | |
} |
| 98 | |
} |
| 99 | |
|
| 100 | 45030 | if (clazz.isArray()) { |
| 101 | 2 | throw new YAMLException("Arrays of primitives are not fully supported."); |
| 102 | |
} |
| 103 | |
|
| 104 | 45028 | if (multiRepresenters.containsKey(null)) { |
| 105 | 0 | Represent representer = multiRepresenters.get(null); |
| 106 | 0 | node = representer.representData(data); |
| 107 | 0 | } else { |
| 108 | 45028 | Represent representer = representers.get(null); |
| 109 | 45028 | node = representer.representData(data); |
| 110 | |
} |
| 111 | |
} |
| 112 | 563773 | return node; |
| 113 | |
} |
| 114 | |
|
| 115 | |
protected Node representScalar(Tag tag, String value, Character style) { |
| 116 | 666285 | if (style == null) { |
| 117 | 658131 | style = this.defaultScalarStyle; |
| 118 | |
} |
| 119 | 666285 | Node node = new ScalarNode(tag, value, null, null, style); |
| 120 | 666284 | return node; |
| 121 | |
} |
| 122 | |
|
| 123 | |
protected Node representScalar(Tag tag, String value) { |
| 124 | 143867 | return representScalar(tag, value, null); |
| 125 | |
} |
| 126 | |
|
| 127 | |
protected Node representSequence(Tag tag, Iterable<? extends Object> sequence, Boolean flowStyle) { |
| 128 | 16228 | int size = 10; |
| 129 | 16228 | if (sequence instanceof List<?>) { |
| 130 | 16221 | size = ((List<?>) sequence).size(); |
| 131 | |
} |
| 132 | 16228 | List<Node> value = new ArrayList<Node>(size); |
| 133 | 16228 | SequenceNode node = new SequenceNode(tag, value, flowStyle); |
| 134 | 16228 | representedObjects.put(objectToRepresent, node); |
| 135 | 16228 | boolean bestStyle = true; |
| 136 | 16228 | for (Object item : sequence) { |
| 137 | 114692 | Node nodeItem = representData(item); |
| 138 | 114692 | if (!((nodeItem instanceof ScalarNode && ((ScalarNode) nodeItem).getStyle() == null))) { |
| 139 | 14202 | bestStyle = false; |
| 140 | |
} |
| 141 | 114692 | value.add(nodeItem); |
| 142 | 114692 | } |
| 143 | 16228 | if (flowStyle == null) { |
| 144 | 16220 | if (defaultFlowStyle != FlowStyle.AUTO) { |
| 145 | 13065 | node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); |
| 146 | |
} else { |
| 147 | 3155 | node.setFlowStyle(bestStyle); |
| 148 | |
} |
| 149 | |
} |
| 150 | 16228 | return node; |
| 151 | |
} |
| 152 | |
|
| 153 | |
protected Node representMapping(Tag tag, Map<? extends Object, Object> mapping, |
| 154 | |
Boolean flowStyle) { |
| 155 | 10206 | List<NodeTuple> value = new ArrayList<NodeTuple>(mapping.size()); |
| 156 | 10206 | MappingNode node = new MappingNode(tag, value, flowStyle); |
| 157 | 10206 | representedObjects.put(objectToRepresent, node); |
| 158 | 10206 | boolean bestStyle = true; |
| 159 | 10206 | for (Map.Entry<? extends Object, Object> entry : mapping.entrySet()) { |
| 160 | 100418 | Node nodeKey = representData(entry.getKey()); |
| 161 | 100418 | Node nodeValue = representData(entry.getValue()); |
| 162 | 100418 | if (!((nodeKey instanceof ScalarNode && ((ScalarNode) nodeKey).getStyle() == null))) { |
| 163 | 52 | bestStyle = false; |
| 164 | |
} |
| 165 | 100418 | if (!((nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null))) { |
| 166 | 116 | bestStyle = false; |
| 167 | |
} |
| 168 | 100418 | value.add(new NodeTuple(nodeKey, nodeValue)); |
| 169 | 100418 | } |
| 170 | 10206 | if (flowStyle == null) { |
| 171 | 10202 | if (defaultFlowStyle != FlowStyle.AUTO) { |
| 172 | 10058 | node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); |
| 173 | |
} else { |
| 174 | 144 | node.setFlowStyle(bestStyle); |
| 175 | |
} |
| 176 | |
} |
| 177 | 10206 | return node; |
| 178 | |
} |
| 179 | |
|
| 180 | |
public void setDefaultScalarStyle(ScalarStyle defaultStyle) { |
| 181 | 132936 | this.defaultScalarStyle = defaultStyle.getChar(); |
| 182 | 132936 | } |
| 183 | |
|
| 184 | |
public void setDefaultFlowStyle(FlowStyle defaultFlowStyle) { |
| 185 | 159068 | this.defaultFlowStyle = defaultFlowStyle; |
| 186 | 159068 | } |
| 187 | |
|
| 188 | |
public FlowStyle getDefaultFlowStyle() { |
| 189 | 13067 | return this.defaultFlowStyle; |
| 190 | |
} |
| 191 | |
|
| 192 | |
public void setPropertyUtils(PropertyUtils propertyUtils) { |
| 193 | 5 | this.propertyUtils = propertyUtils; |
| 194 | 5 | this.explicitPropertyUtils = true; |
| 195 | 5 | } |
| 196 | |
|
| 197 | |
public final PropertyUtils getPropertyUtils() { |
| 198 | 310933 | if (propertyUtils == null) { |
| 199 | 132931 | propertyUtils = new PropertyUtils(); |
| 200 | |
} |
| 201 | 310933 | return propertyUtils; |
| 202 | |
} |
| 203 | |
|
| 204 | |
public final boolean isExplicitPropertyUtils() { |
| 205 | 2 | return explicitPropertyUtils; |
| 206 | |
} |
| 207 | |
} |