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 com.google.common.annotations.VisibleForTesting;
11 import java.io.IOException;
12 import java.io.StringWriter;
13 import java.time.Instant;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.xpath.XPathExpressionException;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
18 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 final class XMLNotificationFormatter extends NotificationFormatter {
23 static final XMLNotificationFormatter EMPTY = new XMLNotificationFormatter(TextParameters.EMPTY);
24 static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory(EMPTY) {
26 public XMLNotificationFormatter newFormatter(final TextParameters textParams) {
27 return new XMLNotificationFormatter(textParams);
31 public XMLNotificationFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
32 throws XPathExpressionException {
33 return new XMLNotificationFormatter(textParams, xpathFilter);
37 XMLNotificationFormatter(final TextParameters textParams) {
41 XMLNotificationFormatter(final TextParameters textParams, final String xpathFilter)
42 throws XPathExpressionException {
43 super(textParams, xpathFilter);
47 String createText(final TextParameters params, final EffectiveModelContext schemaContext,
48 final DOMNotification input, final Instant now) throws IOException {
49 final var writer = new StringWriter();
52 final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
53 try (var nnWriter = NormalizedNodeWriter.forStreamWriter(XMLStreamNormalizedNodeStreamWriter.create(
54 xmlStreamWriter, schemaContext, input.getType()))) {
55 nnWriter.write(input.getBody());
58 xmlStreamWriter.writeEndElement();
59 xmlStreamWriter.writeEndDocument();
60 xmlStreamWriter.flush();
62 } catch (XMLStreamException e) {
63 throw new IOException("Failed to write notification content", e);
66 return writer.toString();