X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParser.java;h=ce0606b4af46c50cfebba37dedfd10bfd09c93ea;hp=e234965da331ef92f20f5fa1afe09c3577f068b9;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=28667abdadc0573ac106542f1fd42ab5775ec415 diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index e234965..ce0606b 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -14,6 +14,8 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import javax.inject.Inject; + import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; @@ -21,7 +23,9 @@ 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.Database; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.SoneBuilder; @@ -32,17 +36,16 @@ import org.w3c.dom.Document; /** * Parses a {@link Sone} from an XML {@link InputStream}. - * - * @author David ‘Bombe’ Roden */ public class SoneParser { - private static final Logger logger = getLogger("Sone.Parser"); + private static final Logger logger = getLogger(SoneParser.class.getName()); private static final int MAX_PROTOCOL_VERSION = 0; - private final Core core; + private final Database database; - public SoneParser(Core core) { - this.core = core; + @Inject + public SoneParser(Database database) { + this.database = database; } public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException { @@ -59,7 +62,7 @@ public class SoneParser { return null; } - SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity()); + SoneBuilder soneBuilder = database.newSoneBuilder().from(originalSone.getIdentity()); if (originalSone.isLocal()) { soneBuilder = soneBuilder.local(); } @@ -151,6 +154,9 @@ public class SoneParser { } try { profile.addField(fieldName.trim()).setValue(fieldValue); + } 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; @@ -176,7 +182,7 @@ public class SoneParser { return null; } try { - PostBuilder postBuilder = core.postBuilder(); + PostBuilder postBuilder = database.newPostBuilder(); /* TODO - parse time correctly. */ postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { @@ -209,7 +215,7 @@ public class SoneParser { return null; } try { - PostReplyBuilder postReplyBuilder = core.postReplyBuilder(); + PostReplyBuilder postReplyBuilder = database.newPostReplyBuilder(); /* TODO - parse time correctly. */ postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText); replies.add(postReplyBuilder.build()); @@ -257,20 +263,19 @@ 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; } Album parent = null; if (parentId != null) { - parent = core.getAlbum(parentId); + parent = database.getAlbum(parentId); if (parent == null) { logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone)); return null; } } - Album album = core.albumBuilder() + Album album = database.newAlbumBuilder() .withId(id) .by(sone) .build() @@ -304,14 +309,13 @@ public class SoneParser { 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 = core.imageBuilder().withId(imageId).build().modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update(); + Image image = database.newImageBuilder().withId(imageId).build().modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update(); image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update(); image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update(); album.addImage(image); allImages.put(imageId, image); } } - album.modify().setAlbumImage(albumImageId).update(); } } @@ -321,16 +325,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;