Use different method to create a local Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneParser.java
index 5851b18..218a368 100644 (file)
@@ -59,17 +59,13 @@ public class SoneParser {
                }
 
                SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity());
-               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;
                }
 
@@ -97,14 +93,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;
                }
 
@@ -113,16 +109,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;
                }
 
@@ -133,7 +131,7 @@ public class SoneParser {
                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(sone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName);
+               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);
@@ -145,7 +143,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 {
@@ -159,11 +157,11 @@ public class SoneParser {
 
                /* parse posts. */
                SimpleXML postsXml = soneXml.getNode("posts");
-               Set<Post> posts = new HashSet<Post>();
                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<Post> posts = new HashSet<Post>();
                        for (SimpleXML postXml : postsXml.getNodes("post")) {
                                String postId = postXml.getValue("id", null);
                                String postRecipientId = postXml.getValue("recipient", null);
@@ -171,32 +169,33 @@ 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);
                }
 
                /* parse replies. */
                SimpleXML repliesXml = soneXml.getNode("replies");
-               Set<PostReply> replies = new HashSet<PostReply>();
                if (repliesXml == null) {
                        /* TODO - mark Sone as bad. */
-                       logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
+                       logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", originalSone));
                } else {
+                       Set<PostReply> replies = new HashSet<PostReply>();
                        for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
                                String replyId = replyXml.getValue("id", null);
                                String replyPostId = replyXml.getValue("post-id", null);
@@ -204,21 +203,23 @@ public class SoneParser {
                                String replyText = replyXml.getValue("text", null);
                                if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
                                        /* TODO - mark Sone as bad. */
-                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", sone, replyId, replyPostId, replyTime, replyText));
+                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", originalSone, replyId, replyPostId, replyTime, replyText));
                                        return null;
                                }
                                try {
                                        PostReplyBuilder postReplyBuilder = core.postReplyBuilder();
                                        /* TODO - parse time correctly. */
-                                       postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
+                                       postReplyBuilder.withId(replyId).from(originalSone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
                                        replies.add(postReplyBuilder.build());
                                } catch (NumberFormatException nfe1) {
                                        /* TODO - mark Sone as bad. */
-                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
+                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", originalSone, replyTime));
                                        return null;
                                }
                        }
+                       soneBuilder.withPostReplies(replies);
                }
+               Sone sone = originalSone.isLocal() ? soneBuilder.buildLocal() : soneBuilder.build();
 
                /* parse liked post IDs. */
                SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
@@ -323,8 +324,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);
                        for (Album album : topLevelAlbums) {