BUG-1668: split the writeout paths
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / stream / NormalizedNodeWriter.java
1 /*
2  * Copyright (c) 2014 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.api.schema.stream;
9
10 import static org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.UNKNOWN_SIZE;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Preconditions;
14
15 import java.io.Closeable;
16 import java.io.Flushable;
17 import java.io.IOException;
18
19 import javax.xml.stream.XMLStreamReader;
20
21 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
34
35 /**
36  * This is an experimental iterator over a {@link NormalizedNode}. This is essentially
37  * the opposite of a {@link XMLStreamReader} -- unlike instantiating an iterator over
38  * the backing data, this encapsulates a {@link NormalizedNodeStreamWriter} and allows
39  * us to write multiple nodes.
40  */
41 @Beta
42 public final class NormalizedNodeWriter implements Closeable, Flushable {
43     private final NormalizedNodeStreamWriter writer;
44
45     private NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
46         this.writer = Preconditions.checkNotNull(writer);
47     }
48
49     /**
50      * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
51      *
52      * @param writer Backend writer
53      * @return A new instance.
54      */
55     public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
56         return new NormalizedNodeWriter(writer);
57     }
58
59     /**
60      * Iterate over the provided {@link NormalizedNode} and emit write
61      * events to the encapsulated {@link NormalizedNodeStreamWriter}.
62      *
63      * @param node Node
64      * @return
65      * @throws IOException when thrown from the backing writer.
66      */
67     public NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
68         if (wasProcessedAsCompositeNode(node)) {
69             return this;
70         }
71
72         if (wasProcessAsSimpleNode(node)) {
73             return this;
74         }
75
76         throw new IllegalStateException("It wasn't possible to serialize node " + node);
77     }
78
79     private boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
80         if (node instanceof LeafSetEntryNode) {
81             final LeafSetEntryNode<?> nodeAsLeafList = (LeafSetEntryNode<?>)node;
82             writer.leafSetEntryNode(nodeAsLeafList.getValue());
83             return true;
84         } else if (node instanceof LeafNode) {
85             final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
86             writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
87             return true;
88         } else if (node instanceof AnyXmlNode) {
89             final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
90             writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
91             return true;
92         }
93
94         return false;
95     }
96
97
98     private boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
99         for (NormalizedNode<?, ?> child : children) {
100             write(child);
101         }
102
103         writer.endNode();
104         return true;
105     }
106
107     private boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
108         if (node instanceof ContainerNode) {
109             final ContainerNode n = (ContainerNode) node;
110             writer.startContainerNode(n.getIdentifier(), UNKNOWN_SIZE);
111             return writeChildren(n.getValue());
112         }
113         if (node instanceof MapEntryNode) {
114             final MapEntryNode n = (MapEntryNode) node;
115             writer.startMapEntryNode(n.getIdentifier(), UNKNOWN_SIZE);
116
117             // FIXME: BUG-1668: we need to emit keyed items first and then suppress
118             //        them from iteration.
119
120             return writeChildren(n.getValue());
121         }
122         if (node instanceof UnkeyedListEntryNode) {
123             final UnkeyedListEntryNode n = (UnkeyedListEntryNode) node;
124             writer.startUnkeyedListItem(n.getIdentifier(), UNKNOWN_SIZE);
125             return writeChildren(n.getValue());
126         }
127         if (node instanceof ChoiceNode) {
128             final ChoiceNode n = (ChoiceNode) node;
129             writer.startChoiceNode(n.getIdentifier(), UNKNOWN_SIZE);
130             return writeChildren(n.getValue());
131         }
132         if (node instanceof AugmentationNode) {
133             final AugmentationNode n = (AugmentationNode) node;
134             writer.startAugmentationNode(n.getIdentifier());
135             return writeChildren(n.getValue());
136         }
137         if (node instanceof UnkeyedListNode) {
138             final UnkeyedListNode n = (UnkeyedListNode) node;
139             writer.startUnkeyedList(n.getIdentifier(), UNKNOWN_SIZE);
140             return writeChildren(n.getValue());
141         }
142         if (node instanceof OrderedMapNode) {
143             final OrderedMapNode n = (OrderedMapNode) node;
144             writer.startOrderedMapNode(n.getIdentifier(), UNKNOWN_SIZE);
145             return writeChildren(n.getValue());
146         }
147         if (node instanceof MapNode) {
148             final MapNode n = (MapNode) node;
149             writer.startMapNode(n.getIdentifier(), UNKNOWN_SIZE);
150             return writeChildren(n.getValue());
151         }
152         if (node instanceof LeafSetNode) {
153             //covers also OrderedLeafSetNode for which doesn't exist start* method
154             final LeafSetNode<?> n = (LeafSetNode<?>) node;
155             writer.startLeafSet(n.getIdentifier(), UNKNOWN_SIZE);
156             return writeChildren(n.getValue());
157         }
158
159         return false;
160     }
161
162     @Override
163     public void flush() throws IOException {
164         writer.flush();
165     }
166
167     @Override
168     public void close() throws IOException {
169         writer.close();
170     }
171 }