2 * Copyright (c) 2016 Brocade Communications 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.codec.xml;
10 import java.io.IOException;
11 import java.util.ArrayDeque;
12 import java.util.Collections;
13 import java.util.Deque;
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;
24 final class SchemalessXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<Object> {
25 private enum ContainerType {
35 private final Deque<ContainerType> containerTypeStack = new ArrayDeque<>();
37 private SchemalessXMLStreamNormalizedNodeStreamWriter(final XMLStreamWriter writer) {
41 static NormalizedNodeStreamWriter newInstance(final XMLStreamWriter writer) {
42 return new SchemalessXMLStreamNormalizedNodeStreamWriter(writer);
46 public void leafNode(final NodeIdentifier name, final Object value, final Map<QName, String> attributes)
48 writeElement(name.getNodeType(), value, attributes, null);
52 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
53 writeElement(name.getNodeType(), value, Collections.emptyMap(), null);
57 public void leafSetEntryNode(final QName name, final Object value, final Map<QName, String> attributes)
59 writeElement(name, value, attributes, null);
63 public void leafSetEntryNode(final QName name, final Object value) throws IOException {
64 writeElement(name, value, Collections.emptyMap(), null);
68 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
69 containerTypeStack.push(ContainerType.LEAF_SET);
73 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
74 containerTypeStack.push(ContainerType.LEAF_SET);
78 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
79 containerTypeStack.push(ContainerType.CONTAINER);
80 startElement(name.getNodeType());
84 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
85 containerTypeStack.push(ContainerType.CHOICE);
89 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
90 containerTypeStack.push(ContainerType.AUGMENTATION);
94 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
95 anyxmlNode(name.getNodeType(), value);
99 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
100 containerTypeStack.push(ContainerType.ANY_XML);
101 startElement(name.getNodeType());
105 protected void writeValue(final XMLStreamWriter xmlWriter, final QName qname, @Nonnull final Object value,
106 final Object context) throws XMLStreamException {
107 xmlWriter.writeCharacters(value.toString());
111 protected void startList(final NodeIdentifier name) {
112 containerTypeStack.push(ContainerType.LIST);
116 protected void startListItem(final PathArgument name) throws IOException {
117 containerTypeStack.push(ContainerType.LIST_ITEM);
118 startElement(name.getNodeType());
122 protected void endNode(final XMLStreamWriter xmlWriter) throws IOException, XMLStreamException {
123 ContainerType type = containerTypeStack.pop();
128 xmlWriter.writeEndElement();