2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. 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.streams;
10 import java.io.IOException;
11 import java.io.StringWriter;
12 import java.time.Instant;
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.xpath.XPathExpressionException;
15 import org.opendaylight.mdsal.dom.api.DOMNotification;
16 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
17 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 final class XMLNotificationFormatter extends NotificationFormatter {
21 private static final XMLNotificationFormatter EMPTY = new XMLNotificationFormatter(TextParameters.EMPTY);
23 static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory(EMPTY) {
25 XMLNotificationFormatter newFormatter(final TextParameters textParams) {
26 return new XMLNotificationFormatter(textParams);
30 XMLNotificationFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
31 throws XPathExpressionException {
32 return new XMLNotificationFormatter(textParams, xpathFilter);
36 XMLNotificationFormatter(final TextParameters textParams) {
40 XMLNotificationFormatter(final TextParameters textParams, final String xpathFilter)
41 throws XPathExpressionException {
42 super(textParams, xpathFilter);
46 String createText(final TextParameters params, final EffectiveModelContext schemaContext,
47 final DOMNotification input, final Instant now) throws IOException {
48 final var writer = new StringWriter();
51 final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
52 final var nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext,
55 try (var nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter)) {
56 nnWriter.write(input.getBody());
59 xmlStreamWriter.writeEndElement();
60 xmlStreamWriter.writeEndDocument();
61 xmlStreamWriter.flush();
63 } catch (XMLStreamException e) {
64 throw new IOException("Failed to write notification content", e);
67 return writer.toString();