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;
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;
/**
* Parses a {@link Sone} from an XML {@link InputStream}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class SoneParser {
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 {
return null;
}
- SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity());
+ SoneBuilder soneBuilder = database.newSoneBuilder().from(originalSone.getIdentity());
if (originalSone.isLocal()) {
soneBuilder = soneBuilder.local();
}
/* parse posts. */
SimpleXML postsXml = soneXml.getNode("posts");
- Set<Post> posts = new HashSet<Post>();
+ Set<Post> posts = new HashSet<>();
if (postsXml == null) {
/* TODO - mark Sone as bad. */
logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone));
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)) {
/* parse replies. */
SimpleXML repliesXml = soneXml.getNode("replies");
- Set<PostReply> replies = new HashSet<PostReply>();
+ Set<PostReply> replies = new HashSet<>();
if (repliesXml == null) {
/* TODO - mark Sone as bad. */
logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
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());
/* parse liked post IDs. */
SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
- Set<String> likedPostIds = new HashSet<String>();
+ Set<String> likedPostIds = new HashSet<>();
if (likePostIdsXml == null) {
/* TODO - mark Sone as bad. */
logger.log(Level.WARNING, String.format("Downloaded Sone %s has no post likes!", sone));
/* parse liked reply IDs. */
SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes");
- Set<String> likedReplyIds = new HashSet<String>();
+ Set<String> likedReplyIds = new HashSet<>();
if (likeReplyIdsXml == null) {
/* TODO - mark Sone as bad. */
logger.log(Level.WARNING, String.format("Downloaded Sone %s has no reply likes!", sone));
/* parse albums. */
SimpleXML albumsXml = soneXml.getNode("albums");
- Map<String, Image> allImages = new HashMap<String, Image>();
- List<Album> topLevelAlbums = new ArrayList<Album>();
+ Map<String, Image> allImages = new HashMap<>();
+ List<Album> topLevelAlbums = new ArrayList<>();
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)) {
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()
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();
}
}