2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.api.schema.stream;
10 import static org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.UNKNOWN_SIZE;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Preconditions;
15 import java.io.Closeable;
16 import java.io.Flushable;
17 import java.io.IOException;
19 import javax.xml.stream.XMLStreamReader;
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;
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.
42 public final class NormalizedNodeWriter implements Closeable, Flushable {
43 private final NormalizedNodeStreamWriter writer;
45 private NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
46 this.writer = Preconditions.checkNotNull(writer);
50 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
52 * @param writer Backend writer
53 * @return A new instance.
55 public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
56 return new NormalizedNodeWriter(writer);
60 * Iterate over the provided {@link NormalizedNode} and emit write
61 * events to the encapsulated {@link NormalizedNodeStreamWriter}.
65 * @throws IOException when thrown from the backing writer.
67 public NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
68 if (wasProcessedAsCompositeNode(node)) {
72 if (wasProcessAsSimpleNode(node)) {
76 throw new IllegalStateException("It wasn't possible to serialize node " + node);
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());
84 } else if (node instanceof LeafNode) {
85 final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
86 writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
88 } else if (node instanceof AnyXmlNode) {
89 final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
90 writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
98 private boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
99 for (NormalizedNode<?, ?> child : children) {
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());
113 if (node instanceof MapEntryNode) {
114 final MapEntryNode n = (MapEntryNode) node;
115 writer.startMapEntryNode(n.getIdentifier(), UNKNOWN_SIZE);
117 // FIXME: BUG-1668: we need to emit keyed items first and then suppress
118 // them from iteration.
120 return writeChildren(n.getValue());
122 if (node instanceof UnkeyedListEntryNode) {
123 final UnkeyedListEntryNode n = (UnkeyedListEntryNode) node;
124 writer.startUnkeyedListItem(n.getIdentifier(), UNKNOWN_SIZE);
125 return writeChildren(n.getValue());
127 if (node instanceof ChoiceNode) {
128 final ChoiceNode n = (ChoiceNode) node;
129 writer.startChoiceNode(n.getIdentifier(), UNKNOWN_SIZE);
130 return writeChildren(n.getValue());
132 if (node instanceof AugmentationNode) {
133 final AugmentationNode n = (AugmentationNode) node;
134 writer.startAugmentationNode(n.getIdentifier());
135 return writeChildren(n.getValue());
137 if (node instanceof UnkeyedListNode) {
138 final UnkeyedListNode n = (UnkeyedListNode) node;
139 writer.startUnkeyedList(n.getIdentifier(), UNKNOWN_SIZE);
140 return writeChildren(n.getValue());
142 if (node instanceof OrderedMapNode) {
143 final OrderedMapNode n = (OrderedMapNode) node;
144 writer.startOrderedMapNode(n.getIdentifier(), UNKNOWN_SIZE);
145 return writeChildren(n.getValue());
147 if (node instanceof MapNode) {
148 final MapNode n = (MapNode) node;
149 writer.startMapNode(n.getIdentifier(), UNKNOWN_SIZE);
150 return writeChildren(n.getValue());
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());
163 public void flush() throws IOException {
168 public void close() throws IOException {