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