Merge "Replaced harcoded nexus proxy URLs with variable."
[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     private boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
98         boolean hasDataContainerChild = false;
99         if (node instanceof ContainerNode) {
100             writer.startContainerNode(((ContainerNode) node).getIdentifier(), UNKNOWN_SIZE);
101             hasDataContainerChild = true;
102         } else if (node instanceof MapEntryNode) {
103             writer.startMapEntryNode(((MapEntryNode) node).getIdentifier(), UNKNOWN_SIZE);
104             hasDataContainerChild = true;
105         } else if (node instanceof UnkeyedListEntryNode) {
106             writer.startUnkeyedListItem(((UnkeyedListEntryNode) node).getIdentifier(), UNKNOWN_SIZE);
107             hasDataContainerChild = true;
108         } else if (node instanceof ChoiceNode) {
109             writer.startChoiceNode(((ChoiceNode) node).getIdentifier(), UNKNOWN_SIZE);
110             hasDataContainerChild = true;
111         } else if (node instanceof AugmentationNode) {
112             writer.startAugmentationNode(((AugmentationNode) node).getIdentifier());
113             hasDataContainerChild = true;
114         } else if (node instanceof UnkeyedListNode) {
115             writer.startUnkeyedList(((UnkeyedListNode) node).getIdentifier(), UNKNOWN_SIZE);
116             hasDataContainerChild = true;
117         } else if (node instanceof OrderedMapNode) {
118             writer.startOrderedMapNode(((OrderedMapNode) node).getIdentifier(), UNKNOWN_SIZE);
119             hasDataContainerChild = true;
120         } else if (node instanceof MapNode) {
121             writer.startMapNode(((MapNode) node).getIdentifier(), UNKNOWN_SIZE);
122             hasDataContainerChild = true;
123           //covers also OrderedLeafSetNode for which doesn't exist start* method
124         } else if (node instanceof LeafSetNode) {
125             writer.startLeafSet(((LeafSetNode<?>) node).getIdentifier(), UNKNOWN_SIZE);
126             hasDataContainerChild = true;
127         }
128
129         if (hasDataContainerChild) {
130             for (NormalizedNode<?, ?> childNode : ((NormalizedNode<?, Iterable<NormalizedNode<?, ?>>>) node).getValue()) {
131                 write(childNode);
132             }
133
134             writer.endNode();
135             return true;
136         }
137         return false;
138
139     }
140
141     @Override
142     public void flush() throws IOException {
143         writer.flush();
144     }
145
146     @Override
147     public void close() throws IOException {
148         writer.close();
149     }
150 }