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.listeners;
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 INSTANCE = new XMLNotificationFormatter();
23 static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory() {
25 public XMLNotificationFormatter getFormatter(final String xpathFilter) throws XPathExpressionException {
26 return new XMLNotificationFormatter(xpathFilter);
30 public XMLNotificationFormatter getFormatter() {
35 XMLNotificationFormatter() {
39 XMLNotificationFormatter(final String xpathFilter) throws XPathExpressionException {
44 String createText(final EffectiveModelContext schemaContext, final DOMNotification input, final Instant now,
45 final boolean leafNodesOnly, final boolean skipData) throws IOException {
46 final var writer = new StringWriter();
49 final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
50 final var nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext,
53 try (var nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter)) {
54 nnWriter.write(input.getBody());
57 xmlStreamWriter.writeEndElement();
58 xmlStreamWriter.writeEndDocument();
59 xmlStreamWriter.flush();
61 } catch (XMLStreamException e) {
62 throw new IOException("Failed to write notification content", e);
65 return writer.toString();