2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.data.codec.xml;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import java.io.IOException;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.stream.XMLStreamWriter;
17 import javax.xml.transform.dom.DOMSource;
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;
22 import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
32 final class SchemaAwareXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<SchemaNode>
33 implements SchemaContextProvider {
34 private final SchemaTracker tracker;
35 private final SchemaAwareXMLStreamWriterUtils streamUtils;
37 SchemaAwareXMLStreamNormalizedNodeStreamWriter(final XMLStreamWriter writer, final SchemaContext context,
38 final SchemaTracker tracker) {
40 this.tracker = requireNonNull(tracker);
41 this.streamUtils = new SchemaAwareXMLStreamWriterUtils(context);
45 String encodeValue(final ValueWriter xmlWriter, final Object value, final SchemaNode schemaNode)
46 throws XMLStreamException {
47 return streamUtils.encodeValue(xmlWriter, schemaNode, value, schemaNode.getQName().getModule());
51 void startList(final NodeIdentifier name) {
52 tracker.startList(name);
56 void startListItem(final PathArgument name) throws IOException {
57 tracker.startListItem(name);
58 startElement(name.getNodeType());
62 public void endNode() throws IOException {
63 final Object schema = tracker.endNode();
64 if (schema instanceof ListSchemaNode || schema instanceof LeafListSchemaNode) {
65 // For lists, we only emit end element on the inner frame
66 final Object parent = tracker.getParent();
67 if (parent == schema) {
70 } else if (schema instanceof ContainerSchemaNode || schema instanceof LeafSchemaNode) {
76 public void startLeafNode(final NodeIdentifier name) throws IOException {
77 tracker.startLeafNode(name);
78 startElement(name.getNodeType());
82 public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
83 tracker.startLeafSetEntryNode(name);
84 startElement(name.getNodeType());
88 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) {
89 tracker.startLeafSet(name);
93 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) {
94 tracker.startLeafSet(name);
98 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
99 final SchemaNode schema = tracker.startContainerNode(name);
100 startElement(schema.getQName());
104 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
105 tracker.startChoiceNode(name);
109 public void startAugmentationNode(final AugmentationIdentifier identifier) {
110 tracker.startAugmentationNode(identifier);
114 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
115 final SchemaNode schema = tracker.startYangModeledAnyXmlNode(name);
116 startElement(schema.getQName());
120 public void startAnyxmlNode(final NodeIdentifier name) throws IOException {
121 tracker.startAnyxmlNode(name);
122 startElement(name.getNodeType());
126 public SchemaContext getSchemaContext() {
127 return streamUtils.getSchemaContext();
131 public void scalarValue(final Object value) throws IOException {
132 final Object current = tracker.getParent();
133 checkState(current instanceof LeafSchemaNode || current instanceof LeafListSchemaNode,
134 "Unexpected scalar value %s with %s", value, current);
135 writeValue(value, (SchemaNode) current);
139 public void domSourceValue(final DOMSource value) throws IOException {
140 final Object current = tracker.getParent();
141 checkState(current instanceof AnyXmlSchemaNode, "Unexpected scala value %s with %s", value, current);