X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParser.java;h=d2914daa21b6ef6b294a815f5bca6ce66a407935;hb=90d193f69c4aa12c232f937a9d2b7d5afdda81dd;hp=213be03411ebf118cd4f82b537df8a8b2259c230;hpb=43740d6a1dfc686ab54d0aebd4a5b1bce75b9ed6;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 213be03..d2914da 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -1,6 +1,8 @@ package net.pterodactylus.sone.core; import static java.util.logging.Logger.getLogger; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; +import static net.pterodactylus.sone.utils.NumberParsers.parseLong; import java.io.InputStream; import java.util.ArrayList; @@ -22,7 +24,6 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.SoneBuilder; -import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; @@ -61,21 +62,20 @@ public class SoneParser { if (originalSone.isLocal()) { soneBuilder = soneBuilder.local(); } - Sone sone = soneBuilder.build(); SimpleXML soneXml; try { soneXml = SimpleXML.fromDocument(document); } catch (NullPointerException npe1) { /* for some reason, invalid XML can cause NPEs. */ - logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", sone), npe1); + logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone), npe1); return null; } Integer protocolVersion = null; String soneProtocolVersion = soneXml.getValue("protocol-version", null); if (soneProtocolVersion != null) { - protocolVersion = Numbers.safeParseInteger(soneProtocolVersion); + protocolVersion = parseInt(soneProtocolVersion, null); } if (protocolVersion == null) { logger.log(Level.INFO, "No protocol version found, assuming 0."); @@ -96,14 +96,14 @@ public class SoneParser { 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)); + logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", originalSone)); return null; } try { - sone.setTime(Long.parseLong(soneTime)); + soneBuilder.lastUpdated(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)); + logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", originalSone, soneTime)); return null; } @@ -112,16 +112,18 @@ public class SoneParser { String clientName = clientXml.getValue("name", null); String clientVersion = clientXml.getValue("version", null); if ((clientName == null) || (clientVersion == null)) { - logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone)); + logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", originalSone)); return null; } - sone.setClient(new Client(clientName, clientVersion)); + soneBuilder.using(new Client(clientName, clientVersion)); + } else { + soneBuilder.using(new Client("Unknown Client", "0.0")); } 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)); + logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", originalSone)); return null; } @@ -129,10 +131,10 @@ public class SoneParser { String profileFirstName = profileXml.getValue("first-name", null); String profileMiddleName = profileXml.getValue("middle-name", null); String profileLastName = profileXml.getValue("last-name", null); - Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null)); - Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null)); - Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null)); - Profile profile = new Profile(sone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName); + Integer profileBirthDay = parseInt(profileXml.getValue("birth-day", ""), null); + Integer profileBirthMonth = parseInt(profileXml.getValue("birth-month", ""), null); + Integer profileBirthYear = parseInt(profileXml.getValue("birth-year", ""), null); + Profile profile = new Profile(originalSone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName); profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear); /* avatar is processed after images are loaded. */ String avatarId = profileXml.getValue("avatar", null); @@ -144,7 +146,7 @@ public class SoneParser { String fieldName = fieldXml.getValue("field-name", null); String fieldValue = fieldXml.getValue("field-value", ""); if (fieldName == null) { - logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", sone, fieldName, fieldValue)); + logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", originalSone, fieldName, fieldValue)); return null; } try { @@ -158,11 +160,11 @@ public class SoneParser { /* parse posts. */ SimpleXML postsXml = soneXml.getNode("posts"); - Set posts = new HashSet(); if (postsXml == null) { /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone)); + logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", originalSone)); } else { + Set posts = new HashSet(); for (SimpleXML postXml : postsXml.getNodes("post")) { String postId = postXml.getValue("id", null); String postRecipientId = postXml.getValue("recipient", null); @@ -170,24 +172,26 @@ public class SoneParser { String postText = postXml.getValue("text", null); if ((postId == null) || (postTime == null) || (postText == null)) { /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", sone, postId, postTime, postText)); + logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", originalSone, postId, postTime, postText)); return null; } try { PostBuilder postBuilder = core.postBuilder(); /* TODO - parse time correctly. */ - postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText); + postBuilder.withId(postId).from(originalSone.getId()).withTime(Long.parseLong(postTime)).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { postBuilder.to(postRecipientId); } posts.add(postBuilder.build()); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime)); + logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", originalSone, postTime)); return null; } } + soneBuilder.withPosts(posts); } + Sone sone = soneBuilder.build(); /* parse replies. */ SimpleXML repliesXml = soneXml.getNode("replies"); @@ -295,9 +299,9 @@ public class SoneParser { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone)); return null; } - long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L); - int imageWidth = Numbers.safeParseInteger(imageWidthString, 0); - int imageHeight = Numbers.safeParseInteger(imageHeightString, 0); + long creationTime = parseLong(imageCreationTimeString, 0L); + int imageWidth = parseInt(imageWidthString, 0); + int imageHeight = parseInt(imageHeightString, 0); if ((imageWidth < 1) || (imageHeight < 1)) { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString)); return null; @@ -322,7 +326,6 @@ public class SoneParser { /* atomic setter operation on the Sone. */ synchronized (sone) { sone.setProfile(profile); - sone.setPosts(posts); sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds);