Fix missing location in error reports
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlParserStream.java
index 53952b16943a58ba3ca3889adf1726da13bd83eb..5c3d7e3735b2fb0b64c919fb094d6855c002f20f 100644 (file)
@@ -59,6 +59,7 @@ import org.opendaylight.yangtools.yang.data.util.AbstractNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.AnydataNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema.ChildReusePolicy;
 import org.opendaylight.yangtools.yang.data.util.ContainerNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema;
@@ -66,20 +67,22 @@ import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.MountPointData;
+import org.opendaylight.yangtools.yang.data.util.MultipleEntryDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
 import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
 import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.YangModeledAnyXmlNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerLike;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
 import org.slf4j.Logger;
@@ -190,20 +193,20 @@ public final class XmlParserStream implements Closeable, Flushable {
     /**
      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, SchemaNode)} instead and
-     * maintain a {@link XmlCodecFactory} to match the current {@link SchemaContext}.
+     * maintain a {@link XmlCodecFactory} to match the current {@link EffectiveModelContext}.
      */
-    public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
-            final SchemaNode parentNode) {
+    public static XmlParserStream create(final NormalizedNodeStreamWriter writer,
+            final EffectiveModelContext schemaContext, final SchemaNode parentNode) {
         return create(writer, schemaContext, parentNode, true);
     }
 
     /**
      * Utility method for use when caching {@link XmlCodecFactory} is not feasible. Users with high performance
      * requirements should use {@link #create(NormalizedNodeStreamWriter, XmlCodecFactory, SchemaNode)} instead and
-     * maintain a {@link XmlCodecFactory} to match the current {@link SchemaContext}.
+     * maintain a {@link XmlCodecFactory} to match the current {@link EffectiveModelContext}.
      */
-    public static XmlParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
-            final SchemaNode parentNode, final boolean strictParsing) {
+    public static XmlParserStream create(final NormalizedNodeStreamWriter writer,
+            final EffectiveModelContext schemaContext, final SchemaNode parentNode, final boolean strictParsing) {
         return create(writer, XmlCodecFactory.create(schemaContext), parentNode, strictParsing);
     }
 
@@ -249,8 +252,8 @@ public final class XmlParserStream implements Closeable, Flushable {
         if (reader.hasNext()) {
             reader.nextTag();
             final AbstractNodeDataWithSchema<?> nodeDataWithSchema;
-            if (parentNode instanceof ContainerSchemaNode) {
-                nodeDataWithSchema = new ContainerNodeDataWithSchema((ContainerSchemaNode) parentNode);
+            if (parentNode instanceof ContainerLike) {
+                nodeDataWithSchema = new ContainerNodeDataWithSchema((ContainerLike) parentNode);
             } else if (parentNode instanceof ListSchemaNode) {
                 nodeDataWithSchema = new ListNodeDataWithSchema((ListSchemaNode) parentNode);
             } else if (parentNode instanceof YangModeledAnyxmlSchemaNode) {
@@ -322,7 +325,7 @@ public final class XmlParserStream implements Closeable, Flushable {
             if (optModule.isPresent()) {
                 final QName qname = QName.create(optModule.get(), localName);
                 final Optional<AnnotationSchemaNode> optAnnotation = AnnotationSchemaNode.find(
-                    codecs.getSchemaContext(), qname);
+                    codecs.getEffectiveModelContext(), qname);
                 if (optAnnotation.isPresent()) {
                     final AnnotationSchemaNode schema = optAnnotation.get();
                     final Object value = codecs.codecFor(schema).parseValue(in.getNamespaceContext(), attrValue);
@@ -436,7 +439,9 @@ public final class XmlParserStream implements Closeable, Flushable {
 
         switch (in.nextTag()) {
             case XMLStreamConstants.START_ELEMENT:
-                // FIXME: why do we even need this tracker? either document it or remove it
+                // FIXME: 7.0.0: why do we even need this tracker? either document it or remove it.
+                //               it looks like it is a crude duplicate finder, which should really be handled via
+                //               ChildReusePolicy.REJECT
                 final Set<Entry<String, String>> namesakes = new HashSet<>();
                 while (in.hasNext()) {
                     final String xmlElementName = in.getLocalName();
@@ -465,11 +470,7 @@ public final class XmlParserStream implements Closeable, Flushable {
                     }
 
                     final String elementNS = in.getNamespaceURI();
-                    if (!namesakes.add(new SimpleImmutableEntry<>(elementNS, xmlElementName))) {
-                        throw new XMLStreamException(String.format(
-                            "Duplicate namespace \"%s\" element \"%s\" in XML input", elementNS, xmlElementName),
-                            in.getLocation());
-                    }
+                    final boolean added = namesakes.add(new SimpleImmutableEntry<>(elementNS, xmlElementName));
 
                     final URI nsUri;
                     try {
@@ -482,8 +483,16 @@ public final class XmlParserStream implements Closeable, Flushable {
                     final Deque<DataSchemaNode> childDataSchemaNodes =
                             ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, xmlElementName, nsUri);
                     if (!childDataSchemaNodes.isEmpty()) {
+                        final boolean elementList = isElementList(childDataSchemaNodes);
+                        if (!added && !elementList) {
+                            throw new XMLStreamException(String.format(
+                                "Duplicate element \"%s\" in namespace \"%s\" with parent \"%s\" in XML input",
+                                xmlElementName, elementNS, parentSchema), in.getLocation());
+                        }
+
                         // We have a match, proceed with it
-                        read(in, ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes), rootElement);
+                        read(in, ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes,
+                            elementList ? ChildReusePolicy.REUSE : ChildReusePolicy.NOOP), rootElement);
                         continue;
                     }
 
@@ -495,7 +504,8 @@ public final class XmlParserStream implements Closeable, Flushable {
                         } else if (parentSchema instanceof ListSchemaNode) {
                             optMount = MountPointSchemaNode.streamAll((ListSchemaNode) parentSchema).findFirst();
                         } else {
-                            throw new XMLStreamException("Unhandled mount-aware schema " + parentSchema);
+                            throw new XMLStreamException("Unhandled mount-aware schema " + parentSchema,
+                                in.getLocation());
                         }
 
                         if (optMount.isPresent()) {
@@ -521,7 +531,7 @@ public final class XmlParserStream implements Closeable, Flushable {
                     if (strictParsing) {
                         throw new XMLStreamException(String.format(
                             "Schema for node with name %s and namespace %s does not exist at %s", xmlElementName,
-                            elementNS, parentSchema.getPath(), in.getLocation()));
+                            elementNS, parentSchema.getPath()), in.getLocation());
                     }
 
                     LOG.debug("Skipping unknown node ns=\"{}\" localName=\"{}\" at path {}", elementNS, xmlElementName,
@@ -543,6 +553,13 @@ public final class XmlParserStream implements Closeable, Flushable {
         }
     }
 
+    // Return true if schema represents a construct which uses multiple sibling elements to represent its content. The
+    // siblings MAY be interleaved as per RFC7950.
+    private static boolean isElementList(final Deque<DataSchemaNode> childDataSchemaNodes) {
+        final DataSchemaNode last = childDataSchemaNodes.getLast();
+        return last instanceof ListSchemaNode || last instanceof LeafListSchemaNode;
+    }
+
     private static void addMountPointChild(final MountPointData mount, final URI namespace, final String localName,
             final DOMSource source) {
         final DOMSourceMountPointChild child = new DOMSourceMountPointChild(source);
@@ -628,15 +645,8 @@ public final class XmlParserStream implements Closeable, Flushable {
     }
 
     private static AbstractNodeDataWithSchema<?> newEntryNode(final AbstractNodeDataWithSchema<?> parent) {
-        final AbstractNodeDataWithSchema<?> newChild;
-        if (parent instanceof ListNodeDataWithSchema) {
-            newChild = ListEntryNodeDataWithSchema.forSchema(((ListNodeDataWithSchema) parent).getSchema());
-        } else {
-            verify(parent instanceof LeafListNodeDataWithSchema, "Unexpected parent %s", parent);
-            newChild = new LeafListEntryNodeDataWithSchema(((LeafListNodeDataWithSchema) parent).getSchema());
-        }
-        ((CompositeNodeDataWithSchema<?>) parent).addChild(newChild);
-        return newChild;
+        verify(parent instanceof MultipleEntryDataWithSchema, "Unexpected parent %s", parent);
+        return ((MultipleEntryDataWithSchema<?>) parent).newChildEntry();
     }
 
     @Override
@@ -652,7 +662,8 @@ public final class XmlParserStream implements Closeable, Flushable {
 
     private Optional<QNameModule> resolveXmlNamespace(final String xmlNamespace) {
         return resolvedNamespaces.computeIfAbsent(xmlNamespace, nsUri -> {
-            final Iterator<Module> it = codecs.getSchemaContext().findModules(URI.create(nsUri)).iterator();
+            final Iterator<? extends Module> it = codecs.getEffectiveModelContext().findModules(URI.create(nsUri))
+                    .iterator();
             return it.hasNext() ? Optional.of(it.next().getQNameModule()) : Optional.empty();
         });
     }