From: Ed Warnicke Date: Tue, 26 Nov 2013 14:04:46 +0000 (+0000) Subject: Merge "Null data from XML" X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~344 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=f20add00faf71465ad092144689450c105f9bde3;hp=3979e330c9f95a898c54a9234f3a07e3b2ae4349 Merge "Null data from XML" --- diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java index 9f31eb46d5..bf7ff7d435 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java @@ -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> 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); } - + } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java index 6249d2a5b0..ef122dd8d7 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java @@ -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); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang index 7c17bf9fdf..b038eb193c 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang @@ -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; } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml index 0c60fbcff3..ce97dd1715 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml @@ -1,5 +1,7 @@ str0 + + 121 131 str1