2 * Copyright © 2021 FRINX s.r.o. 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.netconf.util;
10 import java.io.IOException;
11 import javax.xml.XMLConstants;
12 import javax.xml.stream.XMLStreamException;
13 import javax.xml.stream.XMLStreamWriter;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 * Proxy {@link NormalizedNodeStreamWriter} that is responsible for serialization of empty leaf-list and list
21 * nodes to output {@link XMLStreamWriter} as empty XML elements. Other operations are proxied
22 * to delegated {@link NormalizedNodeStreamWriter}.
24 final class EmptyListXmlWriter extends ForwardingNormalizedNodeStreamWriter {
25 private final NormalizedNodeStreamWriter delegatedWriter;
26 private final XMLStreamWriter xmlStreamWriter;
28 private boolean isInsideEmptyList = false;
30 EmptyListXmlWriter(final NormalizedNodeStreamWriter delegatedWriter, final XMLStreamWriter xmlStreamWriter) {
31 this.delegatedWriter = delegatedWriter;
32 this.xmlStreamWriter = xmlStreamWriter;
36 protected NormalizedNodeStreamWriter delegate() {
37 return delegatedWriter;
41 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
42 if (childSizeHint == 0) {
43 writeEmptyElement(name);
45 super.startUnkeyedList(name, childSizeHint);
50 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
51 if (childSizeHint == 0) {
52 writeEmptyElement(name);
54 super.startMapNode(name, childSizeHint);
59 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
60 if (childSizeHint == 0) {
61 writeEmptyElement(name);
63 super.startOrderedMapNode(name, childSizeHint);
68 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
69 if (childSizeHint == 0) {
70 writeEmptyElement(name);
72 super.startLeafSet(name, childSizeHint);
77 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
78 if (childSizeHint == 0) {
79 writeEmptyElement(name);
81 super.startOrderedLeafSet(name, childSizeHint);
86 public void endNode() throws IOException {
87 if (isInsideEmptyList) {
88 isInsideEmptyList = false;
94 private void writeEmptyElement(final NodeIdentifier identifier) throws IOException {
95 final QName nodeType = identifier.getNodeType();
97 xmlStreamWriter.writeEmptyElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(),
98 nodeType.getNamespace().toString());
99 } catch (XMLStreamException e) {
100 throw new IOException("Failed to serialize empty element to XML: " + identifier, e);
102 isInsideEmptyList = true;