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 static com.google.common.base.Preconditions.checkState;
12 import java.io.IOException;
13 import java.util.ArrayDeque;
14 import java.util.Deque;
15 import javax.xml.stream.XMLStreamWriter;
16 import javax.xml.transform.dom.DOMSource;
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.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 final class SchemalessXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<Object> {
24 private enum NodeType {
37 private final Deque<NodeType> nodeTypeStack = new ArrayDeque<>();
39 SchemalessXMLStreamNormalizedNodeStreamWriter(final XMLStreamWriter writer) {
44 public void startLeafNode(final NodeIdentifier name) throws IOException {
45 nodeTypeStack.push(NodeType.SCALAR);
46 startElement(name.getNodeType());
50 public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
51 nodeTypeStack.push(NodeType.SCALAR);
52 startElement(name.getNodeType());
56 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
57 nodeTypeStack.push(NodeType.LEAF_SET);
61 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
62 nodeTypeStack.push(NodeType.LEAF_SET);
66 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
67 nodeTypeStack.push(NodeType.CONTAINER);
68 startElement(name.getNodeType());
72 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
73 nodeTypeStack.push(NodeType.CHOICE);
77 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
78 nodeTypeStack.push(NodeType.AUGMENTATION);
82 public boolean startAnyxmlNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
83 if (DOMSource.class.isAssignableFrom(objectModel)) {
84 nodeTypeStack.push(NodeType.ANY_XML);
85 startElement(name.getNodeType());
92 String encodeValue(final ValueWriter xmlWriter, final Object value, final Object context) {
93 return value.toString();
97 String encodeAnnotationValue(final ValueWriter xmlWriter, final QName qname, final Object value) {
98 return value.toString();
102 void startList(final NodeIdentifier name) {
103 nodeTypeStack.push(NodeType.LIST);
107 void startListItem(final PathArgument name) throws IOException {
108 nodeTypeStack.push(NodeType.LIST_ITEM);
109 startElement(name.getNodeType());
113 public void scalarValue(final Object value) throws IOException {
114 final NodeType type = nodeTypeStack.peek();
115 if (type == NodeType.SCALAR) {
116 writeValue(value, null);
117 } else if (type == NodeType.ANYDATA) {
120 throw new IllegalStateException("Unexpected scalar " + value + " in type " + type);
125 public void domSourceValue(final DOMSource value) throws IOException {
126 final NodeType type = nodeTypeStack.peek();
127 checkState(type == NodeType.ANY_XML, "Unexpected DOMSource %s in %s", value, type);
132 public void endNode() throws IOException {
133 NodeType type = nodeTypeStack.pop();
148 void startAnydata(final NodeIdentifier name) {
149 nodeTypeStack.push(NodeType.ANYDATA);