946018319c9f18457e70f2a169bb9b65f77bbabf
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / SchemalessXMLStreamNormalizedNodeStreamWriter.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.codec.xml;
9
10 import java.io.IOException;
11 import java.util.ArrayDeque;
12 import java.util.Collections;
13 import java.util.Deque;
14 import java.util.Map;
15 import javax.annotation.Nonnull;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.stream.XMLStreamWriter;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23
24 final class SchemalessXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<Object> {
25     private enum ContainerType {
26         CONTAINER,
27         LEAF_SET,
28         LIST,
29         LIST_ITEM,
30         ANY_XML,
31         CHOICE,
32         AUGMENTATION
33     }
34
35     private final Deque<ContainerType> containerTypeStack = new ArrayDeque<>();
36
37     private SchemalessXMLStreamNormalizedNodeStreamWriter(final XMLStreamWriter writer) {
38         super(writer);
39     }
40
41     static NormalizedNodeStreamWriter newInstance(final XMLStreamWriter writer) {
42         return new SchemalessXMLStreamNormalizedNodeStreamWriter(writer);
43     }
44
45     @Override
46     public void leafNode(final NodeIdentifier name, final Object value, final Map<QName, String> attributes)
47             throws IOException {
48         writeElement(name.getNodeType(), value, attributes, null);
49     }
50
51     @Override
52     public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
53         writeElement(name.getNodeType(), value, Collections.emptyMap(), null);
54     }
55
56     @Override
57     public void leafSetEntryNode(final QName name, final Object value, final Map<QName, String> attributes)
58             throws IOException {
59         writeElement(name, value, attributes, null);
60     }
61
62     @Override
63     public void leafSetEntryNode(final QName name, final Object value) throws IOException {
64         writeElement(name, value, Collections.emptyMap(), null);
65     }
66
67     @Override
68     public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
69         containerTypeStack.push(ContainerType.LEAF_SET);
70     }
71
72     @Override
73     public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
74         containerTypeStack.push(ContainerType.LEAF_SET);
75     }
76
77     @Override
78     public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
79         containerTypeStack.push(ContainerType.CONTAINER);
80         startElement(name.getNodeType());
81     }
82
83     @Override
84     public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
85         containerTypeStack.push(ContainerType.CHOICE);
86     }
87
88     @Override
89     public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
90         containerTypeStack.push(ContainerType.AUGMENTATION);
91     }
92
93     @Override
94     public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
95         anyxmlNode(name.getNodeType(), value);
96     }
97
98     @Override
99     public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
100         containerTypeStack.push(ContainerType.ANY_XML);
101         startElement(name.getNodeType());
102     }
103
104     @Override
105     protected void writeValue(final XMLStreamWriter xmlWriter, final QName qname, @Nonnull final Object value,
106             final Object context) throws XMLStreamException {
107         xmlWriter.writeCharacters(value.toString());
108     }
109
110     @Override
111     protected void startList(final NodeIdentifier name) {
112         containerTypeStack.push(ContainerType.LIST);
113     }
114
115     @Override
116     protected void startListItem(final PathArgument name) throws IOException {
117         containerTypeStack.push(ContainerType.LIST_ITEM);
118         startElement(name.getNodeType());
119     }
120
121     @Override
122     protected void endNode(final XMLStreamWriter xmlWriter) throws IOException, XMLStreamException {
123         ContainerType type = containerTypeStack.pop();
124         switch (type) {
125             case CONTAINER:
126             case LIST_ITEM:
127             case ANY_XML:
128                 xmlWriter.writeEndElement();
129                 break;
130             default:
131                 break;
132         }
133     }
134 }