Merge "BUG-1486: Pick Javassist version from odlparent"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlStreamUtils.java
index 14b5c8593f5d85b68533d95bd496929653a03cd6..4527e75ef9d1291cb3811bf9e8910ca4e62b0aef 100644 (file)
@@ -5,6 +5,7 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 
 import java.net.URI;
+import java.util.Map;
 import java.util.Map.Entry;
 
 import javax.annotation.Nonnull;
@@ -144,7 +145,14 @@ public class XmlStreamUtils {
         final String ns = qname.getNamespace() != null ? qname.getNamespace().toString() : "";
 
         if (isEmptyElement(data)) {
-            writer.writeEmptyElement(pfx, qname.getLocalName(), ns);
+            if (hasAttributes(data)) {
+                writer.writeStartElement(pfx, qname.getLocalName(), ns);
+                RandomPrefix randomPrefix = new RandomPrefix();
+                writeAttributes(writer, (AttributesContainer) data, randomPrefix);
+                writer.writeEndElement();
+            } else {
+                writer.writeEmptyElement(pfx, qname.getLocalName(), ns);
+            }
             return;
         }
 
@@ -163,11 +171,9 @@ public class XmlStreamUtils {
      * @throws XMLStreamException if an encoding problem occurs
      */
     public void writeValue(final XMLStreamWriter writer, final @Nonnull Node<?> data, final SchemaNode schema) throws XMLStreamException {
-        if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
+        if (hasAttributes(data)) {
             RandomPrefix randomPrefix = new RandomPrefix();
-            for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
-                writeAttribute(writer, attribute, randomPrefix);
-            }
+            writeAttributes(writer, (AttributesContainer) data, randomPrefix);
         }
 
         if (data instanceof SimpleNode<?>) {
@@ -200,6 +206,21 @@ public class XmlStreamUtils {
         }
     }
 
+    private static void writeAttributes(final XMLStreamWriter writer, final AttributesContainer data, final RandomPrefix randomPrefix) throws XMLStreamException {
+        for (Entry<QName, String> attribute : data.getAttributes().entrySet()) {
+            writeAttribute(writer, attribute, randomPrefix);
+        }
+    }
+
+    private static boolean hasAttributes(final Node<?> data) {
+        if (data instanceof AttributesContainer) {
+            final Map<QName, String> c = ((AttributesContainer) data).getAttributes();
+            return c == null || c.isEmpty();
+        } else {
+            return false;
+        }
+    }
+
     @VisibleForTesting
     static void writeAttribute(final XMLStreamWriter writer, final Entry<QName, String> attribute, final RandomPrefix randomPrefix)
             throws XMLStreamException {
@@ -269,7 +290,7 @@ public class XmlStreamUtils {
         if (value instanceof YangInstanceIdentifier) {
             write(writer, (YangInstanceIdentifier)value);
         } else {
-            LOG.debug("Value of {}:{} is not an InstanceIdentifier but {}", type.getQName().getNamespace(), type.getQName().getLocalName(), value.getClass());
+            LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", type.getQName().getNamespace(), type.getQName().getLocalName(), value.getClass());
             writer.writeCharacters(String.valueOf(value));
         }
     }