Fix possible null pointer in NormalizedNode XML stream writer
[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         writer.writeStartElement(DEFAULT_NS_PREFIX, qname.getLocalName(), ns);
76         if(writer.getNamespaceContext() != null) {
77             String parentNs = writer.getNamespaceContext().getNamespaceURI(DEFAULT_NS_PREFIX);
78             if (!ns.equals(parentNs)) {
79                 writer.writeDefaultNamespace(ns);
80             }
81         }
82     }
83
84     private void writeElement(final QName qname, final TypeDefinition<?> type, final Object value) throws IOException {
85         try {
86             writeStartElement(qname);
87             if (value != null) {
88                 streamUtils.writeValue(writer, type, value);
89             }
90             writer.writeEndElement();
91         } catch (XMLStreamException e) {
92             throw new IOException("Failed to emit element", e);
93         }
94     }
95
96     private void writeElement(final QName qname, final SchemaNode schemaNode, final Object value) throws IOException {
97         try {
98             writeStartElement(qname);
99             if (value != null) {
100                 streamUtils.writeValue(writer, schemaNode, value);
101             }
102             writer.writeEndElement();
103         } catch (XMLStreamException e) {
104             throw new IOException("Failed to emit element", e);
105         }
106     }
107
108     private void startElement(final QName qname) throws IOException {
109         try {
110             writeStartElement(qname);
111         } catch (XMLStreamException e) {
112             throw new IOException("Failed to start element", e);
113         }
114     }
115
116     private void startList(final NodeIdentifier name) {
117         tracker.startList(name);
118     }
119
120     private void startListItem(final PathArgument name) throws IOException {
121         tracker.startListItem(name);
122         startElement(name.getNodeType());
123     }
124
125     @Override
126     public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
127         final LeafSchemaNode schema = tracker.leafNode(name);
128         writeElement(schema.getQName(), schema, value);
129     }
130
131     @Override
132     public void startLeafSet(final NodeIdentifier name, final int childSizeHint) {
133         tracker.startLeafSet(name);
134     }
135
136     @Override
137     public void leafSetEntryNode(final Object value) throws IOException {
138         final LeafListSchemaNode schema = tracker.leafSetEntryNode();
139         writeElement(schema.getQName(), schema, value);
140     }
141
142     @Override
143     public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
144         final SchemaNode schema = tracker.startContainerNode(name);
145         startElement(schema.getQName());
146     }
147
148     @Override
149     public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) {
150         startList(name);
151     }
152
153     @Override
154     public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
155         startListItem(name);
156     }
157
158     @Override
159     public void startMapNode(final NodeIdentifier name, final int childSizeHint) {
160         startList(name);
161     }
162
163     @Override
164     public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException {
165         startListItem(identifier);
166     }
167
168     @Override
169     public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) {
170         startList(name);
171     }
172
173     @Override
174     public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
175         tracker.startChoiceNode(name);
176     }
177
178     @Override
179     public void startAugmentationNode(final AugmentationIdentifier identifier) {
180         tracker.startAugmentationNode(identifier);
181     }
182
183     @Override
184     public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
185         final AnyXmlSchemaNode schema = tracker.anyxmlNode(name);
186         final QName qname = schema.getQName();
187         try {
188             writeStartElement(qname);
189             if (value != null) {
190                 streamUtils.writeValue(writer, (Node<?>)value, schema);
191             }
192             writer.writeEndElement();
193         } catch (XMLStreamException e) {
194             throw new IOException("Failed to emit element", e);
195         }
196     }
197
198     @Override
199     public void endNode() throws IOException {
200         final Object schema = tracker.endNode();
201
202         try {
203             if (schema instanceof ListSchemaNode) {
204                 // For lists, we only emit end element on the inner frame
205                 final Object parent = tracker.getParent();
206                 if (parent == schema) {
207                     writer.writeEndElement();
208                 }
209             } else if (schema instanceof ContainerSchemaNode) {
210                 // Emit container end element
211                 writer.writeEndElement();
212             }
213         } catch (XMLStreamException e) {
214             throw new IOException("Failed to end element", e);
215         }
216     }
217
218     @Override
219     public void close() throws IOException {
220         try {
221             writer.close();
222         } catch (XMLStreamException e) {
223             throw new IOException("Failed to close writer", e);
224         }
225     }
226
227     @Override
228     public void flush() throws IOException {
229         try {
230             writer.flush();
231         } catch (XMLStreamException e) {
232             throw new IOException("Failed to flush writer", e);
233         }
234     }
235 }