BUG 484 - XmlReader improve readability 37/8037/2
authorJozef Gloncak <jgloncak@cisco.com>
Mon, 16 Jun 2014 11:43:38 +0000 (13:43 +0200)
committerTony Tkacik <ttkacik@cisco.com>
Mon, 23 Jun 2014 09:19:25 +0000 (09:19 +0000)
- XmlReader - improve readibility of methods isSimple(Composite) node by
  extracting common logic to method skipCommentsAndWhitespace.

The change comes from patch set
https://git.opendaylight.org/gerrit/#/c/7337/9
which was originally commited by Tom Pantelis. In this commit is selected
only one change which doesn't relate with BUG 484

Change-Id: Icef852da20e8f624b4bc3d61e334d88f4766474d
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java

index 171805a..1d6ca15 100644 (file)
@@ -14,14 +14,13 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.util.Stack;
-
 import javax.xml.stream.XMLEventReader;
 import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.events.Characters;
 import javax.xml.stream.events.StartElement;
 import javax.xml.stream.events.XMLEvent;
-
 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
 import org.opendaylight.controller.sal.restconf.impl.EmptyNodeWrapper;
 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
@@ -131,17 +130,9 @@ public class XmlReader {
     private boolean isSimpleNodeEvent(final XMLEvent event) throws XMLStreamException {
         checkArgument(event != null, "XML Event cannot be NULL!");
         if (event.isStartElement()) {
-            if (eventReader.hasNext()) {
-                final XMLEvent innerEvent;
-                innerEvent = eventReader.peek();
-                if (innerEvent.isCharacters()) {
-                    final Characters chars = innerEvent.asCharacters();
-                    if (!chars.isWhiteSpace()) {
-                        return true;
-                    }
-                } else if (innerEvent.isEndElement()) {
-                    return true;
-                }
+            XMLEvent innerEvent = skipCommentsAndWhitespace();
+            if ( innerEvent != null && (innerEvent.isCharacters() || innerEvent.isEndElement())) {
+                return true;
             }
         }
         return false;
@@ -150,16 +141,8 @@ public class XmlReader {
     private boolean isCompositeNodeEvent(final XMLEvent event) throws XMLStreamException {
         checkArgument(event != null, "XML Event cannot be NULL!");
         if (event.isStartElement()) {
-            if (eventReader.hasNext()) {
-                XMLEvent innerEvent;
-                innerEvent = eventReader.peek();
-                if (innerEvent.isCharacters()) {
-                    Characters chars = innerEvent.asCharacters();
-                    if (chars.isWhiteSpace()) {
-                        eventReader.nextEvent();
-                        innerEvent = eventReader.peek();
-                    }
-                }
+            XMLEvent innerEvent = skipCommentsAndWhitespace();
+            if( innerEvent != null ) {
                 if (innerEvent.isStartElement()) {
                     return true;
                 }
@@ -168,6 +151,26 @@ public class XmlReader {
         return false;
     }
 
+    private XMLEvent skipCommentsAndWhitespace() throws XMLStreamException {
+        while( eventReader.hasNext() ) {
+            XMLEvent event = eventReader.peek();
+            if( event.getEventType() == XMLStreamConstants.COMMENT ) {
+                eventReader.nextEvent();
+                continue;
+            }
+
+            if( event.isCharacters() ) {
+                Characters chars = event.asCharacters();
+                if( chars.isWhiteSpace() ) {
+                    eventReader.nextEvent();
+                    continue;
+                }
+            }
+            return event;
+        }
+        return null;
+    }
+
     private CompositeNodeWrapper resolveCompositeNodeFromStartElement(final StartElement startElement) {
         checkArgument(startElement != null, "Start Element cannot be NULL!");
         return new CompositeNodeWrapper(getNamespaceFor(startElement), getLocalNameFor(startElement));

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.