fbf2a09a88cfd19360a393a8966ac2fab23c8268
[netconf.git] / restconf / restconf-nb / 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.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
22
23 /**
24  * XML stream-writer with disabled leaf-type validation for specified QName.
25  */
26 final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
27     private static final XMLOutputFactory XML_FACTORY;
28
29     static {
30         XML_FACTORY = XMLOutputFactory.newFactory();
31         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
32     }
33
34     private final XMLStreamWriter xmlWriter;
35     private final NormalizedNodeStreamWriter xmlNodeStreamWriter;
36
37     /**
38      * Creation of the custom XML stream-writer.
39      *
40      * @param schemaContextHandler Handler that holds actual schema context.
41      * @param outputStream         Output stream that is used for creation of JSON writers.
42      */
43     XmlStreamWriterWithDisabledValidation(final SchemaContextHandler schemaContextHandler,
44             final OutputStream outputStream) {
45         try {
46             xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name());
47         } catch (final XMLStreamException | FactoryConfigurationError e) {
48             throw new IllegalStateException("Cannot create XML writer", e);
49         }
50
51         xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
52             errorsContainerInference(schemaContextHandler));
53     }
54
55     @Override
56     protected NormalizedNodeStreamWriter delegate() {
57         return xmlNodeStreamWriter;
58     }
59
60     @Override
61     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
62         final String namespace = nodeIdentifier.getNodeType().getNamespace().toString();
63         try {
64             xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,
65                     nodeIdentifier.getNodeType().getLocalName(), namespace);
66         } catch (XMLStreamException e) {
67             throw new IOException("Error writing leaf node", e);
68         }
69     }
70
71     @Override
72     void scalarValueWithDisabledValidation(final Object value) throws IOException {
73         try {
74             xmlWriter.writeCharacters(value.toString());
75         } catch (XMLStreamException e) {
76             throw new IOException("Error writing value", e);
77         }
78     }
79
80     @Override
81     void endNodeWithDisabledValidation() throws IOException {
82         try {
83             xmlWriter.writeEndElement();
84         } catch (XMLStreamException e) {
85             throw new IOException("Error writing end-node", e);
86         }
87     }
88
89     @Override
90     public void close() throws IOException {
91         xmlNodeStreamWriter.close();
92         try {
93             xmlWriter.close();
94         } catch (XMLStreamException e) {
95             throw new IOException(e);
96         }
97     }
98 }