X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParser.java;h=9e926d4e28f056f35798b5f89fa25fde2008b1ca;hb=5a96a4e4abc272528894bdb70642bf1a5e4e17bd;hp=5a7229d86a93acce2c98aa5f875cf8ea0ee8b588;hpb=bdbcedc1e523e51a5e41b46b1856746b4b0e069a;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 5a7229d..9e926d4 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -85,42 +85,15 @@ public class SoneParser { throw new InvalidXml(); } - Optional soneXml = parseXml(document); - if (!soneXml.isPresent()) { - logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone.getId())); - throw new InvalidXml(); - } - - Optional parsedClient = parseClient(originalSone, soneXml.get()); + SimpleXML soneXml = SimpleXML.fromDocument(document); + Optional parsedClient = parseClient(originalSone, soneXml); Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient())); - Optional protocolVersion = parseProtocolVersion(soneXml.get()); - 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 InvalidProtocolVersion(); - } - } + verifyProtocolVersion(soneXml); - String soneTime = soneXml.get().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.get().getNode("profile"); + SimpleXML profileXml = soneXml.getNode("profile"); if (profileXml == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone)); @@ -159,7 +132,7 @@ public class SoneParser { } /* parse posts. */ - SimpleXML postsXml = soneXml.get().getNode("posts"); + SimpleXML postsXml = soneXml.getNode("posts"); Set posts = new HashSet(); if (postsXml == null) { /* TODO - mark Sone as bad. */ @@ -192,7 +165,7 @@ public class SoneParser { } /* parse replies. */ - SimpleXML repliesXml = soneXml.get().getNode("replies"); + SimpleXML repliesXml = soneXml.getNode("replies"); Set replies = new HashSet(); if (repliesXml == null) { /* TODO - mark Sone as bad. */ @@ -221,7 +194,7 @@ public class SoneParser { } /* parse liked post IDs. */ - SimpleXML likePostIdsXml = soneXml.get().getNode("post-likes"); + SimpleXML likePostIdsXml = soneXml.getNode("post-likes"); Set likedPostIds = new HashSet(); if (likePostIdsXml == null) { /* TODO - mark Sone as bad. */ @@ -234,7 +207,7 @@ public class SoneParser { } /* parse liked reply IDs. */ - SimpleXML likeReplyIdsXml = soneXml.get().getNode("reply-likes"); + SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes"); Set likedReplyIds = new HashSet(); if (likeReplyIdsXml == null) { /* TODO - mark Sone as bad. */ @@ -247,7 +220,7 @@ public class SoneParser { } /* parse albums. */ - SimpleXML albumsXml = soneXml.get().getNode("albums"); + SimpleXML albumsXml = soneXml.getNode("albums"); Map albums = Maps.newHashMap(); if (albumsXml != null) { for (SimpleXML albumXml : albumsXml.getNodes("album")) { @@ -312,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) { @@ -321,15 +324,6 @@ public class SoneParser { return fromNullable(Ints.tryParse(soneProtocolVersion)); } - private Optional parseXml(Document document) { - try { - return fromNullable(SimpleXML.fromDocument(document)); - } catch (NullPointerException npe1) { - /* for some reason, invalid XML can cause NPEs. */ - return absent(); - } - } - private Optional parseClient(Sone sone, SimpleXML soneXml) { SimpleXML clientXml = soneXml.getNode("client"); if (clientXml == null) { @@ -352,11 +346,15 @@ public class SoneParser { } + public static class SoneTooNew extends RuntimeException { + + } + public static class MalformedXml extends RuntimeException { } - public static class DuplicateField extends RuntimeException { + public static class DuplicateField extends RuntimeException { }