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