2 * Copyright © 2019 FRINX s.r.o. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors;
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;
27 * XML stream-writer with disabled leaf-type validation for specified QName.
29 final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
31 private static final XMLOutputFactory XML_FACTORY;
34 XML_FACTORY = XMLOutputFactory.newFactory();
35 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
38 private final XMLStreamWriter xmlWriter;
39 private final NormalizedNodeStreamWriter xmlNodeStreamWriter;
42 * Creation of the custom XML stream-writer.
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.
49 XmlStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStream outputStream,
50 final SchemaPath schemaPath, final SchemaContextHandler schemaContextHandler) {
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);
57 this.xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
58 schemaContextHandler.get(), schemaPath);
62 protected NormalizedNodeStreamWriter delegate() {
63 return xmlNodeStreamWriter;
67 void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
68 final String namespace = nodeIdentifier.getNodeType().getNamespace().toString();
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);
78 void scalarValueWithDisabledValidation(final Object value) throws IOException {
80 xmlWriter.writeCharacters(value.toString());
81 } catch (XMLStreamException e) {
82 throw new IOException("Error writing value", e);
87 void endNodeWithDisabledValidation() throws IOException {
89 xmlWriter.writeEndElement();
90 } catch (XMLStreamException e) {
91 throw new IOException("Error writing end-node", e);
96 public void close() throws IOException {
97 xmlNodeStreamWriter.close();
100 } catch (XMLStreamException e) {
101 throw new IOException(e);