+
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
+ final @Nullable NormalizedMetadata metadata,
+ final DOMResult result, final SchemaPath schemaPath,
+ final SchemaContext context) throws IOException, XMLStreamException {
+ if (metadata == null) {
+ writeNormalizedNode(normalized, result, schemaPath, context);
+ return;
+ }
+
+ final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
+ XML_NAMESPACE_SETTER.initializeNamespace(writer);
+ try (
+ NormalizedNodeStreamWriter normalizedNodeStreamWriter =
+ XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
+ NormalizedMetadataWriter normalizedNodeWriter =
+ NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
+ ) {
+ normalizedNodeWriter.write(normalized, metadata);
+ normalizedNodeWriter.flush();
+ } finally {
+ try {
+ if (writer != null) {
+ writer.close();
+ }
+ } catch (final Exception e) {
+ LOG.warn("Unable to close resource properly", e);
+ }
+ }
+ }
+
+ public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
+ final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
+ if (query.isEmpty()) {
+ // No query at all
+ return;
+ }
+
+ final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+ try {
+ try (NormalizedNodeStreamWriter writer =
+ XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
+ final Iterator<PathArgument> it = query.getPathArguments().iterator();
+ final PathArgument first = it.next();
+ StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
+ it);
+ }
+ } finally {
+ xmlWriter.close();
+ }
+ }
+
+ public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mountContext,
+ final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
+ final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
+ final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
+ final XmlCodecFactory codecs = XmlCodecFactory.create(mountContext);
+
+ // FIXME: we probably need to propagate MountPointContext here and not just the child nodes
+ final ContainerSchemaNode dataRead = new NodeContainerProxy(NETCONF_DATA_QNAME,
+ mountContext.getSchemaContext().getChildNodes());
+ try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, codecs, dataRead)) {
+ xmlParserStream.traverse(value);
+ }
+ return resultHolder;
+ }
+
+
+ // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
+ public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final EffectiveModelContext schemaContext,
+ final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
+ return transformDOMSourceToNormalizedNode(new EmptyMountPointContext(schemaContext), value);
+ }