Merge "Null data from XML"
authorEd Warnicke <eaw@cisco.com>
Tue, 26 Nov 2013 14:04:46 +0000 (14:04 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 26 Nov 2013 14:04:46 +0000 (14:04 +0000)
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java
opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang
opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml

index 9f31eb46d52e9a722bffdc7d742859adba09cfc6..bf7ff7d43524732f067982042fb46e19f3d35134 100644 (file)
@@ -18,13 +18,13 @@ import org.opendaylight.controller.sal.restconf.impl.NodeWrapper;
 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
 
 public class XmlReader {
-    
+
     private final static XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
     private XMLEventReader eventReader;
 
     public CompositeNodeWrapper read(InputStream entityStream) throws XMLStreamException, UnsupportedFormatException {
         eventReader = xmlInputFactory.createXMLEventReader(entityStream);
-        
+
         if (eventReader.hasNext()) {
             XMLEvent element = eventReader.peek();
             if (element.isStartDocument()) {
@@ -35,7 +35,7 @@ public class XmlReader {
         if (eventReader.hasNext() && !isCompositeNodeEvent(eventReader.peek())) {
             throw new UnsupportedFormatException("Root element of XML has to be composite element.");
         }
-        
+
         final Stack<NodeWrapper<?>> processingQueue = new Stack<>();
         CompositeNodeWrapper root = null;
         NodeWrapper<?> element = null;
@@ -73,14 +73,14 @@ public class XmlReader {
                 element = processingQueue.pop();
             }
         }
-        
+
         if (!root.getLocalName().equals(element.getLocalName())) {
             throw new UnsupportedFormatException("XML should contain only one root element");
         }
-        
+
         return root;
     }
-    
+
     private boolean isSimpleNodeEvent(final XMLEvent event) throws XMLStreamException {
         checkArgument(event != null, "XML Event cannot be NULL!");
         if (event.isStartElement()) {
@@ -99,7 +99,7 @@ public class XmlReader {
         }
         return false;
     }
-    
+
     private boolean isCompositeNodeEvent(final XMLEvent event) throws XMLStreamException {
         checkArgument(event != null, "XML Event cannot be NULL!");
         if (event.isStartElement()) {
@@ -120,8 +120,9 @@ public class XmlReader {
         }
         return false;
     }
-    
-    private SimpleNodeWrapper resolveSimpleNodeFromStartElement(final StartElement startElement) throws XMLStreamException {
+
+    private SimpleNodeWrapper resolveSimpleNodeFromStartElement(final StartElement startElement)
+            throws XMLStreamException {
         checkArgument(startElement != null, "Start Element cannot be NULL!");
         String data = null;
 
@@ -133,25 +134,29 @@ public class XmlReader {
                     data = innerEvent.asCharacters().getData();
                 }
             } else if (innerEvent.isEndElement()) {
-                data = "";
+                if (startElement.getLocation().getCharacterOffset() == innerEvent.getLocation().getCharacterOffset()) {
+                    data = null;
+                } else {
+                    data = "";
+                }
             }
         }
-        
+
         return new SimpleNodeWrapper(getNamespaceFrom(startElement), getLocalNameFrom(startElement), data);
     }
-    
+
     private CompositeNodeWrapper resolveCompositeNodeFromStartElement(final StartElement startElement) {
         checkArgument(startElement != null, "Start Element cannot be NULL!");
         return new CompositeNodeWrapper(getNamespaceFrom(startElement), getLocalNameFrom(startElement));
     }
-    
+
     private String getLocalNameFrom(StartElement startElement) {
         return startElement.getName().getLocalPart();
     }
-    
+
     private URI getNamespaceFrom(StartElement startElement) {
         String namespaceURI = startElement.getName().getNamespaceURI();
         return namespaceURI.isEmpty() ? null : URI.create(namespaceURI);
     }
-    
+
 }
index 6249d2a5b0d3a475fa6435714a24fc57d901a554..ef122dd8d7f68cba55088831d9ebe639c869b646 100644 (file)
@@ -11,7 +11,7 @@ import javax.ws.rs.WebApplicationException;
 
 import org.junit.*;
 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
-import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
+import org.opendaylight.controller.sal.restconf.impl.*;
 import org.opendaylight.yangtools.yang.data.api.*;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.slf4j.*;
@@ -41,9 +41,36 @@ public class FromXmlToCompositeNodeTest {
         String nameSpace = "data:container:yang";
         assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
 
+        verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
         verifyCommonPartAOfXml(compNode, "", nameSpace);
     }
 
+    private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
+        assertEquals("cont", compNode.getNodeType().getLocalName());
+
+        SimpleNode<?> lf2 = null;
+        SimpleNode<?> lf3 = null;
+        int found = 0;
+        for (Node<?> child : compNode.getChildren()) {
+            if (found == 0x3)
+                break;
+            if (child instanceof SimpleNode<?>) {
+                SimpleNode<?> childSimple = (SimpleNode<?>) child;
+                if (childSimple.getNodeType().getLocalName().equals("lf3")) {
+                    lf3 = childSimple;
+                    found = found | (1 << 0);
+                } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
+                    lf2 = childSimple;
+                    found = found | (1 << 1);
+                }
+            }
+            assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
+        }
+
+        assertEquals("", lf2.getValue());
+        assertEquals(null, lf3.getValue());
+    }
+
     @Test
     public void testXmlDataList() {
         CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml", false);
index 7c17bf9fdf3b240290595fecb734c33db87b7552..b038eb193ccbc882c2e0c218a27e4531a75e2342 100644 (file)
@@ -2,13 +2,22 @@ module data-container-yang {
   namespace "data:container:yang";  
 
   prefix "dtconyg";
-  revision 2013-11-19 {    
+       revision 2013-11-19 {    
   }
   
   container cont {
        leaf lf1 {
                type string;
        }
+       
+       leaf lf2 {
+               type string;
+       }
+       
+       leaf lf3 {
+               type empty;
+       }
+       
        leaf-list lflst1 {
                type string;
        }
index 0c60fbcff3903fb64ab3fc8165758948f546d107..ce97dd1715c51e8f96b54ca90da5a4d9f6d88ef7 100644 (file)
@@ -1,5 +1,7 @@
 <cont>
        <lf1>str0</lf1>
+       <lf2></lf2>
+       <lf3/>
        <lflst1>121</lflst1>
        <lflst1>131</lflst1>
        <lflst1>str1</lflst1>