1 package net.pterodactylus.sone.core;
3 import java.io.InputStream;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.HashSet;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
13 import net.pterodactylus.sone.data.Album;
14 import net.pterodactylus.sone.data.Client;
15 import net.pterodactylus.sone.data.Image;
16 import net.pterodactylus.sone.data.Post;
17 import net.pterodactylus.sone.data.PostReply;
18 import net.pterodactylus.sone.data.Profile;
19 import net.pterodactylus.sone.data.Sone;
20 import net.pterodactylus.sone.database.PostBuilder;
21 import net.pterodactylus.sone.database.PostReplyBuilder;
22 import net.pterodactylus.sone.database.SoneBuilder;
23 import net.pterodactylus.util.logging.Logging;
24 import net.pterodactylus.util.number.Numbers;
25 import net.pterodactylus.util.xml.SimpleXML;
26 import net.pterodactylus.util.xml.XML;
28 import org.w3c.dom.Document;
31 * Parses a {@link Sone} from an XML {@link InputStream}.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class SoneParser {
37 private static final Logger logger = Logging.getLogger(SoneParser.class);
38 private static final int MAX_PROTOCOL_VERSION = 0;
39 private final Core core;
41 public SoneParser(Core core) {
45 public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException {
46 /* TODO - impose a size limit? */
49 /* XML parsing is not thread-safe. */
51 document = XML.transformToDocument(soneInputStream);
53 if (document == null) {
54 /* TODO - mark Sone as bad. */
55 logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone));
59 SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity());
60 if (originalSone.isLocal()) {
61 soneBuilder = soneBuilder.local();
63 Sone sone = soneBuilder.build();
67 soneXml = SimpleXML.fromDocument(document);
68 } catch (NullPointerException npe1) {
69 /* for some reason, invalid XML can cause NPEs. */
70 logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", sone), npe1);
74 Integer protocolVersion = null;
75 String soneProtocolVersion = soneXml.getValue("protocol-version", null);
76 if (soneProtocolVersion != null) {
77 protocolVersion = Numbers.safeParseInteger(soneProtocolVersion);
79 if (protocolVersion == null) {
80 logger.log(Level.INFO, "No protocol version found, assuming 0.");
84 if (protocolVersion < 0) {
85 logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion));
89 /* check for valid versions. */
90 if (protocolVersion > MAX_PROTOCOL_VERSION) {
91 logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion));
95 String soneTime = soneXml.getValue("time", null);
96 if (soneTime == null) {
97 /* TODO - mark Sone as bad. */
98 logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
102 sone.setTime(Long.parseLong(soneTime));
103 } catch (NumberFormatException nfe1) {
104 /* TODO - mark Sone as bad. */
105 logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
109 SimpleXML clientXml = soneXml.getNode("client");
110 if (clientXml != null) {
111 String clientName = clientXml.getValue("name", null);
112 String clientVersion = clientXml.getValue("version", null);
113 if ((clientName == null) || (clientVersion == null)) {
114 logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone));
117 sone.setClient(new Client(clientName, clientVersion));
120 SimpleXML profileXml = soneXml.getNode("profile");
121 if (profileXml == null) {
122 /* TODO - mark Sone as bad. */
123 logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone));
128 String profileFirstName = profileXml.getValue("first-name", null);
129 String profileMiddleName = profileXml.getValue("middle-name", null);
130 String profileLastName = profileXml.getValue("last-name", null);
131 Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
132 Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
133 Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
134 Profile profile = new Profile(sone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName);
135 profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear);
136 /* avatar is processed after images are loaded. */
137 String avatarId = profileXml.getValue("avatar", null);
139 /* parse profile fields. */
140 SimpleXML profileFieldsXml = profileXml.getNode("fields");
141 if (profileFieldsXml != null) {
142 for (SimpleXML fieldXml : profileFieldsXml.getNodes("field")) {
143 String fieldName = fieldXml.getValue("field-name", null);
144 String fieldValue = fieldXml.getValue("field-value", "");
145 if (fieldName == null) {
146 logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", sone, fieldName, fieldValue));
150 profile.addField(fieldName.trim()).setValue(fieldValue);
151 } catch (IllegalArgumentException iae1) {
152 logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
159 SimpleXML postsXml = soneXml.getNode("posts");
160 Set<Post> posts = new HashSet<Post>();
161 if (postsXml == null) {
162 /* TODO - mark Sone as bad. */
163 logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone));
165 for (SimpleXML postXml : postsXml.getNodes("post")) {
166 String postId = postXml.getValue("id", null);
167 String postRecipientId = postXml.getValue("recipient", null);
168 String postTime = postXml.getValue("time", null);
169 String postText = postXml.getValue("text", null);
170 if ((postId == null) || (postTime == null) || (postText == null)) {
171 /* TODO - mark Sone as bad. */
172 logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", sone, postId, postTime, postText));
176 PostBuilder postBuilder = core.postBuilder();
177 /* TODO - parse time correctly. */
178 postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText);
179 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
180 postBuilder.to(postRecipientId);
182 posts.add(postBuilder.build());
183 } catch (NumberFormatException nfe1) {
184 /* TODO - mark Sone as bad. */
185 logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime));
192 SimpleXML repliesXml = soneXml.getNode("replies");
193 Set<PostReply> replies = new HashSet<PostReply>();
194 if (repliesXml == null) {
195 /* TODO - mark Sone as bad. */
196 logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
198 for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
199 String replyId = replyXml.getValue("id", null);
200 String replyPostId = replyXml.getValue("post-id", null);
201 String replyTime = replyXml.getValue("time", null);
202 String replyText = replyXml.getValue("text", null);
203 if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
204 /* TODO - mark Sone as bad. */
205 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));
209 PostReplyBuilder postReplyBuilder = core.postReplyBuilder();
210 /* TODO - parse time correctly. */
211 postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
212 replies.add(postReplyBuilder.build());
213 } catch (NumberFormatException nfe1) {
214 /* TODO - mark Sone as bad. */
215 logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
221 /* parse liked post IDs. */
222 SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
223 Set<String> likedPostIds = new HashSet<String>();
224 if (likePostIdsXml == null) {
225 /* TODO - mark Sone as bad. */
226 logger.log(Level.WARNING, String.format("Downloaded Sone %s has no post likes!", sone));
228 for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) {
229 String postId = likedPostIdXml.getValue();
230 likedPostIds.add(postId);
234 /* parse liked reply IDs. */
235 SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes");
236 Set<String> likedReplyIds = new HashSet<String>();
237 if (likeReplyIdsXml == null) {
238 /* TODO - mark Sone as bad. */
239 logger.log(Level.WARNING, String.format("Downloaded Sone %s has no reply likes!", sone));
241 for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) {
242 String replyId = likedReplyIdXml.getValue();
243 likedReplyIds.add(replyId);
248 SimpleXML albumsXml = soneXml.getNode("albums");
249 Map<String, Image> allImages = new HashMap<String, Image>();
250 List<Album> topLevelAlbums = new ArrayList<Album>();
251 if (albumsXml != null) {
252 for (SimpleXML albumXml : albumsXml.getNodes("album")) {
253 String id = albumXml.getValue("id", null);
254 String parentId = albumXml.getValue("parent", null);
255 String title = albumXml.getValue("title", null);
256 String description = albumXml.getValue("description", "");
257 String albumImageId = albumXml.getValue("album-image", null);
258 if ((id == null) || (title == null)) {
259 logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
263 if (parentId != null) {
264 parent = core.getAlbum(parentId);
265 if (parent == null) {
266 logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
270 Album album = core.albumBuilder()
276 .setDescription(description)
278 if (parent != null) {
279 parent.addAlbum(album);
281 topLevelAlbums.add(album);
283 SimpleXML imagesXml = albumXml.getNode("images");
284 if (imagesXml != null) {
285 for (SimpleXML imageXml : imagesXml.getNodes("image")) {
286 String imageId = imageXml.getValue("id", null);
287 String imageCreationTimeString = imageXml.getValue("creation-time", null);
288 String imageKey = imageXml.getValue("key", null);
289 String imageTitle = imageXml.getValue("title", null);
290 String imageDescription = imageXml.getValue("description", "");
291 String imageWidthString = imageXml.getValue("width", null);
292 String imageHeightString = imageXml.getValue("height", null);
293 if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) {
294 logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone));
297 long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L);
298 int imageWidth = Numbers.safeParseInteger(imageWidthString, 0);
299 int imageHeight = Numbers.safeParseInteger(imageHeightString, 0);
300 if ((imageWidth < 1) || (imageHeight < 1)) {
301 logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
304 Image image = core.imageBuilder().withId(imageId).build().modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update();
305 image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
306 image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update();
307 album.addImage(image);
308 allImages.put(imageId, image);
311 album.modify().setAlbumImage(albumImageId).update();
315 /* process avatar. */
316 if (avatarId != null) {
317 profile.setAvatar(allImages.get(avatarId));
320 /* okay, apparently everything was parsed correctly. Now import. */
321 /* atomic setter operation on the Sone. */
322 synchronized (sone) {
323 sone.setProfile(profile);
324 sone.setPosts(posts);
325 sone.setReplies(replies);
326 sone.setLikePostIds(likedPostIds);
327 sone.setLikeReplyIds(likedReplyIds);
328 for (Album album : topLevelAlbums) {
329 sone.getRootAlbum().addAlbum(album);