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.impl.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 class SchemalessXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<Object> {
25 private enum ContainerType {
35 private final Deque<ContainerType> containerTypeStack = new ArrayDeque<>();
37 private SchemalessXMLStreamNormalizedNodeStreamWriter(XMLStreamWriter writer) {
41 static NormalizedNodeStreamWriter newInstance(XMLStreamWriter writer) {
42 return new SchemalessXMLStreamNormalizedNodeStreamWriter(writer);
46 public void leafNode(NodeIdentifier name, Object value, Map<QName, String> attributes) throws IOException {
47 writeElement(name.getNodeType(), value, attributes, null);
51 public void leafSetEntryNode(QName name, Object value, Map<QName, String> attributes) throws IOException {
52 writeElement(name, value, attributes, null);
56 public void leafNode(NodeIdentifier name, Object value) throws IOException {
57 writeElement(name.getNodeType(), value, Collections.emptyMap(), null);
61 public void leafSetEntryNode(QName name, Object value) throws IOException {
62 writeElement(name, value, Collections.emptyMap(), null);
66 public void startLeafSet(NodeIdentifier name, int childSizeHint) throws IOException {
67 containerTypeStack.push(ContainerType.LEAF_SET);
71 public void startOrderedLeafSet(NodeIdentifier name, int childSizeHint)
72 throws IOException, IllegalArgumentException {
73 containerTypeStack.push(ContainerType.LEAF_SET);
77 public void startContainerNode(NodeIdentifier name, int childSizeHint) throws IOException {
78 containerTypeStack.push(ContainerType.CONTAINER);
79 startElement(name.getNodeType());
83 public void startChoiceNode(NodeIdentifier name, int childSizeHint) throws IOException {
84 containerTypeStack.push(ContainerType.CHOICE);
88 public void startAugmentationNode(AugmentationIdentifier identifier) throws IOException {
89 containerTypeStack.push(ContainerType.AUGMENTATION);
93 public void anyxmlNode(NodeIdentifier name, Object value) throws IOException {
94 anyxmlNode(name.getNodeType(), value);
98 public void startYangModeledAnyXmlNode(NodeIdentifier name, int childSizeHint) throws IOException {
99 containerTypeStack.push(ContainerType.ANY_XML);
100 startElement(name.getNodeType());
104 protected void writeValue(XMLStreamWriter xmlWriter, QName qname, @Nonnull Object value, Object context)
105 throws XMLStreamException {
106 xmlWriter.writeCharacters(value.toString());
110 protected void startList(NodeIdentifier name) {
111 containerTypeStack.push(ContainerType.LIST);
115 protected void startListItem(PathArgument name) throws IOException {
116 containerTypeStack.push(ContainerType.LIST_ITEM);
117 startElement(name.getNodeType());
121 protected void endNode(XMLStreamWriter xmlWriter) throws IOException, XMLStreamException {
122 ContainerType type = containerTypeStack.pop();
127 xmlWriter.writeEndElement();