StreamWriterMountPointExtension operates on MountPointLabel
[yangtools.git] / codec / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlParserStream.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.xml;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static java.util.Objects.requireNonNull;
14
15 import com.google.common.annotations.Beta;
16 import com.google.common.base.Strings;
17 import com.google.common.collect.ImmutableMap;
18 import java.io.Closeable;
19 import java.io.Flushable;
20 import java.io.IOException;
21 import java.net.URISyntaxException;
22 import java.util.AbstractMap.SimpleImmutableEntry;
23 import java.util.Deque;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Optional;
31 import java.util.Set;
32 import javax.xml.XMLConstants;
33 import javax.xml.namespace.NamespaceContext;
34 import javax.xml.stream.XMLStreamConstants;
35 import javax.xml.stream.XMLStreamException;
36 import javax.xml.stream.XMLStreamReader;
37 import javax.xml.stream.util.StreamReaderDelegate;
38 import javax.xml.transform.TransformerException;
39 import javax.xml.transform.TransformerFactory;
40 import javax.xml.transform.TransformerFactoryConfigurationError;
41 import javax.xml.transform.dom.DOMResult;
42 import javax.xml.transform.dom.DOMSource;
43 import javax.xml.transform.stax.StAXSource;
44 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode;
45 import org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants;
46 import org.opendaylight.yangtools.rfc8528.model.api.MountPointLabel;
47 import org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode;
48 import org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.common.QNameModule;
51 import org.opendaylight.yangtools.yang.common.XMLNamespace;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
54 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContextFactory.ContainerName;
55 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
56 import org.opendaylight.yangtools.yang.data.util.AbstractMountPointDataWithSchema;
57 import org.opendaylight.yangtools.yang.data.util.AbstractNodeDataWithSchema;
58 import org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema;
59 import org.opendaylight.yangtools.yang.data.util.AnydataNodeDataWithSchema;
60 import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema;
61 import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema.ChildReusePolicy;
62 import org.opendaylight.yangtools.yang.data.util.ContainerNodeDataWithSchema;
63 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
64 import org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema;
65 import org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema;
66 import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
67 import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
68 import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
69 import org.opendaylight.yangtools.yang.data.util.MountPointData;
70 import org.opendaylight.yangtools.yang.data.util.MultipleEntryDataWithSchema;
71 import org.opendaylight.yangtools.yang.data.util.NotificationAsContainer;
72 import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
73 import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
74 import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
75 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
77 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
78 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
79 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
80 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
81 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
82 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
83 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
84 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
85 import org.opendaylight.yangtools.yang.model.api.Module;
86 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
87 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
88 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
89 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
90 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
91 import org.slf4j.Logger;
92 import org.slf4j.LoggerFactory;
93 import org.w3c.dom.Document;
94 import org.xml.sax.SAXException;
95
96 /**
97  * This class provides functionality for parsing an XML source containing YANG-modeled data. It disallows multiple
98  * instances of the same element except for leaf-list and list entries. It also expects that the YANG-modeled data in
99  * the XML source are wrapped in a root element. This class is NOT thread-safe.
100  *
101  * <p>
102  * Due to backwards compatibility reasons, RFC7952 metadata emitted by this parser may include key QNames with empty URI
103  * (as exposed via {@link #LEGACY_ATTRIBUTE_NAMESPACE}) as their QNameModule. These indicate an unqualified XML
104  * attribute and their value can be assumed to be a String. Furthermore, this extends to qualified attributes, which
105  * uses the proper namespace, but will not bind to a proper module revision -- these need to be reconciled with a
106  * particular SchemaContext and are expected to either be fully decoded, or contain a String value. Handling of such
107  * annotations is at the discretion of the user encountering it: preferred way of handling is to either filter or
108  * normalize them to proper QNames/values when encountered. This caveat will be removed in a future version.
109  */
110 @Beta
111 public final class XmlParserStream implements Closeable, Flushable {
112     /**
113      * {@link QNameModule} for use with legacy XML attributes.
114      * @deprecated The use on this namespace is discouraged and users are strongly encouraged to proper RFC7952 metadata
115      *             annotations.
116      */
117     @Deprecated
118     public static final QNameModule LEGACY_ATTRIBUTE_NAMESPACE = QNameModule.create(XMLNamespace.of("")).intern();
119
120     private static final Logger LOG = LoggerFactory.getLogger(XmlParserStream.class);
121     private static final String XML_STANDARD_VERSION = "1.0";
122     private static final String COM_SUN_TRANSFORMER =
123             "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
124
125     private static final TransformerFactory TRANSFORMER_FACTORY;
126
127     static {
128         TransformerFactory fa = TransformerFactory.newInstance();
129         if (!fa.getFeature(StAXSource.FEATURE)) {
130             LOG.warn("Platform-default TransformerFactory {} does not support StAXSource, attempting fallback to {}",
131                     fa, COM_SUN_TRANSFORMER);
132             fa = TransformerFactory.newInstance(COM_SUN_TRANSFORMER, null);
133             if (!fa.getFeature(StAXSource.FEATURE)) {
134                 throw new TransformerFactoryConfigurationError("No TransformerFactory supporting StAXResult found.");
135             }
136         }
137
138         TRANSFORMER_FACTORY = fa;
139     }
140
141     // Cache of nsUri Strings to QNameModules, as resolved in context
142     private final Map<String, Optional<QNameModule>> resolvedNamespaces = new HashMap<>();
143     // Cache of nsUri Strings to QNameModules, as inferred from document
144     private final Map<String, QNameModule> rawNamespaces = new HashMap<>();
145     private final NormalizedNodeStreamWriter writer;
146     private final SchemaInferenceStack stack;
147     private final XmlCodecFactory codecs;
148     private final DataSchemaNode parentNode;
149     private final boolean strictParsing;
150
151     private XmlParserStream(final NormalizedNodeStreamWriter writer, final XmlCodecFactory codecs,
152             final SchemaInferenceStack stack, final boolean strictParsing) {
153         this.writer = requireNonNull(writer);
154         this.codecs = requireNonNull(codecs);
155         this.stack = requireNonNull(stack);
156         this.strictParsing = strictParsing;
157
158         if (!stack.isEmpty()) {
159             final var stmt = stack.currentStatement();
160             if (stmt instanceof DataSchemaNode data) {
161                 parentNode = data;
162             } else if (stmt instanceof OperationDefinition oper) {
163                 parentNode = OperationAsContainer.of(oper);
164             } else if (stmt instanceof NotificationDefinition notif) {
165                 parentNode = NotificationAsContainer.of(notif);
166             } else {
167                 throw new IllegalArgumentException("Illegal parent node " + stmt);
168             }
169         } else {
170             parentNode = stack.getEffectiveModelContext();
171         }
172     }
173
174     /**
175      * Construct a new {@link XmlParserStream} with strict parsing mode switched on.
176      *
177      * @param writer Output writer
178      * @param codecs Shared codecs
179      * @param parentNode Parent root node
180      * @return A new stream instance
181      */
182     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final XmlCodecFactory codecs,
183             final EffectiveStatementInference parentNode) {
184         return create(writer, codecs, parentNode, true);
185     }
186
187     /**
188      * Construct a new {@link XmlParserStream}.
189      *
190      * @param writer Output writer
191      * @param codecs Shared codecs
192      * @param parentNode Parent root node
193      * @param strictParsing parsing mode
194      *            if set to true, the parser will throw an exception if it encounters unknown child nodes
195      *            (nodes, that are not defined in the provided SchemaContext) in containers and lists
196      *            if set to false, the parser will skip unknown child nodes
197      * @return A new stream instance
198      */
199     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final XmlCodecFactory codecs,
200             final EffectiveStatementInference parentNode, final boolean strictParsing) {
201         return new XmlParserStream(writer, codecs, SchemaInferenceStack.ofInference(parentNode), strictParsing);
202     }
203
204     /**
205      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
206      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, EffectiveStatementInference)}
207      * instead and maintain a {@link XmlCodecFactory} to match the current {@link EffectiveModelContext}.
208      */
209     public static XmlParserStream create(final NormalizedNodeStreamWriter writer,
210             final EffectiveModelContext context) {
211         return create(writer, context, true);
212     }
213
214     /**
215      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
216      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, EffectiveStatementInference)}
217      * instead and maintain a {@link XmlCodecFactory} to match the current {@link EffectiveModelContext}.
218      */
219     public static XmlParserStream create(final NormalizedNodeStreamWriter writer,
220             final EffectiveModelContext context, final boolean strictParsing) {
221         return create(writer, SchemaInferenceStack.of(context).toInference(), strictParsing);
222     }
223
224     /**
225      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
226      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, EffectiveStatementInference)}
227      * instead and maintain a {@link XmlCodecFactory} to match the current {@link EffectiveModelContext}.
228      */
229     public static XmlParserStream create(final NormalizedNodeStreamWriter writer,
230             final EffectiveStatementInference parentNode) {
231         return create(writer, parentNode, true);
232     }
233
234     /**
235      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
236      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, EffectiveStatementInference)}
237      * instead and maintain a {@link XmlCodecFactory} to match the current {@link EffectiveModelContext}.
238      */
239     public static XmlParserStream create(final NormalizedNodeStreamWriter writer,
240             final EffectiveStatementInference parentNode, final boolean strictParsing) {
241         return create(writer, XmlCodecFactory.create(parentNode.getEffectiveModelContext()), parentNode, strictParsing);
242     }
243
244     /**
245      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
246      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, EffectiveStatementInference)}
247      * instead and maintain a {@link XmlCodecFactory} to match the current {@link MountPointContext}.
248      */
249     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx) {
250         return create(writer, mountCtx, SchemaInferenceStack.of(mountCtx.getEffectiveModelContext()).toInference(),
251             true);
252     }
253
254     /**
255      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
256      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, EffectiveStatementInference)}
257      * instead and maintain a {@link XmlCodecFactory} to match the current {@link MountPointContext}.
258      */
259     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx,
260             final EffectiveStatementInference parentNode) {
261         return create(writer, mountCtx, parentNode, true);
262     }
263
264     @Beta
265     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx,
266             final Absolute parentNode) {
267         return create(writer, mountCtx, parentNode, true);
268     }
269
270     @Beta
271     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx,
272             final Absolute parentNode, final boolean strictParsing) {
273         return create(writer, XmlCodecFactory.create(mountCtx), parentNode, strictParsing);
274     }
275
276     @Beta
277     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final XmlCodecFactory codecs,
278             final Absolute parentNode) {
279         return create(writer, codecs, parentNode, true);
280     }
281
282     @Beta
283     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final XmlCodecFactory codecs,
284             final Absolute parentNode, final boolean strictParsing) {
285         return new XmlParserStream(writer, codecs,
286             SchemaInferenceStack.of(codecs.getEffectiveModelContext(), parentNode), strictParsing);
287     }
288
289     @Beta
290     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx,
291             final YangInstanceIdentifier parentNode) {
292         return create(writer, mountCtx, parentNode, true);
293     }
294
295     @Beta
296     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx,
297             final YangInstanceIdentifier parentNode, final boolean strictParsing) {
298         final var init = DataSchemaContextTree.from(mountCtx.getEffectiveModelContext())
299             .enterPath(parentNode)
300             .orElseThrow();
301         return new XmlParserStream(writer, XmlCodecFactory.create(mountCtx), init.stack(), strictParsing);
302     }
303
304     /**
305      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
306      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, EffectiveStatementInference)}
307      * instead and maintain a {@link XmlCodecFactory} to match the current {@link MountPointContext}.
308      */
309     public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final MountPointContext mountCtx,
310             final EffectiveStatementInference parentNode, final boolean strictParsing) {
311         return create(writer, XmlCodecFactory.create(mountCtx), parentNode, strictParsing);
312     }
313
314     /**
315      * This method parses the XML source and emits node events into a NormalizedNodeStreamWriter based on the
316      * YANG-modeled data contained in the XML source.
317      *
318      * @param reader
319      *              StAX reader which is to used to walk through the XML source
320      * @return
321      *              instance of XmlParserStream
322      * @throws XMLStreamException
323      *              if a well-formedness error or an unexpected processing condition occurs while parsing the XML
324      * @throws IOException
325      *              if an error occurs while parsing the value of an anyxml node
326      */
327     public XmlParserStream parse(final XMLStreamReader reader) throws XMLStreamException, IOException {
328         if (reader.hasNext()) {
329             reader.nextTag();
330             final AbstractNodeDataWithSchema<?> nodeDataWithSchema;
331             if (parentNode instanceof ContainerLike containerLike) {
332                 nodeDataWithSchema = new ContainerNodeDataWithSchema(containerLike);
333             } else if (parentNode instanceof ListSchemaNode list) {
334                 nodeDataWithSchema = new ListNodeDataWithSchema(list);
335             } else if (parentNode instanceof AnyxmlSchemaNode anyxml) {
336                 nodeDataWithSchema = new AnyXmlNodeDataWithSchema(anyxml);
337             } else if (parentNode instanceof LeafSchemaNode leaf) {
338                 nodeDataWithSchema = new LeafNodeDataWithSchema(leaf);
339             } else if (parentNode instanceof LeafListSchemaNode leafList) {
340                 nodeDataWithSchema = new LeafListNodeDataWithSchema(leafList);
341             } else if (parentNode instanceof AnydataSchemaNode anydata) {
342                 nodeDataWithSchema = new AnydataNodeDataWithSchema(anydata);
343             } else {
344                 throw new IllegalStateException("Unsupported schema node type " + parentNode.getClass() + ".");
345             }
346
347             read(reader, nodeDataWithSchema, reader.getLocalName());
348             nodeDataWithSchema.write(writer);
349         }
350
351         return this;
352     }
353
354     /**
355      * This method traverses a {@link DOMSource} and emits node events into a NormalizedNodeStreamWriter based on the
356      * YANG-modeled data contained in the source.
357      *
358      * @param src
359      *              {@link DOMSource} to be traversed
360      * @return
361      *              instance of XmlParserStream
362      * @throws XMLStreamException
363      *              if a well-formedness error or an unexpected processing condition occurs while parsing the XML
364      * @throws URISyntaxException
365      *              if the namespace URI of an XML element contains a syntax error
366      * @throws IOException
367      *              if an error occurs while parsing the value of an anyxml node
368      * @throws SAXException
369      *              if an error occurs while parsing the value of an anyxml node
370      */
371     @Beta
372     public XmlParserStream traverse(final DOMSource src) throws XMLStreamException, URISyntaxException, IOException,
373             SAXException {
374         return parse(new DOMSourceXMLStreamReader(src));
375     }
376
377     private ImmutableMap<QName, Object> getElementAttributes(final XMLStreamReader in) {
378         checkState(in.isStartElement(), "Attributes can be extracted only from START_ELEMENT.");
379         final Map<QName, Object> attributes = new LinkedHashMap<>();
380
381         for (int attrIndex = 0; attrIndex < in.getAttributeCount(); attrIndex++) {
382             final String attributeNS = in.getAttributeNamespace(attrIndex);
383
384             // Skip namespace definitions
385             if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attributeNS)) {
386                 continue;
387             }
388
389             final String localName = in.getAttributeLocalName(attrIndex);
390             final String attrValue = in.getAttributeValue(attrIndex);
391             if (Strings.isNullOrEmpty(attributeNS)) {
392                 StreamWriterFacade.warnLegacyAttribute(localName);
393                 attributes.put(QName.create(LEGACY_ATTRIBUTE_NAMESPACE, localName), attrValue);
394                 continue;
395             }
396
397             // Cross-relate attribute namespace to the module
398             final Optional<QNameModule> optModule = resolveXmlNamespace(attributeNS);
399             if (optModule.isPresent()) {
400                 final QName qname = QName.create(optModule.orElseThrow(), localName);
401                 final var optAnnotation = AnnotationSchemaNode.find(codecs.getEffectiveModelContext(), qname);
402                 if (optAnnotation.isPresent()) {
403                     final AnnotationSchemaNode schema = optAnnotation.orElseThrow();
404                     final Object value = codecs.codecFor(schema, stack)
405                         .parseValue(in.getNamespaceContext(), attrValue);
406                     attributes.put(schema.getQName(), value);
407                     continue;
408                 }
409
410                 LOG.debug("Annotation for {} not found, using legacy QName", qname);
411             }
412
413             attributes.put(QName.create(rawXmlNamespace(attributeNS), localName), attrValue);
414         }
415
416         return ImmutableMap.copyOf(attributes);
417     }
418
419     private static Document readAnyXmlValue(final XMLStreamReader in) throws XMLStreamException {
420         // Underlying reader might return null when asked for version, however when such reader is plugged into
421         // Stax -> DOM transformer, it fails with NPE due to null version. Use default xml version in such case.
422         final XMLStreamReader inWrapper;
423         if (in.getVersion() == null) {
424             inWrapper = new StreamReaderDelegate(in) {
425                 @Override
426                 public String getVersion() {
427                     final String ver = super.getVersion();
428                     return ver != null ? ver : XML_STANDARD_VERSION;
429                 }
430             };
431         } else {
432             inWrapper = in;
433         }
434
435         final DOMResult result = new DOMResult();
436         try {
437             TRANSFORMER_FACTORY.newTransformer().transform(new StAXSource(inWrapper), result);
438         } catch (final TransformerException e) {
439             throw new XMLStreamException("Unable to read anyxml value", e);
440         }
441         return (Document) result.getNode();
442     }
443
444     private void read(final XMLStreamReader in, final AbstractNodeDataWithSchema<?> parent, final String rootElement)
445             throws XMLStreamException {
446         if (!in.hasNext()) {
447             return;
448         }
449
450         if (parent instanceof LeafNodeDataWithSchema || parent instanceof LeafListEntryNodeDataWithSchema) {
451             parent.setAttributes(getElementAttributes(in));
452             setValue((SimpleNodeDataWithSchema<?>) parent, in.getElementText().trim(), in.getNamespaceContext());
453             if (isNextEndDocument(in)) {
454                 return;
455             }
456
457             if (!isAtElement(in)) {
458                 in.nextTag();
459             }
460             return;
461         }
462
463         if (parent instanceof ListEntryNodeDataWithSchema || parent instanceof ContainerNodeDataWithSchema) {
464             parent.setAttributes(getElementAttributes(in));
465         }
466
467         if (parent instanceof LeafListNodeDataWithSchema || parent instanceof ListNodeDataWithSchema) {
468             final String localName = in.getLocalName();
469             final String namespaceURI = in.getNamespaceURI();
470             // aggregate current and subsequent nodes having same localName and namespace
471             // into set of entries belonging to current parent node
472             while (localName.equals(in.getLocalName()) && namespaceURI.equals(in.getNamespaceURI())) {
473                 read(in, newEntryNode(parent), rootElement);
474                 if (in.getEventType() == XMLStreamConstants.END_DOCUMENT
475                         || in.getEventType() == XMLStreamConstants.END_ELEMENT) {
476                     break;
477                 }
478             }
479             return;
480         }
481
482         if (parent instanceof AnyXmlNodeDataWithSchema anyxml) {
483             setValue(anyxml, readAnyXmlValue(in), in.getNamespaceContext());
484             if (isNextEndDocument(in)) {
485                 return;
486             }
487
488             if (!isAtElement(in)) {
489                 in.nextTag();
490             }
491
492             return;
493         }
494
495         if (parent instanceof AnydataNodeDataWithSchema anydata) {
496             anydata.setObjectModel(DOMSourceAnydata.class);
497             anydata.setAttributes(getElementAttributes(in));
498             setValue(anydata, readAnyXmlValue(in), in.getNamespaceContext());
499             if (isNextEndDocument(in)) {
500                 return;
501             }
502
503             if (!isAtElement(in)) {
504                 in.nextTag();
505             }
506
507             return;
508         }
509
510         switch (in.nextTag()) {
511             case XMLStreamConstants.START_ELEMENT:
512                 // FIXME: 7.0.0: why do we even need this tracker? either document it or remove it.
513                 //               it looks like it is a crude duplicate finder, which should really be handled via
514                 //               ChildReusePolicy.REJECT
515                 final Set<Entry<String, String>> namesakes = new HashSet<>();
516                 while (in.hasNext()) {
517                     final String xmlElementName = in.getLocalName();
518                     final DataSchemaNode parentSchema = parent.getSchema();
519
520                     final String parentSchemaName = parentSchema.getQName().getLocalName();
521                     if (parentSchemaName.equals(xmlElementName)
522                             && in.getEventType() == XMLStreamConstants.END_ELEMENT) {
523                         if (isNextEndDocument(in)) {
524                             break;
525                         }
526
527                         if (!isAtElement(in)) {
528                             in.nextTag();
529                         }
530                         break;
531                     }
532
533                     if (in.isEndElement() && rootElement.equals(xmlElementName)) {
534                         break;
535                     }
536
537                     final String elementNS = in.getNamespaceURI();
538                     final boolean added = namesakes.add(new SimpleImmutableEntry<>(elementNS, xmlElementName));
539
540                     final XMLNamespace nsUri;
541                     try {
542                         nsUri = rawXmlNamespace(elementNS).getNamespace();
543                     } catch (IllegalArgumentException e) {
544                         throw new XMLStreamException("Failed to convert namespace " + xmlElementName, in.getLocation(),
545                             e);
546                     }
547
548                     final Deque<DataSchemaNode> childDataSchemaNodes =
549                             ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, xmlElementName, nsUri);
550                     if (!childDataSchemaNodes.isEmpty()) {
551                         final boolean elementList = isElementList(childDataSchemaNodes);
552                         if (!added && !elementList) {
553                             throw new XMLStreamException(String.format(
554                                 "Duplicate element \"%s\" in namespace \"%s\" with parent \"%s\" in XML input",
555                                 xmlElementName, elementNS, parentSchema), in.getLocation());
556                         }
557
558                         // We have a match, proceed with it
559                         final QName qname = childDataSchemaNodes.peekLast().getQName();
560                         final AbstractNodeDataWithSchema<?> child = ((CompositeNodeDataWithSchema<?>) parent).addChild(
561                             childDataSchemaNodes, elementList ? ChildReusePolicy.REUSE : ChildReusePolicy.NOOP);
562                         stack.enterDataTree(qname);
563                         read(in, child, rootElement);
564                         stack.exit();
565                         continue;
566                     }
567
568                     if (parent instanceof AbstractMountPointDataWithSchema<?> mountParent) {
569                         // Parent can potentially hold a mount point, let's see if there is a label present
570                         final Optional<MountPointSchemaNode> optMount;
571                         if (parentSchema instanceof ContainerSchemaNode container) {
572                             optMount = MountPointSchemaNode.streamAll(container).findFirst();
573                         } else if (parentSchema instanceof ListSchemaNode list) {
574                             optMount = MountPointSchemaNode.streamAll(list).findFirst();
575                         } else {
576                             throw new XMLStreamException("Unhandled mount-aware schema " + parentSchema,
577                                 in.getLocation());
578                         }
579
580                         if (optMount.isPresent()) {
581                             final var label = new MountPointLabel(optMount.orElseThrow().getQName());
582                             LOG.debug("Assuming node {} and namespace {} belongs to mount point {}", xmlElementName,
583                                 nsUri, label);
584
585                             final var optFactory = codecs.mountPointContext().findMountPoint(label);
586                             if (optFactory.isPresent()) {
587                                 final var mountData = mountParent.getMountPointData(label, optFactory.orElseThrow());
588                                 addMountPointChild(mountData, nsUri, xmlElementName,
589                                     new DOMSource(readAnyXmlValue(in).getDocumentElement()));
590                                 continue;
591                             }
592
593                             LOG.debug("Mount point {} not attached", label);
594                         }
595                     }
596
597                     // We have not handled the node -- let's decide what to do about that
598                     if (strictParsing) {
599                         throw new XMLStreamException(String.format(
600                             "Schema for node with name %s and namespace %s does not exist in parent %s", xmlElementName,
601                             elementNS, parentSchema), in.getLocation());
602                     }
603
604                     LOG.debug("Skipping unknown node ns=\"{}\" localName=\"{}\" in parent {}", elementNS,
605                         xmlElementName, parentSchema);
606                     skipUnknownNode(in);
607                 }
608                 break;
609             case XMLStreamConstants.END_ELEMENT:
610                 if (isNextEndDocument(in)) {
611                     break;
612                 }
613
614                 if (!isAtElement(in)) {
615                     in.nextTag();
616                 }
617                 break;
618             default:
619                 break;
620         }
621     }
622
623     // Return true if schema represents a construct which uses multiple sibling elements to represent its content. The
624     // siblings MAY be interleaved as per RFC7950.
625     private static boolean isElementList(final Deque<DataSchemaNode> childDataSchemaNodes) {
626         final DataSchemaNode last = childDataSchemaNodes.getLast();
627         return last instanceof ListSchemaNode || last instanceof LeafListSchemaNode;
628     }
629
630     private static void addMountPointChild(final MountPointData mount, final XMLNamespace namespace,
631             final String localName, final DOMSource source) {
632         final DOMSourceMountPointChild child = new DOMSourceMountPointChild(source);
633         if (YangLibraryConstants.MODULE_NAMESPACE.equals(namespace)) {
634             final var optName = ContainerName.forLocalName(localName);
635             if (optName != null) {
636                 mount.setContainer(optName, child);
637                 return;
638             }
639
640             LOG.warn("Encountered unknown element {} from YANG Library namespace", localName);
641         } else if (SchemaMountConstants.RFC8528_MODULE.getNamespace().equals(namespace)) {
642             mount.setSchemaMounts(child);
643             return;
644         }
645
646         mount.addChild(child);
647     }
648
649     private static boolean isNextEndDocument(final XMLStreamReader in) throws XMLStreamException {
650         return !in.hasNext() || in.next() == XMLStreamConstants.END_DOCUMENT;
651     }
652
653     private static boolean isAtElement(final XMLStreamReader in) {
654         return in.getEventType() == XMLStreamConstants.START_ELEMENT
655                 || in.getEventType() == XMLStreamConstants.END_ELEMENT;
656     }
657
658     private static void skipUnknownNode(final XMLStreamReader in) throws XMLStreamException {
659         // in case when the unknown node and at least one of its descendant nodes have the same name
660         // we cannot properly reach the end just by checking if the current node is an end element and has the same name
661         // as the root unknown element. therefore we ignore the names completely and just track the level of nesting
662         int levelOfNesting = 0;
663         while (in.hasNext()) {
664             // in case there are text characters in an element, we cannot skip them by calling nextTag()
665             // therefore we skip them by calling next(), and then proceed to next element
666             in.next();
667             if (!isAtElement(in)) {
668                 in.nextTag();
669             }
670             if (in.isStartElement()) {
671                 levelOfNesting++;
672             }
673
674             if (in.isEndElement()) {
675                 if (levelOfNesting == 0) {
676                     break;
677                 }
678
679                 levelOfNesting--;
680             }
681         }
682
683         in.nextTag();
684     }
685
686     private void setValue(final SimpleNodeDataWithSchema<?> parent, final Object value,
687             final NamespaceContext nsContext) {
688         final DataSchemaNode schema = parent.getSchema();
689         final Object prev = parent.getValue();
690         checkArgument(prev == null, "Node '%s' has already set its value to '%s'", schema.getQName(), prev);
691         parent.setValue(translateValueByType(value, schema, nsContext));
692     }
693
694     private Object translateValueByType(final Object value, final DataSchemaNode node,
695             final NamespaceContext namespaceCtx) {
696         if (node instanceof AnyxmlSchemaNode) {
697             checkArgument(value instanceof Document);
698             /*
699              * FIXME: Figure out some YANG extension dispatch, which will reuse JSON parsing or XML parsing -
700              *        anyxml is not well-defined in JSON.
701              */
702             return new DOMSource(((Document) value).getDocumentElement());
703         }
704         if (node instanceof AnydataSchemaNode) {
705             checkArgument(value instanceof Document);
706             return new DOMSourceAnydata(new DOMSource(((Document) value).getDocumentElement()));
707         }
708
709         checkArgument(node instanceof TypedDataSchemaNode);
710         checkArgument(value instanceof String);
711         return codecs.codecFor((TypedDataSchemaNode) node, stack).parseValue(namespaceCtx, (String) value);
712     }
713
714     private static AbstractNodeDataWithSchema<?> newEntryNode(final AbstractNodeDataWithSchema<?> parent) {
715         verify(parent instanceof MultipleEntryDataWithSchema, "Unexpected parent %s", parent);
716         return ((MultipleEntryDataWithSchema<?>) parent).newChildEntry();
717     }
718
719     @Override
720     public void close() throws IOException {
721         writer.flush();
722         writer.close();
723     }
724
725     @Override
726     public void flush() throws IOException {
727         writer.flush();
728     }
729
730     private Optional<QNameModule> resolveXmlNamespace(final String xmlNamespace) {
731         return resolvedNamespaces.computeIfAbsent(xmlNamespace, nsUri -> {
732             final Iterator<? extends Module> it = codecs.getEffectiveModelContext().findModules(XMLNamespace.of(nsUri))
733                     .iterator();
734             return it.hasNext() ? Optional.of(it.next().getQNameModule()) : Optional.empty();
735         });
736     }
737
738     private QNameModule rawXmlNamespace(final String xmlNamespace) {
739         return rawNamespaces.computeIfAbsent(xmlNamespace, nsUri -> QNameModule.create(XMLNamespace.of(nsUri)));
740     }
741 }