Rename fetch action methods
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneParser.java
index 5dca54c..fdbd9ae 100644 (file)
@@ -1,5 +1,9 @@
 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;
 import java.util.HashMap;
@@ -16,12 +20,12 @@ import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Profile.DuplicateField;
+import net.pterodactylus.sone.data.Profile.EmptyFieldName;
 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.logging.Logging;
-import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.xml.SimpleXML;
 import net.pterodactylus.util.xml.XML;
 
@@ -34,7 +38,7 @@ import org.w3c.dom.Document;
  */
 public class SoneParser {
 
-       private static final Logger logger = Logging.getLogger(SoneParser.class);
+       private static final Logger logger = getLogger(SoneParser.class.getName());
        private static final int MAX_PROTOCOL_VERSION = 0;
        private final Core core;
 
@@ -74,7 +78,7 @@ public class SoneParser {
                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.");
@@ -128,9 +132,9 @@ 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));
+               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.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear);
                /* avatar is processed after images are loaded. */
@@ -148,8 +152,11 @@ public class SoneParser {
                                }
                                try {
                                        profile.addField(fieldName.trim()).setValue(fieldValue);
-                               } catch (IllegalArgumentException iae1) {
-                                       logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
+                               } catch (EmptyFieldName efn1) {
+                                       logger.log(Level.WARNING, "Empty field name!", efn1);
+                                       return null;
+                               } catch (DuplicateField df1) {
+                                       logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), df1);
                                        return null;
                                }
                        }
@@ -254,7 +261,6 @@ public class SoneParser {
                                String parentId = albumXml.getValue("parent", null);
                                String title = albumXml.getValue("title", null);
                                String description = albumXml.getValue("description", "");
-                               String albumImageId = albumXml.getValue("album-image", null);
                                if ((id == null) || (title == null)) {
                                        logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
                                        return null;
@@ -294,9 +300,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;
@@ -308,7 +314,6 @@ public class SoneParser {
                                                allImages.put(imageId, image);
                                        }
                                }
-                               album.modify().setAlbumImage(albumImageId).update();
                        }
                }
 
@@ -318,16 +323,13 @@ public class SoneParser {
                }
 
                /* okay, apparently everything was parsed correctly. Now import. */
-               /* 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) {
-                               sone.getRootAlbum().addAlbum(album);
-                       }
+               sone.setProfile(profile);
+               sone.setPosts(posts);
+               sone.setReplies(replies);
+               sone.setLikePostIds(likedPostIds);
+               sone.setLikeReplyIds(likedReplyIds);
+               for (Album album : topLevelAlbums) {
+                       sone.getRootAlbum().addAlbum(album);
                }
 
                return sone;