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 org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
34 * This is an experimental
37 public final class NormalizedNodeWriter implements Closeable, Flushable {
38 private final NormalizedNodeStreamWriter writer;
40 private NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
41 this.writer = Preconditions.checkNotNull(writer);
44 public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
45 return new NormalizedNodeWriter(writer);
48 public NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
49 if (wasProcessedAsCompositeNode(node)) {
53 if (wasProcessAsSimpleNode(node)) {
57 throw new IllegalStateException("It wasn't possible to serialize node " + node);
60 private boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
61 if (node instanceof LeafSetEntryNode) {
62 final LeafSetEntryNode<?> nodeAsLeafList = (LeafSetEntryNode<?>)node;
63 writer.leafSetEntryNode(nodeAsLeafList.getValue());
65 } else if (node instanceof LeafNode) {
66 final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
67 writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
69 } else if (node instanceof AnyXmlNode) {
70 final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
71 writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
78 private boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
79 boolean hasDataContainerChild = false;
80 if (node instanceof ContainerNode) {
81 writer.startContainerNode(((ContainerNode) node).getIdentifier(), UNKNOWN_SIZE);
82 hasDataContainerChild = true;
83 } else if (node instanceof MapEntryNode) {
84 writer.startMapEntryNode(((MapEntryNode) node).getIdentifier(), UNKNOWN_SIZE);
85 hasDataContainerChild = true;
86 } else if (node instanceof UnkeyedListEntryNode) {
87 writer.startUnkeyedListItem(((UnkeyedListEntryNode) node).getIdentifier(), UNKNOWN_SIZE);
88 hasDataContainerChild = true;
89 } else if (node instanceof ChoiceNode) {
90 writer.startChoiceNode(((ChoiceNode) node).getIdentifier(), UNKNOWN_SIZE);
91 hasDataContainerChild = true;
92 } else if (node instanceof AugmentationNode) {
93 writer.startAugmentationNode(((AugmentationNode) node).getIdentifier());
94 hasDataContainerChild = true;
95 } else if (node instanceof UnkeyedListNode) {
96 writer.startUnkeyedList(((UnkeyedListNode) node).getIdentifier(), UNKNOWN_SIZE);
97 hasDataContainerChild = true;
98 } else if (node instanceof OrderedMapNode) {
99 writer.startOrderedMapNode(((OrderedMapNode) node).getIdentifier(), UNKNOWN_SIZE);
100 hasDataContainerChild = true;
101 } else if (node instanceof MapNode) {
102 writer.startMapNode(((MapNode) node).getIdentifier(), UNKNOWN_SIZE);
103 hasDataContainerChild = true;
104 //covers also OrderedLeafSetNode for which doesn't exist start* method
105 } else if (node instanceof LeafSetNode) {
106 writer.startLeafSet(((LeafSetNode<?>) node).getIdentifier(), UNKNOWN_SIZE);
107 hasDataContainerChild = true;
110 if (hasDataContainerChild) {
111 for (NormalizedNode<?, ?> childNode : ((NormalizedNode<?, Iterable<NormalizedNode<?, ?>>>) node).getValue()) {
123 public void flush() throws IOException {
128 public void close() throws IOException {