List<NodeTuple> nodeValue = node.getValue();
// reversed for http://code.google.com/p/snakeyaml/issues/detail?id=139
Collections.reverse(nodeValue);
for (Iterator<NodeTuple> iter = nodeValue.iterator(); iter.hasNext();) {
final NodeTuple nodeTuple = iter.next();
final Node keyNode = nodeTuple.getKeyNode();
final Node valueNode = nodeTuple.getValueNode();
if (keyNode.getTag().equals(Tag.MERGE)) {
iter.remove();
switch (valueNode.getNodeId()) {
case mapping:
MappingNode mn = (MappingNode) valueNode;
mergeNode(mn, false, key2index, values);
break;
case sequence:
SequenceNode sn = (SequenceNode) valueNode;
List<Node> vals = sn.getValue();
for (Node subnode : vals) {
if (!(subnode instanceof MappingNode)) {
throw new ConstructorException("while constructing a mapping",
node.getStartMark(),
"expected a mapping for merging, but found "
+ subnode.getNodeId(), subnode.getStartMark());
}
MappingNode mnode = (MappingNode) subnode;
mergeNode(mnode, false, key2index, values);
}
break;
default:
throw new ConstructorException("while constructing a mapping",
node.getStartMark(),
"expected a mapping or list of mappings for merging, but found "
+ valueNode.getNodeId(), valueNode.getStartMark());
}
} else {
// we need to construct keys to avoid duplications
Object key = constructObject(keyNode);
if (!key2index.containsKey(key)) { // 1st time merging key