Move protocol version handling and time parsing into their own methods.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 25 Oct 2013 05:23:28 +0000 (07:23 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:38 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/SoneParser.java

index 98af5b6..9e926d4 100644 (file)
@@ -89,31 +89,9 @@ public class SoneParser {
                Optional<Client> parsedClient = parseClient(originalSone, soneXml);
                Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient()));
 
-               Optional<Integer> protocolVersion = parseProtocolVersion(soneXml);
-               if (protocolVersion.isPresent()) {
-                       if (protocolVersion.get() < 0) {
-                               logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion.get()));
-                               throw new InvalidProtocolVersion();
-                       }
-                       if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
-                               logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
-                               throw new SoneTooNew();
-                       }
-               }
+               verifyProtocolVersion(soneXml);
 
-               String soneTime = soneXml.getValue("time", null);
-               if (soneTime == null) {
-                       /* TODO - mark Sone as bad. */
-                       logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
-                       throw new MalformedXml();
-               }
-               try {
-                       sone.setTime(Long.parseLong(soneTime));
-               } catch (NumberFormatException nfe1) {
-                       /* TODO - mark Sone as bad. */
-                       logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
-                       throw new MalformedTime();
-               }
+               parseTime(soneXml, sone);
 
                SimpleXML profileXml = soneXml.getNode("profile");
                if (profileXml == null) {
@@ -307,6 +285,36 @@ public class SoneParser {
                return sone;
        }
 
+       private void parseTime(SimpleXML soneXml, Sone sone) {
+               String soneTime = soneXml.getValue("time", null);
+               if (soneTime == null) {
+                       /* TODO - mark Sone as bad. */
+                       logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
+                       throw new MalformedXml();
+               }
+               try {
+                       sone.setTime(Long.parseLong(soneTime));
+               } catch (NumberFormatException nfe1) {
+                       /* TODO - mark Sone as bad. */
+                       logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
+                       throw new MalformedTime();
+               }
+       }
+
+       private void verifyProtocolVersion(SimpleXML soneXml) {
+               Optional<Integer> protocolVersion = parseProtocolVersion(soneXml);
+               if (protocolVersion.isPresent()) {
+                       if (protocolVersion.get() < 0) {
+                               logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion.get()));
+                               throw new InvalidProtocolVersion();
+                       }
+                       if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
+                               logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
+                               throw new SoneTooNew();
+                       }
+               }
+       }
+
        private Optional<Integer> parseProtocolVersion(SimpleXML soneXml) {
                String soneProtocolVersion = soneXml.getValue("protocol-version", null);
                if (soneProtocolVersion == null) {