Propagate type to XMLStreamWriterUtils 73/90273/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Jun 2020 10:14:59 +0000 (12:14 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Jun 2020 12:23:46 +0000 (14:23 +0200)
We have a useless checkArgument here, which is impossible to hit
if we structure this just right.

JIRA: YANGTOOLS-1108
Change-Id: I589da629526026fa09c0dda9c11f0bd5516941a0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 22384c114cd763c21096f8d673c7ca7dae24b549)

yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemaAwareXMLStreamNormalizedNodeStreamWriter.java
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java

index 9c60bb759c2a039034a7923ae0f5f9ce6e33b443..c74a80e2fcb539ca9ee83959e608a11e4f359399 100644 (file)
@@ -35,8 +35,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
 
-final class SchemaAwareXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<SchemaNode>
-        implements SchemaContextProvider {
+final class SchemaAwareXMLStreamNormalizedNodeStreamWriter
+        extends XMLStreamNormalizedNodeStreamWriter<TypedDataSchemaNode> implements SchemaContextProvider {
     private final SchemaTracker tracker;
     private final SchemaAwareXMLStreamWriterUtils streamUtils;
 
@@ -48,9 +48,10 @@ final class SchemaAwareXMLStreamNormalizedNodeStreamWriter extends XMLStreamNorm
     }
 
     @Override
-    String encodeValue(final ValueWriter xmlWriter, final Object value, final SchemaNode schemaNode)
+    String encodeValue(final ValueWriter xmlWriter, final Object value, final TypedDataSchemaNode schemaNode)
             throws XMLStreamException {
-        return streamUtils.encodeValue(xmlWriter, schemaNode, value, schemaNode.getQName().getModule());
+        return streamUtils.encodeValue(xmlWriter, schemaNode, schemaNode.getType(), value,
+            schemaNode.getQName().getModule());
     }
 
     @Override
@@ -59,7 +60,8 @@ final class SchemaAwareXMLStreamNormalizedNodeStreamWriter extends XMLStreamNorm
         final Optional<AnnotationSchemaNode> optAnnotation = AnnotationSchemaNode.find(streamUtils.getSchemaContext(),
             qname);
         if (optAnnotation.isPresent()) {
-            return streamUtils.encodeValue(xmlWriter, optAnnotation.get(), value, qname.getModule());
+            final AnnotationSchemaNode schema = optAnnotation.get();
+            return streamUtils.encodeValue(xmlWriter, schema, schema.getType(), value, qname.getModule());
         }
 
         checkArgument(!qname.getRevision().isPresent(), "Failed to find bound annotation %s", qname);
@@ -156,7 +158,7 @@ final class SchemaAwareXMLStreamNormalizedNodeStreamWriter extends XMLStreamNorm
     public void scalarValue(final Object value) throws IOException {
         final Object current = tracker.getParent();
         if (current instanceof TypedDataSchemaNode) {
-            writeValue(value, (SchemaNode) current);
+            writeValue(value, (TypedDataSchemaNode) current);
         } else if (current instanceof AnydataSchemaNode) {
             anydataValue(value);
         } else {
index d684c9678a61f40bb8ec14c92eea346f6a87e3f9..05b0ab3ced1e2173323b07f151b48fc7571cb185 100644 (file)
@@ -7,8 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.data.codec.xml;
 
-import static com.google.common.base.Preconditions.checkArgument;
-
 import com.google.common.annotations.VisibleForTesting;
 import javax.xml.stream.XMLStreamException;
 import org.eclipse.jdt.annotation.NonNull;
@@ -17,7 +15,6 @@ import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.TypeAware;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
@@ -38,23 +35,18 @@ abstract class XMLStreamWriterUtils {
      *
      * @param writer XML Stream writer
      * @param schemaNode Schema node that describes the value
+     * @param type Schema type definition
      * @param value data value
      * @param parent module QName owning the leaf definition
      * @return String characters to be written
      * @throws XMLStreamException if an encoding problem occurs
      */
-    String encodeValue(final @NonNull ValueWriter writer, final @NonNull SchemaNode schemaNode,
-            final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
-        checkArgument(schemaNode instanceof TypeAware,
-            "Unable to write value for node %s, only nodes of type: leaf, leaf-list and annotations can be written "
-                    + "at this point", schemaNode.getQName());
-
-        TypeDefinition<?> type = ((TypeAware) schemaNode).getType();
-        if (type instanceof LeafrefTypeDefinition) {
-            type = getBaseTypeForLeafRef(schemaNode, (LeafrefTypeDefinition) type);
-        }
-
-        return encodeValue(writer, type, value, parent);
+    String encodeValue(final @NonNull ValueWriter writer,final @NonNull SchemaNode schemaNode,
+            final TypeDefinition<?> type, final @NonNull Object value, final QNameModule parent)
+                    throws XMLStreamException {
+        return type instanceof LeafrefTypeDefinition
+                ? encodeValue(writer, getBaseTypeForLeafRef(schemaNode, (LeafrefTypeDefinition) type), value, parent)
+                        : encodeValue(writer, type, value, parent);
     }
 
     /**