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.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 schemaContextHandler Handler that holds actual schema context.
41 * @param outputStream Output stream that is used for creation of JSON writers.
43 XmlStreamWriterWithDisabledValidation(final SchemaContextHandler schemaContextHandler,
44 final OutputStream outputStream) {
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);
51 xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
52 errorsContainerInference(schemaContextHandler));
56 protected NormalizedNodeStreamWriter delegate() {
57 return xmlNodeStreamWriter;
61 void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
62 final String namespace = nodeIdentifier.getNodeType().getNamespace().toString();
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);
72 void scalarValueWithDisabledValidation(final Object value) throws IOException {
74 xmlWriter.writeCharacters(value.toString());
75 } catch (XMLStreamException e) {
76 throw new IOException("Error writing value", e);
81 void endNodeWithDisabledValidation() throws IOException {
83 xmlWriter.writeEndElement();
84 } catch (XMLStreamException e) {
85 throw new IOException("Error writing end-node", e);
90 public void close() throws IOException {
91 xmlNodeStreamWriter.close();
94 } catch (XMLStreamException e) {
95 throw new IOException(e);