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