Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / 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.impl.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 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(XMLStreamWriter writer) {
38         super(writer);
39     }
40
41     static NormalizedNodeStreamWriter newInstance(XMLStreamWriter writer) {
42         return new SchemalessXMLStreamNormalizedNodeStreamWriter(writer);
43     }
44
45     @Override
46     public void leafNode(NodeIdentifier name, Object value, Map<QName, String> attributes) throws IOException {
47         writeElement(name.getNodeType(), value, attributes, null);
48     }
49
50     @Override
51     public void leafSetEntryNode(QName name, Object value, Map<QName, String> attributes) throws IOException {
52         writeElement(name, value, attributes, null);
53     }
54
55     @Override
56     public void leafNode(NodeIdentifier name, Object value) throws IOException {
57         writeElement(name.getNodeType(), value, Collections.emptyMap(), null);
58     }
59
60     @Override
61     public void leafSetEntryNode(QName name, Object value) throws IOException {
62         writeElement(name, value, Collections.emptyMap(), null);
63     }
64
65     @Override
66     public void startLeafSet(NodeIdentifier name, int childSizeHint) throws IOException {
67         containerTypeStack.push(ContainerType.LEAF_SET);
68     }
69
70     @Override
71     public void startOrderedLeafSet(NodeIdentifier name, int childSizeHint)
72             throws IOException, IllegalArgumentException {
73         containerTypeStack.push(ContainerType.LEAF_SET);
74     }
75
76     @Override
77     public void startContainerNode(NodeIdentifier name, int childSizeHint) throws IOException {
78         containerTypeStack.push(ContainerType.CONTAINER);
79         startElement(name.getNodeType());
80     }
81
82     @Override
83     public void startChoiceNode(NodeIdentifier name, int childSizeHint) throws IOException {
84         containerTypeStack.push(ContainerType.CHOICE);
85     }
86
87     @Override
88     public void startAugmentationNode(AugmentationIdentifier identifier) throws IOException {
89         containerTypeStack.push(ContainerType.AUGMENTATION);
90     }
91
92     @Override
93     public void anyxmlNode(NodeIdentifier name, Object value) throws IOException {
94         anyxmlNode(name.getNodeType(), value);
95     }
96
97     @Override
98     public void startYangModeledAnyXmlNode(NodeIdentifier name, int childSizeHint) throws IOException {
99         containerTypeStack.push(ContainerType.ANY_XML);
100         startElement(name.getNodeType());
101     }
102
103     @Override
104     protected void writeValue(XMLStreamWriter xmlWriter, QName qname, @Nonnull Object value, Object context)
105             throws XMLStreamException {
106         xmlWriter.writeCharacters(value.toString());
107     }
108
109     @Override
110     protected void startList(NodeIdentifier name) {
111         containerTypeStack.push(ContainerType.LIST);
112     }
113
114     @Override
115     protected void startListItem(PathArgument name) throws IOException {
116         containerTypeStack.push(ContainerType.LIST_ITEM);
117         startElement(name.getNodeType());
118     }
119
120     @Override
121     protected void endNode(XMLStreamWriter xmlWriter) throws IOException, XMLStreamException {
122         ContainerType type = containerTypeStack.pop();
123         switch(type) {
124         case CONTAINER:
125         case LIST_ITEM:
126         case ANY_XML:
127             xmlWriter.writeEndElement();
128             break;
129         default:
130             break;
131         }
132     }
133 }