X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParser.java;h=38b21e332debdd0b448042e6d171fe117f4aa3d8;hb=c4eb31ab64627adfdeed2a445d67883371203e99;hp=1fe4794853b57da2dbd5ef87e8d7d2d5dc65cf00;hpb=a3ba9ccfd92c4497a40f5326254a2200e5016c82;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 1fe4794..38b21e3 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -48,6 +48,7 @@ import net.pterodactylus.util.xml.XML; import com.google.common.base.Optional; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.google.common.primitives.Ints; import org.w3c.dom.Document; @@ -85,48 +86,76 @@ public class SoneParser { 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.getId())); - throw new InvalidXml(); - } - - Optional parsedClient = parseClient(originalSone, soneXml.get()); + SimpleXML soneXml = SimpleXML.fromDocument(document); + Optional parsedClient = parseClient(originalSone, soneXml); Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient())); - 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(); - } - } + verifyProtocolVersion(soneXml); - 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)); - 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)); - throw new MalformedXml(); - } + parseTime(soneXml, sone); - SimpleXML profileXml = soneXml.get().getNode("profile"); + 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)); throw new MalformedXml(); } + /* parse albums. */ + SimpleXML albumsXml = soneXml.getNode("albums"); + Map albums = Maps.newHashMap(); + Set images = Sets.newHashSet(); + if (albumsXml != null) { + for (SimpleXML albumXml : albumsXml.getNodes("album")) { + String id = albumXml.getValue("id", null); + 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) || (description == null)) { + logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone)); + 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)); + throw new InvalidParentAlbum(); + } + } + Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update(); + albums.put(album.getId(), album); + SimpleXML imagesXml = albumXml.getNode("images"); + if (imagesXml != null) { + for (SimpleXML imageXml : imagesXml.getNodes("image")) { + String imageId = imageXml.getValue("id", null); + String imageCreationTimeString = imageXml.getValue("creation-time", null); + String imageKey = imageXml.getValue("key", null); + String imageTitle = imageXml.getValue("title", null); + String imageDescription = imageXml.getValue("description", ""); + String imageWidthString = imageXml.getValue("width", null); + String imageHeightString = imageXml.getValue("height", null); + if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) { + logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone)); + throw new MalformedXml(); + } + long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L); + int imageWidth = Numbers.safeParseInteger(imageWidthString, 0); + int imageHeight = Numbers.safeParseInteger(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)); + throw new MalformedDimension(); + } + Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.absent()); + image.modify().setTitle(imageTitle).setDescription(imageDescription).update(); + images.add(imageId); + } + } + album.modify().setAlbumImage(albumImageId).update(); + } + } + /* parse profile. */ String profileFirstName = profileXml.getValue("first-name", null); String profileMiddleName = profileXml.getValue("middle-name", null); @@ -136,8 +165,13 @@ public class SoneParser { Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null)); Profile profile = new Profile(sone).modify().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName).update(); profile.modify().setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear).update(); + /* avatar is processed after images are loaded. */ String avatarId = profileXml.getValue("avatar", null); + if ((avatarId != null) && !images.contains(avatarId)) { + throw new InvalidAvatarId(); + } + profile.setAvatar(fromNullable(avatarId)); /* parse profile fields. */ SimpleXML profileFieldsXml = profileXml.getNode("fields"); @@ -159,7 +193,7 @@ public class SoneParser { } /* parse posts. */ - SimpleXML postsXml = soneXml.get().getNode("posts"); + SimpleXML postsXml = soneXml.getNode("posts"); Set posts = new HashSet(); if (postsXml == null) { /* TODO - mark Sone as bad. */ @@ -192,7 +226,7 @@ public class SoneParser { } /* parse replies. */ - SimpleXML repliesXml = soneXml.get().getNode("replies"); + SimpleXML repliesXml = soneXml.getNode("replies"); Set replies = new HashSet(); if (repliesXml == null) { /* TODO - mark Sone as bad. */ @@ -206,7 +240,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. */ @@ -215,13 +249,13 @@ 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(); } } } /* parse liked post IDs. */ - SimpleXML likePostIdsXml = soneXml.get().getNode("post-likes"); + SimpleXML likePostIdsXml = soneXml.getNode("post-likes"); Set likedPostIds = new HashSet(); if (likePostIdsXml == null) { /* TODO - mark Sone as bad. */ @@ -234,7 +268,7 @@ public class SoneParser { } /* parse liked reply IDs. */ - SimpleXML likeReplyIdsXml = soneXml.get().getNode("reply-likes"); + SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes"); Set likedReplyIds = new HashSet(); if (likeReplyIdsXml == null) { /* TODO - mark Sone as bad. */ @@ -246,62 +280,6 @@ public class SoneParser { } } - /* parse albums. */ - SimpleXML albumsXml = soneXml.get().getNode("albums"); - Map albums = Maps.newHashMap(); - if (albumsXml != null) { - for (SimpleXML albumXml : albumsXml.getNodes("album")) { - String id = albumXml.getValue("id", null); - 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) || (description == null)) { - logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone)); - return null; - } - 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; - } - } - Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update(); - albums.put(album.getId(), album); - SimpleXML imagesXml = albumXml.getNode("images"); - if (imagesXml != null) { - for (SimpleXML imageXml : imagesXml.getNodes("image")) { - String imageId = imageXml.getValue("id", null); - String imageCreationTimeString = imageXml.getValue("creation-time", null); - String imageKey = imageXml.getValue("key", null); - String imageTitle = imageXml.getValue("title", null); - String imageDescription = imageXml.getValue("description", ""); - String imageWidthString = imageXml.getValue("width", null); - String imageHeightString = imageXml.getValue("height", null); - if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) { - 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); - 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; - } - Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.absent()); - image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update(); - } - } - album.modify().setAlbumImage(albumImageId).update(); - } - } - - /* process avatar. */ - profile.setAvatar(fromNullable(avatarId)); - /* okay, apparently everything was parsed correctly. Now import. */ sone.setProfile(profile); sone.setPosts(posts); @@ -312,6 +290,36 @@ public class SoneParser { return sone; } + private void parseTime(SimpleXML soneXml, Sone sone) { + 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)); + 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)); + throw new MalformedTime(); + } + } + + private void verifyProtocolVersion(SimpleXML soneXml) { + Optional protocolVersion = parseProtocolVersion(soneXml); + 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 SoneTooNew(); + } + } + } + private Optional parseProtocolVersion(SimpleXML soneXml) { String soneProtocolVersion = soneXml.getValue("protocol-version", null); if (soneProtocolVersion == null) { @@ -321,15 +329,6 @@ public class SoneParser { return fromNullable(Ints.tryParse(soneProtocolVersion)); } - private Optional 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 parseClient(Sone sone, SimpleXML soneXml) { SimpleXML clientXml = soneXml.getNode("client"); if (clientXml == null) { @@ -352,15 +351,32 @@ public class SoneParser { } + public static class SoneTooNew extends RuntimeException { + + } + public static class MalformedXml extends RuntimeException { } - public static class DuplicateField extends RuntimeException { + public static class InvalidAvatarId extends RuntimeException { + + } + + public static class DuplicateField extends RuntimeException { } public static class MalformedTime extends RuntimeException { } + + public static class InvalidParentAlbum extends RuntimeException { + + } + + public static class MalformedDimension extends RuntimeException { + + } + }