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.server.api.DatabindContext;
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;
24 * XML stream-writer with disabled leaf-type validation for specified QName.
26 final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
27 private static final XMLOutputFactory XML_FACTORY;
30 XML_FACTORY = XMLOutputFactory.newFactory();
31 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
34 private final XMLStreamWriter xmlWriter;
35 private final NormalizedNodeStreamWriter xmlNodeStreamWriter;
38 * Creation of the custom XML stream-writer.
40 * @param databindContext {@link DatabindContext} to use
41 * @param outputStream Output stream that is used for creation of JSON writers.
43 XmlStreamWriterWithDisabledValidation(final DatabindContext databindContext, final OutputStream outputStream) {
45 xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name());
46 } catch (final XMLStreamException | FactoryConfigurationError e) {
47 throw new IllegalStateException("Cannot create XML writer", e);
50 xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
51 errorsContainerInference(databindContext));
55 protected NormalizedNodeStreamWriter delegate() {
56 return xmlNodeStreamWriter;
60 void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
61 final String namespace = nodeIdentifier.getNodeType().getNamespace().toString();
63 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,
64 nodeIdentifier.getNodeType().getLocalName(), namespace);
65 } catch (XMLStreamException e) {
66 throw new IOException("Error writing leaf node", e);
71 void scalarValueWithDisabledValidation(final Object value) throws IOException {
73 xmlWriter.writeCharacters(value.toString());
74 } catch (XMLStreamException e) {
75 throw new IOException("Error writing value", e);
80 void endNodeWithDisabledValidation() throws IOException {
82 xmlWriter.writeEndElement();
83 } catch (XMLStreamException e) {
84 throw new IOException("Error writing end-node", e);
89 public void close() throws IOException {
90 xmlNodeStreamWriter.close();
93 } catch (XMLStreamException e) {
94 throw new IOException(e);