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.xml.stream.XMLStreamException;
16 import javax.xml.stream.XMLStreamWriter;
17 import org.opendaylight.yangtools.yang.common.QName;
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.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 class SchemalessXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<Object> {
24 private enum ContainerType {
34 private final Deque<ContainerType> containerTypeStack = new ArrayDeque<>();
36 private SchemalessXMLStreamNormalizedNodeStreamWriter(XMLStreamWriter writer) {
40 static NormalizedNodeStreamWriter newInstance(XMLStreamWriter writer) {
41 return new SchemalessXMLStreamNormalizedNodeStreamWriter(writer);
45 public void leafNode(NodeIdentifier name, Object value, Map<QName, String> attributes) throws IOException {
46 writeElement(name.getNodeType(), value, attributes, null);
50 public void leafSetEntryNode(QName name, Object value, Map<QName, String> attributes) throws IOException {
51 writeElement(name, value, attributes, null);
55 public void leafNode(NodeIdentifier name, Object value) throws IOException {
56 writeElement(name.getNodeType(), value, Collections.emptyMap(), null);
60 public void leafSetEntryNode(QName name, Object value) throws IOException {
61 writeElement(name, value, Collections.emptyMap(), null);
65 public void startLeafSet(NodeIdentifier name, int childSizeHint) throws IOException {
66 containerTypeStack.push(ContainerType.LEAF_SET);
70 public void startOrderedLeafSet(NodeIdentifier name, int childSizeHint)
71 throws IOException, IllegalArgumentException {
72 containerTypeStack.push(ContainerType.LEAF_SET);
76 public void startContainerNode(NodeIdentifier name, int childSizeHint) throws IOException {
77 containerTypeStack.push(ContainerType.CONTAINER);
78 startElement(name.getNodeType());
82 public void startChoiceNode(NodeIdentifier name, int childSizeHint) throws IOException {
83 containerTypeStack.push(ContainerType.CHOICE);
87 public void startAugmentationNode(AugmentationIdentifier identifier) throws IOException {
88 containerTypeStack.push(ContainerType.AUGMENTATION);
92 public void anyxmlNode(NodeIdentifier name, Object value) throws IOException {
93 anyxmlNode(name.getNodeType(), value);
97 public void startYangModeledAnyXmlNode(NodeIdentifier name, int childSizeHint) throws IOException {
98 containerTypeStack.push(ContainerType.ANY_XML);
99 startElement(name.getNodeType());
103 protected void writeValue(XMLStreamWriter xmlWriter, QName qname, Object value, Object context)
104 throws XMLStreamException {
105 xmlWriter.writeCharacters(value.toString());
109 protected void startList(NodeIdentifier name) {
110 containerTypeStack.push(ContainerType.LIST);
114 protected void startListItem(PathArgument name) throws IOException {
115 containerTypeStack.push(ContainerType.LIST_ITEM);
116 startElement(name.getNodeType());
120 protected void endNode(XMLStreamWriter xmlWriter) throws IOException, XMLStreamException {
121 ContainerType type = containerTypeStack.pop();
126 xmlWriter.writeEndElement();