Fix anyData content normalization incl no data case
[yangtools.git] / codec / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / DOMSourceAnydata.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.xml;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import java.io.IOException;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.stream.XMLStreamReader;
17 import javax.xml.transform.dom.DOMSource;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.impl.schema.AbstractNormalizableAnydata;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
22
23 /**
24  * Internal parser representation of a parsed-out chunk of XML. This format is completely internal to the parser
25  * and can be changed at any time. Current implementation uses W3C DOM tree as the backing implementations, but others
26  * are possible as well.
27  *
28  * <p>
29  * Note that the DOMSource is expected to contain a top-level synthetic element, which acts as holder of namespace
30  * declarations coming from parsing context but is otherwise ignored. Parser-side of things is expected to reuse the
31  * {@code anydata} element name for this purpose. Writer-side of things is expected to skip this element except for
32  * its namespace declarations.
33  *
34  * @author Robert Varga
35  */
36 @NonNullByDefault
37 final class DOMSourceAnydata extends AbstractNormalizableAnydata {
38     private final DOMSource source;
39
40     DOMSourceAnydata(final DOMSource source) {
41         this.source = requireNonNull(source);
42     }
43
44     XMLStreamReader toStreamReader() throws XMLStreamException {
45         return new DOMSourceXMLStreamReader(source);
46     }
47
48     @Override
49     protected void writeTo(final NormalizedNodeStreamWriter streamWriter, final EffectiveStatementInference inference)
50             throws IOException {
51         final XmlParserStream xmlParser;
52         try {
53             xmlParser = XmlParserStream.create(streamWriter, inference);
54         } catch (IllegalArgumentException e) {
55             throw new IOException("Failed to instantiate XML parser", e);
56         }
57
58         try {
59             final XMLStreamReader reader = toStreamReader();
60             xmlParser.parse(reader).flush();
61         } catch (XMLStreamException e) {
62             throw new IOException("Failed to parse payload", e);
63         }
64     }
65
66     @Override
67     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
68         return helper.add("source", source);
69     }
70
71     @VisibleForTesting
72     DOMSource getSource() {
73         return source;
74     }
75 }