Merge "Improve InstanceIdentifier.keyOf()"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XMLStreamNormalizedNodeStreamWriter.java
1 /*
2  * Copyright (c) 2014 Cisco 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 static javax.xml.XMLConstants.DEFAULT_NS_PREFIX;
11
12 import com.google.common.base.Preconditions;
13 import java.io.IOException;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.stream.XMLStreamWriter;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.Node;
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.NodeIdentifierWithPredicates;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker;
24 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
33
34 /**
35  * A {@link NormalizedNodeStreamWriter} which translates the events into an
36  * {@link XMLStreamWriter}, resulting in a RFC 6020 XML encoding.
37  */
38 public final class XMLStreamNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
39
40     private final XMLStreamWriter writer;
41     private final SchemaTracker tracker;
42     private final XmlStreamUtils streamUtils;
43
44     private XMLStreamNormalizedNodeStreamWriter(final XMLStreamWriter writer, final SchemaContext context, final SchemaPath path) {
45         this.writer = Preconditions.checkNotNull(writer);
46         this.tracker = SchemaTracker.create(context, path);
47         this.streamUtils = XmlStreamUtils.create(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, context);
48     }
49
50     /**
51      * Create a new writer with the specified context as its root.
52      *
53      * @param writer Output {@link XMLStreamWriter}
54      * @param context Associated {@link SchemaContext}.
55      * @return A new {@link NormalizedNodeStreamWriter}
56      */
57     public static NormalizedNodeStreamWriter create(final XMLStreamWriter writer, final SchemaContext context) {
58         return create( writer, context, SchemaPath.ROOT);
59     }
60
61     /**
62      * Create a new writer with the specified context and rooted in the specified schema path
63      *
64      * @param writer Output {@link XMLStreamWriter}
65      * @param context Associated {@link SchemaContext}.
66      *
67      * @return A new {@link NormalizedNodeStreamWriter}
68      */
69     public static NormalizedNodeStreamWriter create(final XMLStreamWriter writer, final SchemaContext context, final SchemaPath path) {
70         return new XMLStreamNormalizedNodeStreamWriter(writer, context, path);
71     }
72
73     private void writeStartElement( QName qname) throws XMLStreamException {
74         String ns = qname.getNamespace().toString();
75         String parentNs = writer.getNamespaceContext().getNamespaceURI(DEFAULT_NS_PREFIX);
76         writer.writeStartElement(DEFAULT_NS_PREFIX, qname.getLocalName(), ns);
77         if (!ns.equals(parentNs)) {
78             writer.writeDefaultNamespace(ns);
79         }
80     }
81
82     private void writeElement(final QName qname, final TypeDefinition<?> type, final Object value) throws IOException {
83         try {
84             writeStartElement(qname);
85             if (value != null) {
86                 streamUtils.writeValue(writer, type, value);
87             }
88             writer.writeEndElement();
89         } catch (XMLStreamException e) {
90             throw new IOException("Failed to emit element", e);
91         }
92     }
93
94     private void writeElement(final QName qname, final SchemaNode schemaNode, final Object value) throws IOException {
95         try {
96             writeStartElement(qname);
97             if (value != null) {
98                 streamUtils.writeValue(writer, schemaNode, value);
99             }
100             writer.writeEndElement();
101         } catch (XMLStreamException e) {
102             throw new IOException("Failed to emit element", e);
103         }
104     }
105
106     private void startElement(final QName qname) throws IOException {
107         try {
108             writeStartElement(qname);
109         } catch (XMLStreamException e) {
110             throw new IOException("Failed to start element", e);
111         }
112     }
113
114     private void startList(final NodeIdentifier name) {
115         tracker.startList(name);
116     }
117
118     private void startListItem(final PathArgument name) throws IOException {
119         tracker.startListItem(name);
120         startElement(name.getNodeType());
121     }
122
123     @Override
124     public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
125         final LeafSchemaNode schema = tracker.leafNode(name);
126         writeElement(schema.getQName(), schema, value);
127     }
128
129     @Override
130     public void startLeafSet(final NodeIdentifier name, final int childSizeHint) {
131         tracker.startLeafSet(name);
132     }
133
134     @Override
135     public void leafSetEntryNode(final Object value) throws IOException {
136         final LeafListSchemaNode schema = tracker.leafSetEntryNode();
137         writeElement(schema.getQName(), schema, value);
138     }
139
140     @Override
141     public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
142         final SchemaNode schema = tracker.startContainerNode(name);
143         startElement(schema.getQName());
144     }
145
146     @Override
147     public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) {
148         startList(name);
149     }
150
151     @Override
152     public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
153         startListItem(name);
154     }
155
156     @Override
157     public void startMapNode(final NodeIdentifier name, final int childSizeHint) {
158         startList(name);
159     }
160
161     @Override
162     public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException {
163         startListItem(identifier);
164     }
165
166     @Override
167     public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) {
168         startList(name);
169     }
170
171     @Override
172     public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
173         tracker.startChoiceNode(name);
174     }
175
176     @Override
177     public void startAugmentationNode(final AugmentationIdentifier identifier) {
178         tracker.startAugmentationNode(identifier);
179     }
180
181     @Override
182     public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
183         final AnyXmlSchemaNode schema = tracker.anyxmlNode(name);
184         final QName qname = schema.getQName();
185         try {
186             writeStartElement(qname);
187             if (value != null) {
188                 streamUtils.writeValue(writer, (Node<?>)value, schema);
189             }
190             writer.writeEndElement();
191         } catch (XMLStreamException e) {
192             throw new IOException("Failed to emit element", e);
193         }
194     }
195
196     @Override
197     public void endNode() throws IOException {
198         final Object schema = tracker.endNode();
199
200         try {
201             if (schema instanceof ListSchemaNode) {
202                 // For lists, we only emit end element on the inner frame
203                 final Object parent = tracker.getParent();
204                 if (parent == schema) {
205                     writer.writeEndElement();
206                 }
207             } else if (schema instanceof ContainerSchemaNode) {
208                 // Emit container end element
209                 writer.writeEndElement();
210             }
211         } catch (XMLStreamException e) {
212             throw new IOException("Failed to end element", e);
213         }
214     }
215
216     @Override
217     public void close() throws IOException {
218         try {
219             writer.close();
220         } catch (XMLStreamException e) {
221             throw new IOException("Failed to close writer", e);
222         }
223     }
224
225     @Override
226     public void flush() throws IOException {
227         try {
228             writer.flush();
229         } catch (XMLStreamException e) {
230             throw new IOException("Failed to flush writer", e);
231         }
232     }
233 }