From 5a96a4e4abc272528894bdb70642bf1a5e4e17bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 25 Oct 2013 07:23:28 +0200 Subject: [PATCH] Move protocol version handling and time parsing into their own methods. --- .../net/pterodactylus/sone/core/SoneParser.java | 56 ++++++++++++---------- 1 file changed, 32 insertions(+), 24 deletions(-) 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) { -- 2.7.4