Clean up imports.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneParser.java
index 7ecae92..470e163 100644 (file)
 
 package net.pterodactylus.sone.core;
 
+import static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.base.Optional.of;
+
 import java.io.InputStream;
-import java.net.MalformedURLException;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -33,20 +36,19 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.impl.DefaultSone;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.ImageBuilder.ImageCreated;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
-import net.pterodactylus.sone.database.memory.MemoryDatabase;
 import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.xml.SimpleXML;
 import net.pterodactylus.util.xml.XML;
 
-import freenet.keys.FreenetURI;
-
 import com.google.common.base.Optional;
 import com.google.common.collect.Maps;
+import com.google.common.primitives.Ints;
 import org.w3c.dom.Document;
 
 /**
@@ -58,11 +60,6 @@ public class SoneParser {
 
        private static final Logger logger = Logger.getLogger(SoneParser.class.getName());
        private static final int MAX_PROTOCOL_VERSION = 0;
-       private final Core core;
-
-       public SoneParser(Core core) {
-               this.core = core;
-       }
 
        /**
         * Parses a Sone from the given input stream and creates a new Sone from the
@@ -73,10 +70,8 @@ public class SoneParser {
         * @param soneInputStream
         *              The input stream to parse the Sone from
         * @return The parsed Sone
-        * @throws SoneException
-        *              if a parse error occurs, or the protocol is invalid
         */
-       public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException {
+       public Sone parseSone(Database database, Sone originalSone, InputStream soneInputStream) {
                /* TODO - impose a size limit? */
 
                Document document;
@@ -86,43 +81,32 @@ public class SoneParser {
                }
                if (document == null) {
                        /* TODO - mark Sone as bad. */
-                       logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone));
+                       logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone.getId()));
                        return null;
                }
 
-               Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal());
-
-               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);
+               Optional<SimpleXML> soneXml = parseXml(originalSone, document);
+               if (!soneXml.isPresent()) {
+                       logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone.getId()));
                        return null;
                }
 
-               Integer protocolVersion = null;
-               String soneProtocolVersion = soneXml.getValue("protocol-version", null);
-               if (soneProtocolVersion != null) {
-                       protocolVersion = Numbers.safeParseInteger(soneProtocolVersion);
-               }
-               if (protocolVersion == null) {
-                       logger.log(Level.INFO, "No protocol version found, assuming 0.");
-                       protocolVersion = 0;
-               }
+               Optional<Client> parsedClient = parseClient(originalSone, soneXml.get());
+               Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient()));
 
-               if (protocolVersion < 0) {
-                       logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion));
-                       return null;
-               }
-
-               /* check for valid versions. */
-               if (protocolVersion > MAX_PROTOCOL_VERSION) {
-                       logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion));
-                       return null;
+               Optional<Integer> 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()));
+                               return null;
+                       }
+                       if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
+                               logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
+                               return null;
+                       }
                }
 
-               String soneTime = soneXml.getValue("time", null);
+               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));
@@ -136,22 +120,7 @@ public class SoneParser {
                        return null;
                }
 
-               SimpleXML clientXml = soneXml.getNode("client");
-               if (clientXml != null) {
-                       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));
-                               return null;
-                       }
-                       sone.setClient(new Client(clientName, clientVersion));
-               }
-
-               if (originalSone.getInsertUri() != null) {
-                       sone.setInsertUri(originalSone.getInsertUri());
-               }
-
-               SimpleXML profileXml = soneXml.getNode("profile");
+               SimpleXML profileXml = soneXml.get().getNode("profile");
                if (profileXml == null) {
                        /* TODO - mark Sone as bad. */
                        logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone));
@@ -190,7 +159,7 @@ public class SoneParser {
                }
 
                /* parse posts. */
-               SimpleXML postsXml = soneXml.getNode("posts");
+               SimpleXML postsXml = soneXml.get().getNode("posts");
                Set<Post> posts = new HashSet<Post>();
                if (postsXml == null) {
                        /* TODO - mark Sone as bad. */
@@ -211,7 +180,7 @@ public class SoneParser {
                                        /* TODO - parse time correctly. */
                                        postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
                                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
-                                               postBuilder.to(Optional.of(postRecipientId));
+                                               postBuilder.to(of(postRecipientId));
                                        }
                                        posts.add(postBuilder.build(Optional.<PostCreated>absent()));
                                } catch (NumberFormatException nfe1) {
@@ -223,7 +192,7 @@ public class SoneParser {
                }
 
                /* parse replies. */
-               SimpleXML repliesXml = soneXml.getNode("replies");
+               SimpleXML repliesXml = soneXml.get().getNode("replies");
                Set<PostReply> replies = new HashSet<PostReply>();
                if (repliesXml == null) {
                        /* TODO - mark Sone as bad. */
@@ -252,7 +221,7 @@ public class SoneParser {
                }
 
                /* parse liked post IDs. */
-               SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
+               SimpleXML likePostIdsXml = soneXml.get().getNode("post-likes");
                Set<String> likedPostIds = new HashSet<String>();
                if (likePostIdsXml == null) {
                        /* TODO - mark Sone as bad. */
@@ -265,7 +234,7 @@ public class SoneParser {
                }
 
                /* parse liked reply IDs. */
-               SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes");
+               SimpleXML likeReplyIdsXml = soneXml.get().getNode("reply-likes");
                Set<String> likedReplyIds = new HashSet<String>();
                if (likeReplyIdsXml == null) {
                        /* TODO - mark Sone as bad. */
@@ -278,7 +247,7 @@ public class SoneParser {
                }
 
                /* parse albums. */
-               SimpleXML albumsXml = soneXml.getNode("albums");
+               SimpleXML albumsXml = soneXml.get().getNode("albums");
                Map<String, Album> albums = Maps.newHashMap();
                if (albumsXml != null) {
                        for (SimpleXML albumXml : albumsXml.getNodes("album")) {
@@ -331,9 +300,7 @@ public class SoneParser {
                }
 
                /* process avatar. */
-               if (avatarId != null) {
-                       profile.setAvatar(core.getImage(avatarId).orNull());
-               }
+               profile.setAvatar(fromNullable(avatarId));
 
                /* okay, apparently everything was parsed correctly. Now import. */
                sone.setProfile(profile);
@@ -344,4 +311,37 @@ public class SoneParser {
 
                return sone;
        }
+
+       private Optional<Integer> parseProtocolVersion(SimpleXML soneXml) {
+               String soneProtocolVersion = soneXml.getValue("protocol-version", null);
+               if (soneProtocolVersion == null) {
+                       logger.log(Level.INFO, "No protocol version found, assuming 0.");
+                       return absent();
+               }
+               return fromNullable(Ints.tryParse(soneProtocolVersion));
+       }
+
+       private Optional<SimpleXML> parseXml(Sone originalSone, Document document) {
+               try {
+                       return fromNullable(SimpleXML.fromDocument(document));
+               } catch (NullPointerException npe1) {
+                       /* for some reason, invalid XML can cause NPEs. */
+                       return absent();
+               }
+       }
+
+       private Optional<Client> parseClient(Sone sone, SimpleXML soneXml) {
+               SimpleXML clientXml = soneXml.getNode("client");
+               if (clientXml == null) {
+                       return absent();
+               }
+               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));
+                       return null;
+               }
+               return of(new Client(clientName, clientVersion));
+       }
+
 }