+ xmlWriter.close();
+ }
+ }
+
+ /**
+ * Write {@code normalized} data along with corresponding {@code metadata} into {@link DOMResult}.
+ *
+ * @param normalized data to be written
+ * @param metadata metadata to be written
+ * @param result DOM result holder
+ * @param schemaPath schema path of the parent node
+ * @param context mountpoint schema context
+ * @throws IOException when failed to write data into {@link NormalizedNodeStreamWriter}
+ * @throws XMLStreamException when failed to serialize data into XML document
+ */
+ public static void writeNormalizedNode(final NormalizedNode normalized, final @Nullable NormalizedMetadata metadata,
+ final DOMResult result, final SchemaPath schemaPath, final EffectiveModelContext context)
+ throws IOException, XMLStreamException {
+ if (metadata == null) {
+ writeNormalizedNode(normalized, result, schemaPath, context);
+ return;
+ }
+
+ final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+ XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter);
+ try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+ var writer = NormalizedMetadataWriter.forStreamWriter(streamWriter)) {
+ writer.write(normalized, metadata);
+ writer.flush();
+ } finally {
+ xmlWriter.close();
+ }
+ }
+
+ /**
+ * Write data specified by {@link YangInstanceIdentifier} into {@link DOMResult}.
+ *
+ * @param query path to the root node
+ * @param result DOM result holder
+ * @param schemaPath schema path of the parent node
+ * @param context mountpoint schema context
+ * @throws IOException when failed to write data into {@link NormalizedNodeStreamWriter}
+ * @throws XMLStreamException when failed to serialize data into XML document
+ */
+ public static void writeNormalizedNode(final YangInstanceIdentifier query, final DOMResult result,
+ final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
+ final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+ XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter);
+ try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+ var writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
+ final Iterator<PathArgument> it = query.getPathArguments().iterator();
+ final PathArgument first = it.next();
+ StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
+ } finally {
+ xmlWriter.close();
+ }
+ }
+
+ /**
+ * Write data specified by {@link YangInstanceIdentifier} along with corresponding {@code metadata}
+ * into {@link DOMResult}.
+ *
+ * @param query path to the root node
+ * @param metadata metadata to be written
+ * @param result DOM result holder
+ * @param schemaPath schema path of the parent node
+ * @param context mountpoint schema context
+ * @throws IOException when failed to write data into {@link NormalizedNodeStreamWriter}
+ * @throws XMLStreamException when failed to serialize data into XML document
+ */
+ public static void writeNormalizedNode(final YangInstanceIdentifier query,
+ final @Nullable NormalizedMetadata metadata, final DOMResult result, final SchemaPath schemaPath,
+ final EffectiveModelContext context) throws IOException, XMLStreamException {
+ if (metadata == null) {
+ writeNormalizedNode(query, result, schemaPath, context);
+ return;
+ }
+
+ final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+ XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter);
+ try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+ var writer = new EmptyListXmlMetadataWriter(streamWriter, xmlWriter,
+ streamWriter.getExtensions().getInstance(StreamWriterMetadataExtension.class), metadata)) {
+ final Iterator<PathArgument> it = query.getPathArguments().iterator();
+ final PathArgument first = it.next();
+ StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
+ } finally {
+ xmlWriter.close();
+ }
+ }
+
+ /**
+ * Writing subtree filter specified by {@link YangInstanceIdentifier} into {@link DOMResult}.
+ *
+ * @param query path to the root node
+ * @param result DOM result holder
+ * @param schemaPath schema path of the parent node
+ * @param context mountpoint schema context
+ * @throws IOException failed to write filter into {@link NormalizedNodeStreamWriter}
+ * @throws XMLStreamException failed to serialize filter into XML document
+ */
+ public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
+ final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
+ if (query.isEmpty()) {
+ // No query at all
+ return;
+ }
+
+ final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+ try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+ var writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
+ final Iterator<PathArgument> it = query.getPathArguments().iterator();
+ final PathArgument first = it.next();
+ StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
+ } finally {
+ xmlWriter.close();
+ }
+ }
+
+ /**
+ * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
+ * into {@link DOMResult}. Field paths are relative to parent query path.
+ *
+ * @param query path to the root node
+ * @param result DOM result holder
+ * @param schemaPath schema path of the parent node
+ * @param context mountpoint schema context
+ * @param fields list of specific fields for which the filter should be created
+ * @throws IOException failed to write filter into {@link NormalizedNodeStreamWriter}
+ * @throws XMLStreamException failed to serialize filter into XML document
+ * @throws NullPointerException if any argument is null
+ */
+ public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
+ final SchemaPath schemaPath, final EffectiveModelContext context,
+ final List<YangInstanceIdentifier> fields) throws IOException, XMLStreamException {
+ if (query.isEmpty() || fields.isEmpty()) {
+ // No query at all
+ return;
+ }
+
+ final List<YangInstanceIdentifier> aggregatedFields = aggregateFields(fields);
+ final PathNode rootNode = constructPathArgumentTree(query, aggregatedFields);
+
+ final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+ try {
+ try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+ var writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
+ final PathArgument first = rootNode.element();
+ StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
+ .streamToWriter(writer, first, rootNode);