Bug 8745: Add support for parsing attributes to 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.NormalizedNodeStreamAttributeWriter;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23
24 /**
25  * Utility class used for tracking parser state as needed by a StAX-like parser.
26  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
27  *
28  * <p>
29  * Represents a YANG list entry node.
30  */
31 public class ListEntryNodeDataWithSchema extends CompositeNodeDataWithSchema {
32
33     private final Map<QName, SimpleNodeDataWithSchema> qnameToKeys = new HashMap<>();
34
35     public ListEntryNodeDataWithSchema(final DataSchemaNode schema) {
36         super(schema);
37     }
38
39     @Override
40     public void addChild(final AbstractNodeDataWithSchema newChild) {
41         final DataSchemaNode childSchema = newChild.getSchema();
42         if (childSchema instanceof LeafSchemaNode && isPartOfKey((LeafSchemaNode) childSchema)) {
43             qnameToKeys.put(childSchema.getQName(), (SimpleNodeDataWithSchema)newChild);
44         }
45         super.addChild(newChild);
46     }
47
48     private boolean isPartOfKey(final LeafSchemaNode potentialKey) {
49         for (QName qname : ((ListSchemaNode) getSchema()).getKeyDefinition()) {
50             if (qname.equals(potentialKey.getQName())) {
51                 return true;
52             }
53         }
54         return false;
55     }
56
57     @Override
58     public void write(final NormalizedNodeStreamWriter writer) throws IOException {
59         final Collection<QName> keyDef = ((ListSchemaNode) getSchema()).getKeyDefinition();
60         if (keyDef.isEmpty()) {
61             writer.nextDataSchemaNode(getSchema());
62             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
63             super.write(writer);
64             writer.endNode();
65             return;
66         }
67
68         Preconditions.checkState(keyDef.size() == qnameToKeys.size(), "Input is missing some of the keys of %s",
69                 getSchema().getQName());
70
71         // Need to restore schema order...
72         final Map<QName, Object> predicates = new LinkedHashMap<>();
73         for (QName qname : keyDef) {
74             predicates.put(qname, qnameToKeys.get(qname).getValue());
75         }
76
77         writer.nextDataSchemaNode(getSchema());
78
79         if (writer instanceof NormalizedNodeStreamAttributeWriter && getAttributes() != null) {
80             ((NormalizedNodeStreamAttributeWriter) writer).startMapEntryNode(
81                     new NodeIdentifierWithPredicates(getSchema().getQName(), predicates), childSizeHint(),
82                     getAttributes());
83         } else {
84             writer.startMapEntryNode(new NodeIdentifierWithPredicates(getSchema().getQName(), predicates),
85                     childSizeHint());
86         }
87
88         super.write(writer);
89         writer.endNode();
90     }
91 }