Special-case schema-mount nodes
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / XMLStreamWriterUtils.java
index ff86e2e7987281c55803ecda471dd29daf012dd4..d684c9678a61f40bb8ec14c92eea346f6a87e3f9 100644 (file)
@@ -17,8 +17,8 @@ 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.TypedDataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
@@ -33,27 +33,28 @@ abstract class XMLStreamWriterUtils {
     private static final Logger LOG = LoggerFactory.getLogger(XMLStreamWriterUtils.class);
 
     /**
-     * Write a value into a XML stream writer. This method assumes the start and end of element is
-     * emitted by the caller.
+     * Encode a value into a String in the context of a XML stream writer. This method assumes the start and end of
+     * element is emitted by the caller.
      *
      * @param writer XML Stream writer
      * @param schemaNode Schema node that describes the value
      * @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
      */
-    void writeValue(final @NonNull ValueWriter writer, final @NonNull SchemaNode schemaNode,
+    String encodeValue(final @NonNull ValueWriter writer, final @NonNull SchemaNode schemaNode,
             final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
-        checkArgument(schemaNode instanceof TypedDataSchemaNode,
-            "Unable to write value for node %s, only nodes of type: leaf and leaf-list can be written at this point",
-            schemaNode.getQName());
+        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 = ((TypedDataSchemaNode) schemaNode).getType();
+        TypeDefinition<?> type = ((TypeAware) schemaNode).getType();
         if (type instanceof LeafrefTypeDefinition) {
             type = getBaseTypeForLeafRef(schemaNode, (LeafrefTypeDefinition) type);
         }
 
-        writeValue(writer, type, value, parent);
+        return encodeValue(writer, type, value, parent);
     }
 
     /**
@@ -64,16 +65,17 @@ abstract class XMLStreamWriterUtils {
      * @param type data type. In case of leaf ref this should be the type of leaf being referenced
      * @param value data value
      * @param parent optional parameter of a module QName owning the leaf definition
+     * @return String characters to be written
      * @throws XMLStreamException if an encoding problem occurs
      */
-    private void writeValue(final @NonNull ValueWriter writer, final @NonNull TypeDefinition<?> type,
+    private String encodeValue(final @NonNull ValueWriter writer, final @NonNull TypeDefinition<?> type,
             final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
         if (type instanceof IdentityrefTypeDefinition) {
-            write(writer, (IdentityrefTypeDefinition) type, value, parent);
+            return encode(writer, (IdentityrefTypeDefinition) type, value, parent);
         } else if (type instanceof InstanceIdentifierTypeDefinition) {
-            write(writer, (InstanceIdentifierTypeDefinition) type, value);
+            return encode(writer, (InstanceIdentifierTypeDefinition) type, value);
         } else {
-            writer.writeCharacters(serialize(type, value));
+            return serialize(type, value);
         }
     }
 
@@ -94,43 +96,42 @@ abstract class XMLStreamWriterUtils {
     }
 
     @VisibleForTesting
-    static void write(final @NonNull ValueWriter writer, final @NonNull IdentityrefTypeDefinition type,
+    static String encode(final @NonNull ValueWriter writer, final @NonNull IdentityrefTypeDefinition type,
             final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
         if (value instanceof QName) {
             final QName qname = (QName) value;
 
             //in case parent is present and same as element namespace write value without namespace
             if (qname.getNamespace().equals(parent.getNamespace())) {
-                writer.writeCharacters(qname.getLocalName());
-            } else {
-                final String ns = qname.getNamespace().toString();
-                final String prefix = "x";
-                writer.writeNamespace(prefix, ns);
-                writer.writeCharacters(prefix + ':' + qname.getLocalName());
+                return qname.getLocalName();
             }
-        } else {
-            final QName qname = type.getQName();
-            LOG.debug("Value of {}:{} is not a QName but {}", qname.getNamespace(), qname.getLocalName(),
-                value.getClass());
-            writer.writeToStringCharacters(value);
+
+            final String ns = qname.getNamespace().toString();
+            final String prefix = "x";
+            writer.writeNamespace(prefix, ns);
+            return prefix + ':' + qname.getLocalName();
         }
+
+        final QName qname = type.getQName();
+        LOG.debug("Value of {}:{} is not a QName but {}", qname.getNamespace(), qname.getLocalName(), value.getClass());
+        return value.toString();
     }
 
-    private void write(final @NonNull ValueWriter writer, final @NonNull InstanceIdentifierTypeDefinition type,
+    private String encode(final @NonNull ValueWriter writer, final @NonNull InstanceIdentifierTypeDefinition type,
             final @NonNull Object value) throws XMLStreamException {
         if (value instanceof YangInstanceIdentifier) {
-            writeInstanceIdentifier(writer, (YangInstanceIdentifier)value);
-        } else {
-            final QName qname = type.getQName();
-            LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", qname.getNamespace(), qname.getLocalName(),
-                value.getClass());
-            writer.writeToStringCharacters(value);
+            return encodeInstanceIdentifier(writer, (YangInstanceIdentifier)value);
         }
+
+        final QName qname = type.getQName();
+        LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", qname.getNamespace(), qname.getLocalName(),
+            value.getClass());
+        return value.toString();
     }
 
     abstract @NonNull TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode,
             @NonNull LeafrefTypeDefinition type);
 
-    abstract void writeInstanceIdentifier(@NonNull ValueWriter writer, YangInstanceIdentifier value)
+    abstract String encodeInstanceIdentifier(@NonNull ValueWriter writer, YangInstanceIdentifier value)
             throws XMLStreamException;
 }