X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParser.java;h=512da1bb435ce3d25aaaf5759dafdc056c9bfb75;hb=a56d9c3bc7315ce5655d71946ba38c51e8161b41;hp=8d4883dab107b0cf50452c690af5024d51de180c;hpb=49ff797a72d192c6772cd3f9e201beb8146d2462;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 8d4883d..512da1b 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -36,18 +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 com.google.common.base.Optional; import com.google.common.collect.Maps; +import com.google.common.primitives.Ints; import org.w3c.dom.Document; /** @@ -59,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 @@ -74,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; @@ -87,59 +81,50 @@ 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)); - return null; + logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone.getId())); + throw new InvalidXml(); } Optional soneXml = parseXml(originalSone, document); if (!soneXml.isPresent()) { - logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone)); - return null; + logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone.getId())); + throw new InvalidXml(); } Optional parsedClient = parseClient(originalSone, soneXml.get()); - Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient())); + Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient())); - Integer protocolVersion = null; - String soneProtocolVersion = soneXml.get().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; - } - - 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 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())); + throw new InvalidProtocolVersion(); + } + if (protocolVersion.get() > MAX_PROTOCOL_VERSION) { + logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get())); + throw new InvalidProtocolVersion(); + } } 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)); - 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. */ @@ -162,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(); } } } @@ -188,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(); @@ -201,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(); } } } @@ -221,7 +206,7 @@ public class SoneParser { 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)); - return null; + throw new MalformedXml(); } try { /* TODO - parse time correctly. */ @@ -230,7 +215,7 @@ public class SoneParser { } 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)); - return null; + throw new MalformedTime(); } } } @@ -273,14 +258,14 @@ public class SoneParser { String albumImageId = albumXml.getValue("album-image", null); if ((id == null) || (title == null) || (description == null)) { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone)); - return null; + throw new MalformedXml(); } Album parent = sone.getRootAlbum(); if (parentId != null) { parent = albums.get(parentId); if (parent == null) { logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone)); - return null; + throw new InvalidParentAlbum(); } } Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update(); @@ -315,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); @@ -329,6 +312,15 @@ public class SoneParser { return sone; } + private Optional 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 parseXml(Sone originalSone, Document document) { try { return fromNullable(SimpleXML.fromDocument(document)); @@ -347,9 +339,33 @@ 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 { + + } + + public static class InvalidParentAlbum extends RuntimeException { + + } + }