Fix odlparent-3-detected checkstyle issues
[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)
74             throws IOException, IllegalArgumentException {
75         containerTypeStack.push(ContainerType.LEAF_SET);
76     }
77
78     @Override
79     public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
80         containerTypeStack.push(ContainerType.CONTAINER);
81         startElement(name.getNodeType());
82     }
83
84     @Override
85     public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
86         containerTypeStack.push(ContainerType.CHOICE);
87     }
88
89     @Override
90     public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
91         containerTypeStack.push(ContainerType.AUGMENTATION);
92     }
93
94     @Override
95     public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
96         anyxmlNode(name.getNodeType(), value);
97     }
98
99     @Override
100     public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
101         containerTypeStack.push(ContainerType.ANY_XML);
102         startElement(name.getNodeType());
103     }
104
105     @Override
106     protected void writeValue(final XMLStreamWriter xmlWriter, final QName qname, @Nonnull final Object value,
107             final Object context)
108             throws XMLStreamException {
109         xmlWriter.writeCharacters(value.toString());
110     }
111
112     @Override
113     protected void startList(final NodeIdentifier name) {
114         containerTypeStack.push(ContainerType.LIST);
115     }
116
117     @Override
118     protected void startListItem(final PathArgument name) throws IOException {
119         containerTypeStack.push(ContainerType.LIST_ITEM);
120         startElement(name.getNodeType());
121     }
122
123     @Override
124     protected void endNode(final XMLStreamWriter xmlWriter) throws IOException, XMLStreamException {
125         ContainerType type = containerTypeStack.pop();
126         switch (type) {
127             case CONTAINER:
128             case LIST_ITEM:
129             case ANY_XML:
130                 xmlWriter.writeEndElement();
131                 break;
132             default:
133                 break;
134         }
135     }
136 }