From 47ced1c22f0604a5b57c79a4d5560e15adfddc0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 19 Oct 2013 20:43:12 +0200 Subject: [PATCH] Move parsing of protocol version into its own method. --- .../net/pterodactylus/sone/core/SoneParser.java | 39 +++++++++++----------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 8d4883d..7640ebd 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -48,6 +48,7 @@ import net.pterodactylus.util.xml.XML; import com.google.common.base.Optional; import com.google.common.collect.Maps; +import com.google.common.primitives.Ints; import org.w3c.dom.Document; /** @@ -100,25 +101,16 @@ public class SoneParser { Optional parsedClient = parseClient(originalSone, soneXml.get()); Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient())); - Integer protocolVersion = null; - String soneProtocolVersion = soneXml.get().getValue("protocol-version", null); - if (soneProtocolVersion != null) { - protocolVersion = Numbers.safeParseInteger(soneProtocolVersion); - } - if (protocolVersion == null) { - logger.log(Level.INFO, "No protocol version found, assuming 0."); - protocolVersion = 0; - } - - if (protocolVersion < 0) { - logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion)); - return null; - } - - /* check for valid versions. */ - if (protocolVersion > MAX_PROTOCOL_VERSION) { - logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion)); - return null; + Optional protocolVersion = parseProtocolVersion(originalSone, soneXml.get()); + if (protocolVersion.isPresent()) { + if (protocolVersion.get() < 0) { + logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion)); + return null; + } + if (protocolVersion.get() > MAX_PROTOCOL_VERSION) { + logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion)); + return null; + } } String soneTime = soneXml.get().getValue("time", null); @@ -329,6 +321,15 @@ public class SoneParser { return sone; } + private Optional parseProtocolVersion(Sone originalSone, SimpleXML soneXml) { + String soneProtocolVersion = soneXml.getValue("protocol-version", null); + if (soneProtocolVersion == null) { + logger.log(Level.INFO, "No protocol version found, assuming 0."); + return absent(); + } + return fromNullable(Ints.tryParse(soneProtocolVersion)); + } + private Optional parseXml(Sone originalSone, Document document) { try { return fromNullable(SimpleXML.fromDocument(document)); -- 2.7.4