Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / DOMSourceMountPointChild.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 java.io.IOException;
13 import java.net.URISyntaxException;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.stream.XMLStreamReader;
16 import javax.xml.transform.dom.DOMSource;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.impl.schema.AbstractMountPointChild;
21 import org.xml.sax.SAXException;
22
23 /**
24  * Internal MountPointChild implementation, reusing data bits from {@link DOMSourceAnydata}.
25  */
26 @NonNullByDefault
27 final class DOMSourceMountPointChild extends AbstractMountPointChild {
28     private final DOMSource source;
29
30     DOMSourceMountPointChild(final DOMSource source) {
31         this.source = requireNonNull(source);
32     }
33
34     @Override
35     public void writeTo(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx) throws IOException {
36
37         final XmlParserStream xmlParser;
38         try {
39             xmlParser = XmlParserStream.create(writer, mountCtx, mountCtx.getSchemaContext());
40         } catch (IllegalArgumentException e) {
41             throw new IOException("Failed to instantiate XML parser", e);
42         }
43
44         try {
45             final XMLStreamReader reader = new DOMSourceXMLStreamReader(source);
46             xmlParser.parse(reader).flush();
47         } catch (XMLStreamException | URISyntaxException | SAXException e) {
48             throw new IOException("Failed to parse payload", e);
49         }
50     }
51 }