From: David ‘Bombe’ Roden Date: Fri, 25 Oct 2013 05:23:28 +0000 (+0200) Subject: Move protocol version handling and time parsing into their own methods. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5a96a4e4abc272528894bdb70642bf1a5e4e17bd;p=Sone.git Move protocol version handling and time parsing into their own methods. --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 98af5b6..9e926d4 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -89,31 +89,9 @@ public class SoneParser { Optional parsedClient = parseClient(originalSone, soneXml); Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient())); - Optional 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 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 parseProtocolVersion(SimpleXML soneXml) { String soneProtocolVersion = soneXml.getValue("protocol-version", null); if (soneProtocolVersion == null) {