Throw special exception if a time can not be parsed.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneParser.java
index 17d741e..1fe4794 100644 (file)
@@ -42,7 +42,6 @@ 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;
@@ -71,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(Database database, Sone originalSone, InputStream soneInputStream) throws SoneException {
+       public Sone parseSone(Database database, Sone originalSone, InputStream soneInputStream) {
                /* TODO - impose a size limit? */
 
                Document document;
@@ -85,13 +82,13 @@ 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.getId()));
-                       return null;
+                       throw new InvalidXml();
                }
 
                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;
+                       throw new InvalidXml();
                }
 
                Optional<Client> parsedClient = parseClient(originalSone, soneXml.get());
@@ -100,12 +97,12 @@ public class SoneParser {
                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));
-                               return null;
+                               logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion.get()));
+                               throw new InvalidProtocolVersion();
                        }
                        if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
-                               logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion));
-                               return null;
+                               logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
+                               throw new InvalidProtocolVersion();
                        }
                }
 
@@ -113,21 +110,21 @@ public class SoneParser {
                if (soneTime == null) {
                        /* TODO - mark Sone as bad. */
                        logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
-                       return null;
+                       throw new MalformedXml();
                }
                try {
                        sone.setTime(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));
-                       return null;
+                       throw new MalformedXml();
                }
 
                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));
-                       return null;
+                       throw new MalformedXml();
                }
 
                /* parse profile. */
@@ -150,13 +147,13 @@ public class SoneParser {
                                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));
-                                       return null;
+                                       throw new MalformedXml();
                                }
                                try {
-                                       profile.addField(fieldName).setValue(fieldValue);
+                                       profile.setField(profile.addField(fieldName), fieldValue);
                                } catch (IllegalArgumentException iae1) {
                                        logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
-                                       return null;
+                                       throw new DuplicateField();
                                }
                        }
                }
@@ -176,7 +173,7 @@ public class SoneParser {
                                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));
-                                       return null;
+                                       throw new MalformedXml();
                                }
                                try {
                                        PostBuilder postBuilder = sone.newPostBuilder();
@@ -189,7 +186,7 @@ public class SoneParser {
                                } 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));
-                                       return null;
+                                       throw new MalformedTime();
                                }
                        }
                }
@@ -342,9 +339,28 @@ public class SoneParser {
                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 absent();
                }
                return of(new Client(clientName, clientVersion));
        }
 
+       public static class InvalidXml extends RuntimeException {
+
+       }
+
+       public static class InvalidProtocolVersion extends RuntimeException {
+
+       }
+
+       public static class MalformedXml extends RuntimeException {
+
+       }
+
+        public static class DuplicateField extends RuntimeException {
+
+       }
+
+       public static class MalformedTime extends RuntimeException {
+
+       }
 }