From fdf3e6fef7205fcf18e0c24d98a0d187b5e80036 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 23 Oct 2010 16:33:58 +0200 Subject: [PATCH] Make parser a bit more tolerant for Sones with missing elements. --- .../pterodactylus/sone/core/SoneDownloader.java | 139 ++++++++++----------- 1 file changed, 67 insertions(+), 72 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 9f76b7e..a611bae 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -223,111 +223,106 @@ public class SoneDownloader extends AbstractService { /* parse posts. */ SimpleXML postsXml = soneXml.getNode("posts"); + Set posts = new HashSet(); if (postsXml == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, "Downloaded Sone %s has no posts!", new Object[] { sone }); - return null; - } - - Set posts = new HashSet(); - for (SimpleXML postXml : postsXml.getNodes("post")) { - String postId = postXml.getValue("id", null); - String postTime = postXml.getValue("time", null); - String postText = postXml.getValue("text", null); - if ((postId == null) || (postTime == null) || (postText == null)) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, "Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", new Object[] { sone, postId, postTime, postText }); - return null; - } - try { - posts.add(core.getPost(postId).setSone(sone).setTime(Long.parseLong(postTime)).setText(postText)); - } catch (NumberFormatException nfe1) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, "Downloaded post for Sone %s with invalid time: %s", new Object[] { sone, postTime }); - return null; + } else { + for (SimpleXML postXml : postsXml.getNodes("post")) { + String postId = postXml.getValue("id", null); + String postTime = postXml.getValue("time", null); + String postText = postXml.getValue("text", null); + if ((postId == null) || (postTime == null) || (postText == null)) { + /* TODO - mark Sone as bad. */ + logger.log(Level.WARNING, "Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", new Object[] { sone, postId, postTime, postText }); + return null; + } + try { + posts.add(core.getPost(postId).setSone(sone).setTime(Long.parseLong(postTime)).setText(postText)); + } catch (NumberFormatException nfe1) { + /* TODO - mark Sone as bad. */ + logger.log(Level.WARNING, "Downloaded post for Sone %s with invalid time: %s", new Object[] { sone, postTime }); + return null; + } } } /* parse replies. */ SimpleXML repliesXml = soneXml.getNode("replies"); + Set replies = new HashSet(); if (repliesXml == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, "Downloaded Sone %s has no replies!", new Object[] { sone }); - return null; - } - - Set replies = new HashSet(); - for (SimpleXML replyXml : repliesXml.getNodes("reply")) { - String replyId = replyXml.getValue("id", null); - String replyPostId = replyXml.getValue("post-id", null); - String replyTime = replyXml.getValue("time", null); - String replyText = replyXml.getValue("text", null); - if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, "Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", new Object[] { sone, replyId, replyPostId, replyTime, replyText }); - return null; - } - try { - replies.add(core.getReply(replyId).setSone(sone).setPost(core.getPost(replyPostId)).setTime(Long.parseLong(replyTime)).setText(replyText)); - } catch (NumberFormatException nfe1) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, "Downloaded reply for Sone %s with invalid time: %s", new Object[] { sone, replyTime }); - return null; + } else { + for (SimpleXML replyXml : repliesXml.getNodes("reply")) { + String replyId = replyXml.getValue("id", null); + String replyPostId = replyXml.getValue("post-id", null); + String replyTime = replyXml.getValue("time", null); + String replyText = replyXml.getValue("text", null); + if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) { + /* TODO - mark Sone as bad. */ + logger.log(Level.WARNING, "Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", new Object[] { sone, replyId, replyPostId, replyTime, replyText }); + return null; + } + try { + replies.add(core.getReply(replyId).setSone(sone).setPost(core.getPost(replyPostId)).setTime(Long.parseLong(replyTime)).setText(replyText)); + } catch (NumberFormatException nfe1) { + /* TODO - mark Sone as bad. */ + logger.log(Level.WARNING, "Downloaded reply for Sone %s with invalid time: %s", new Object[] { sone, replyTime }); + return null; + } } } /* parse liked post IDs. */ SimpleXML likePostIdsXml = soneXml.getNode("post-likes"); + Set likedPostIds = new HashSet(); if (likePostIdsXml == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, "Downloaded Sone %s has no post likes!", new Object[] { sone }); - return null; - } - - Set likedPostIds = new HashSet(); - for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) { - String postId = likedPostIdXml.getValue(); - likedPostIds.add(postId); + } else { + for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) { + String postId = likedPostIdXml.getValue(); + likedPostIds.add(postId); + } } /* parse liked reply IDs. */ SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes"); + Set likedReplyIds = new HashSet(); if (likeReplyIdsXml == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, "Downloaded Sone %s has no reply likes!", new Object[] { sone }); - return null; - } - - Set likedReplyIds = new HashSet(); - for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) { - String replyId = likedReplyIdXml.getValue(); - likedReplyIds.add(replyId); + } else { + for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) { + String replyId = likedReplyIdXml.getValue(); + likedReplyIds.add(replyId); + } } /* parse known Sones. */ SimpleXML knownSonesXml = soneXml.getNode("known-sones"); + Set knownSones = new HashSet(); if (knownSonesXml == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, "Downloaded Sone %s has no known Sones!", new Object[] { sone }); - return null; - } - - Set knownSones = new HashSet(); - for (SimpleXML knownSoneXml : knownSonesXml.getNodes("known-sone")) { - String knownSoneId = knownSoneXml.getValue("sone-id", null); - String knownSoneKey = knownSoneXml.getValue("sone-key", null); - String knownSoneName = knownSoneXml.getValue("sone-name", null); - if ((knownSoneId == null) || (knownSoneKey == null) || (knownSoneName == null)) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, "Downloaded known Sone for Sone %s with missing data! ID: %s, Key: %s, Name: %s", new Object[] { sone, knownSoneId, knownSoneKey, knownSoneName }); - return null; - } - try { - knownSones.add(core.getSone(knownSoneId).setRequestUri(new FreenetURI(knownSoneKey)).setName(knownSoneName)); - } catch (MalformedURLException mue1) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, "Downloaded known Sone for Sone %s with invalid key: %s", new Object[] { sone, knownSoneKey }); - return null; + } else { + for (SimpleXML knownSoneXml : knownSonesXml.getNodes("known-sone")) { + String knownSoneId = knownSoneXml.getValue("sone-id", null); + String knownSoneKey = knownSoneXml.getValue("sone-key", null); + String knownSoneName = knownSoneXml.getValue("sone-name", null); + if ((knownSoneId == null) || (knownSoneKey == null) || (knownSoneName == null)) { + /* TODO - mark Sone as bad. */ + logger.log(Level.WARNING, "Downloaded known Sone for Sone %s with missing data! ID: %s, Key: %s, Name: %s", new Object[] { sone, knownSoneId, knownSoneKey, knownSoneName }); + return null; + } + try { + knownSones.add(core.getSone(knownSoneId).setRequestUri(new FreenetURI(knownSoneKey)).setName(knownSoneName)); + } catch (MalformedURLException mue1) { + /* TODO - mark Sone as bad. */ + logger.log(Level.WARNING, "Downloaded known Sone for Sone %s with invalid key: %s", new Object[] { sone, knownSoneKey }); + return null; + } } } -- 2.7.4