Bug 8675: Fix a design flaw of the new XML parser
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ListEntryNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.util;
9
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.LinkedHashMap;
15 import java.util.Map;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22
23 /**
24  * Utility class used for tracking parser state as needed by a StAX-like parser.
25  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
26  *
27  * <p>
28  * Represents a YANG list entry node.
29  */
30 public class ListEntryNodeDataWithSchema extends CompositeNodeDataWithSchema {
31
32     private final Map<QName, SimpleNodeDataWithSchema> qnameToKeys = new HashMap<>();
33
34     public ListEntryNodeDataWithSchema(final DataSchemaNode schema) {
35         super(schema);
36     }
37
38     @Override
39     public void addChild(final AbstractNodeDataWithSchema newChild) {
40         final DataSchemaNode childSchema = newChild.getSchema();
41         if (childSchema instanceof LeafSchemaNode && isPartOfKey((LeafSchemaNode) childSchema)) {
42             qnameToKeys.put(childSchema.getQName(), (SimpleNodeDataWithSchema)newChild);
43         }
44         super.addChild(newChild);
45     }
46
47     private boolean isPartOfKey(final LeafSchemaNode potentialKey) {
48         for (QName qname : ((ListSchemaNode) getSchema()).getKeyDefinition()) {
49             if (qname.equals(potentialKey.getQName())) {
50                 return true;
51             }
52         }
53         return false;
54     }
55
56     @Override
57     public void write(final NormalizedNodeStreamWriter writer) throws IOException {
58         final Collection<QName> keyDef = ((ListSchemaNode) getSchema()).getKeyDefinition();
59         if (keyDef.isEmpty()) {
60             writer.nextDataSchemaNode(getSchema());
61             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
62             super.write(writer);
63             writer.endNode();
64             return;
65         }
66
67         Preconditions.checkState(keyDef.size() == qnameToKeys.size(), "Input is missing some of the keys of %s",
68                 getSchema().getQName());
69
70         // Need to restore schema order...
71         final Map<QName, Object> predicates = new LinkedHashMap<>();
72         for (QName qname : keyDef) {
73             predicates.put(qname, qnameToKeys.get(qname).getValue());
74         }
75
76         writer.nextDataSchemaNode(getSchema());
77         writer.startMapEntryNode(
78             new NodeIdentifierWithPredicates(getSchema().getQName(), predicates),
79             childSizeHint());
80         super.write(writer);
81         writer.endNode();
82     }
83 }