d1c8917abba13c37dee0176e47334941fa448be9
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / errors / XmlStreamWriterWithDisabledValidation.java
1 /*
2  * Copyright © 2019 FRINX s.r.o. 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.restconf.nb.rfc8040.jersey.providers.errors;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.nio.charset.StandardCharsets;
13 import javax.xml.XMLConstants;
14 import javax.xml.stream.FactoryConfigurationError;
15 import javax.xml.stream.XMLOutputFactory;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.stream.XMLStreamWriter;
18 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25
26 /**
27  * XML stream-writer with disabled leaf-type validation for specified QName.
28  */
29 final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
30
31     private static final XMLOutputFactory XML_FACTORY;
32
33     static {
34         XML_FACTORY = XMLOutputFactory.newFactory();
35         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
36     }
37
38     private final XMLStreamWriter xmlWriter;
39     private final NormalizedNodeStreamWriter xmlNodeStreamWriter;
40
41     /**
42      * Creation of the custom XML stream-writer.
43      *
44      * @param excludedQName        QName of the element that is excluded from type-check.
45      * @param outputStream         Output stream that is used for creation of JSON writers.
46      * @param schemaPath           Schema-path of the {@link NormalizedNode} to be written.
47      * @param schemaContextHandler Handler that holds actual schema context.
48      */
49     XmlStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStream outputStream,
50             final SchemaPath schemaPath, final SchemaContextHandler schemaContextHandler) {
51         super(excludedQName);
52         try {
53             this.xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name());
54         } catch (final XMLStreamException | FactoryConfigurationError e) {
55             throw new IllegalStateException("Cannot create XML writer", e);
56         }
57         this.xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
58                 schemaContextHandler.get(), schemaPath);
59     }
60
61     @Override
62     protected NormalizedNodeStreamWriter delegate() {
63         return xmlNodeStreamWriter;
64     }
65
66     @Override
67     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
68         final String namespace = nodeIdentifier.getNodeType().getNamespace().toString();
69         try {
70             xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,
71                     nodeIdentifier.getNodeType().getLocalName(), namespace);
72         } catch (XMLStreamException e) {
73             throw new IOException("Error writing leaf node", e);
74         }
75     }
76
77     @Override
78     void scalarValueWithDisabledValidation(final Object value) throws IOException {
79         try {
80             xmlWriter.writeCharacters(value.toString());
81         } catch (XMLStreamException e) {
82             throw new IOException("Error writing value", e);
83         }
84     }
85
86     @Override
87     void endNodeWithDisabledValidation() throws IOException {
88         try {
89             xmlWriter.writeEndElement();
90         } catch (XMLStreamException e) {
91             throw new IOException("Error writing end-node", e);
92         }
93     }
94
95     @Override
96     public void close() throws IOException {
97         xmlNodeStreamWriter.close();
98         try {
99             xmlWriter.close();
100         } catch (XMLStreamException e) {
101             throw new IOException(e);
102         }
103     }
104 }