Rework NormalizedNodeStreamWriter
[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 static com.google.common.base.Verify.verify;
11
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import org.opendaylight.yangtools.util.ImmutableMapTemplate;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamAttributeWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24
25 /**
26  * Utility class used for tracking parser state as needed by a StAX-like parser.
27  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
28  *
29  * <p>
30  * Represents a YANG list entry node.
31  */
32 public abstract class ListEntryNodeDataWithSchema extends CompositeNodeDataWithSchema<ListSchemaNode> {
33     private static final class Keyed extends ListEntryNodeDataWithSchema {
34         private final Map<QName, SimpleNodeDataWithSchema<?>> keyValues = new HashMap<>();
35         // This template results in Maps in schema definition order
36         private final ImmutableMapTemplate<QName> predicateTemplate;
37
38         Keyed(final ListSchemaNode schema, final List<QName> keyDef) {
39             super(schema);
40             predicateTemplate = ImmutableMapTemplate.ordered(keyDef);
41         }
42
43         @Override
44         public void addChild(final AbstractNodeDataWithSchema<?> newChild) {
45             final DataSchemaNode childSchema = newChild.getSchema();
46             if (childSchema instanceof LeafSchemaNode) {
47                 final QName childName = childSchema.getQName();
48                 if (predicateTemplate.keySet().contains(childName)) {
49                     verify(newChild instanceof SimpleNodeDataWithSchema);
50                     keyValues.put(childName, (SimpleNodeDataWithSchema<?>)newChild);
51                 }
52             }
53             super.addChild(newChild);
54         }
55
56         @Override
57         public void write(final NormalizedNodeStreamWriter writer) throws IOException {
58             writer.nextDataSchemaNode(getSchema());
59             final NodeIdentifierWithPredicates identifier = new NodeIdentifierWithPredicates(getSchema().getQName(),
60                 predicateTemplate.instantiateTransformed(keyValues, (key, node) -> node.getValue()));
61
62             writer.startMapEntryNode(identifier, childSizeHint());
63             if (writer instanceof NormalizedNodeStreamAttributeWriter) {
64                 final Map<QName, String> attrs = getAttributes();
65                 if (attrs != null) {
66                     ((NormalizedNodeStreamAttributeWriter) writer).attributes(attrs);
67                 }
68             }
69
70             super.write(writer);
71             writer.endNode();
72         }
73     }
74
75     private static final class Unkeyed extends ListEntryNodeDataWithSchema {
76         Unkeyed(final ListSchemaNode schema) {
77             super(schema);
78         }
79
80         @Override
81         public void write(final NormalizedNodeStreamWriter writer) throws IOException {
82             writer.nextDataSchemaNode(getSchema());
83             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
84             super.write(writer);
85             writer.endNode();
86         }
87     }
88
89     ListEntryNodeDataWithSchema(final ListSchemaNode schema) {
90         super(schema);
91     }
92
93     public static ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
94         final List<QName> keyDef = schema.getKeyDefinition();
95         return keyDef.isEmpty() ? new Unkeyed(schema) :  new Keyed(schema, keyDef);
96     }
97 }