2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
10 import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX;
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;
35 * A {@link NormalizedNodeStreamWriter} which translates the events into an
36 * {@link XMLStreamWriter}, resulting in a RFC 6020 XML encoding.
38 public final class XMLStreamNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
40 private final XMLStreamWriter writer;
41 private final SchemaTracker tracker;
42 private final XmlStreamUtils streamUtils;
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);
51 * Create a new writer with the specified context as its root.
53 * @param writer Output {@link XMLStreamWriter}
54 * @param context Associated {@link SchemaContext}.
55 * @return A new {@link NormalizedNodeStreamWriter}
57 public static NormalizedNodeStreamWriter create(final XMLStreamWriter writer, final SchemaContext context) {
58 return create( writer, context, SchemaPath.ROOT);
62 * Create a new writer with the specified context and rooted in the specified schema path
64 * @param writer Output {@link XMLStreamWriter}
65 * @param context Associated {@link SchemaContext}.
67 * @return A new {@link NormalizedNodeStreamWriter}
69 public static NormalizedNodeStreamWriter create(final XMLStreamWriter writer, final SchemaContext context, final SchemaPath path) {
70 return new XMLStreamNormalizedNodeStreamWriter(writer, context, path);
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);
82 private void writeElement(final QName qname, final TypeDefinition<?> type, final Object value) throws IOException {
84 writeStartElement(qname);
86 streamUtils.writeValue(writer, type, value);
88 writer.writeEndElement();
89 } catch (XMLStreamException e) {
90 throw new IOException("Failed to emit element", e);
94 private void writeElement(final QName qname, final SchemaNode schemaNode, final Object value) throws IOException {
96 writeStartElement(qname);
98 streamUtils.writeValue(writer, schemaNode, value);
100 writer.writeEndElement();
101 } catch (XMLStreamException e) {
102 throw new IOException("Failed to emit element", e);
106 private void startElement(final QName qname) throws IOException {
108 writeStartElement(qname);
109 } catch (XMLStreamException e) {
110 throw new IOException("Failed to start element", e);
114 private void startList(final NodeIdentifier name) {
115 tracker.startList(name);
118 private void startListItem(final PathArgument name) throws IOException {
119 tracker.startListItem(name);
120 startElement(name.getNodeType());
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);
130 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) {
131 tracker.startLeafSet(name);
135 public void leafSetEntryNode(final Object value) throws IOException {
136 final LeafListSchemaNode schema = tracker.leafSetEntryNode();
137 writeElement(schema.getQName(), schema, value);
141 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
142 final SchemaNode schema = tracker.startContainerNode(name);
143 startElement(schema.getQName());
147 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) {
152 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
157 public void startMapNode(final NodeIdentifier name, final int childSizeHint) {
162 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException {
163 startListItem(identifier);
167 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) {
172 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
173 tracker.startChoiceNode(name);
177 public void startAugmentationNode(final AugmentationIdentifier identifier) {
178 tracker.startAugmentationNode(identifier);
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();
186 writeStartElement(qname);
188 streamUtils.writeValue(writer, (Node<?>)value, schema);
190 writer.writeEndElement();
191 } catch (XMLStreamException e) {
192 throw new IOException("Failed to emit element", e);
197 public void endNode() throws IOException {
198 final Object schema = tracker.endNode();
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();
207 } else if (schema instanceof ContainerSchemaNode) {
208 // Emit container end element
209 writer.writeEndElement();
211 } catch (XMLStreamException e) {
212 throw new IOException("Failed to end element", e);
217 public void close() throws IOException {
220 } catch (XMLStreamException e) {
221 throw new IOException("Failed to close writer", e);
226 public void flush() throws IOException {
229 } catch (XMLStreamException e) {
230 throw new IOException("Failed to flush writer", e);