Expose EffectiveStatementInference from NormalizedAnyData
[yangtools.git] / yang / 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 java.net.URISyntaxException;
16 import java.util.List;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamReader;
19 import javax.xml.transform.dom.DOMSource;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.impl.schema.AbstractNormalizableAnydata;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.xml.sax.SAXException;
27
28 /**
29  * Internal parser representation of a parsed-out chunk of XML. This format is completely internal to the parser
30  * and can be changed at any time. Current implementation uses W3C DOM tree as the backing implementations, but others
31  * are possible as well.
32  *
33  * <p>
34  * Note that the DOMSource is expected to contain a top-level synthetic element, which acts as holder of namespace
35  * declarations coming from parsing context but is otherwise ignored. Parser-side of things is expected to reuse the
36  * {@code anydata} element name for this purpose. Writer-side of things is expected to skip this element except for
37  * its namespace declarations.
38  *
39  * @author Robert Varga
40  */
41 @NonNullByDefault
42 final class DOMSourceAnydata extends AbstractNormalizableAnydata {
43     private final DOMSource source;
44
45     DOMSourceAnydata(final DOMSource source) {
46         this.source = requireNonNull(source);
47     }
48
49     XMLStreamReader toStreamReader() throws XMLStreamException {
50         return new DOMSourceXMLStreamReader(source);
51     }
52
53     @Override
54     protected void writeTo(final NormalizedNodeStreamWriter streamWriter, final EffectiveStatementInference inference)
55             throws IOException {
56         final List<? extends EffectiveStatement<?, ?>> path = inference.statementPath();
57         if (path.isEmpty()) {
58             throw new IOException("Cannot handle empty inference " + inference);
59         }
60         final EffectiveStatement<?, ?> stmt = path.get(path.size() - 1);
61         if (!(stmt instanceof SchemaNode)) {
62             throw new IOException("Unhandled statement " + stmt);
63         }
64
65         final XmlParserStream xmlParser;
66         try {
67             xmlParser = XmlParserStream.create(streamWriter, inference.getEffectiveModelContext(), (SchemaNode) stmt);
68         } catch (IllegalArgumentException e) {
69             throw new IOException("Failed to instantiate XML parser", e);
70         }
71
72         try {
73             final XMLStreamReader reader = toStreamReader();
74             reader.nextTag();
75
76             xmlParser.parse(reader).flush();
77         } catch (XMLStreamException | URISyntaxException | SAXException e) {
78             throw new IOException("Failed to parse payload", e);
79         }
80     }
81
82     @Override
83     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
84         return helper.add("source", source);
85     }
86
87     @VisibleForTesting
88     DOMSource getSource() {
89         return source;
90     }
91 }