Store the insert URI in the information used for inserting.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneParser.java
1 /*
2  * Sone - SoneParser.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import static com.google.common.base.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Optional.of;
23
24 import java.io.InputStream;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.data.Album;
32 import net.pterodactylus.sone.data.Client;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.PostReply;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Sone;
38 import net.pterodactylus.sone.data.impl.DefaultSone;
39 import net.pterodactylus.sone.database.Database;
40 import net.pterodactylus.sone.database.ImageBuilder.ImageCreated;
41 import net.pterodactylus.sone.database.PostBuilder;
42 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
43 import net.pterodactylus.sone.database.PostReplyBuilder;
44 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
45 import net.pterodactylus.util.number.Numbers;
46 import net.pterodactylus.util.xml.SimpleXML;
47 import net.pterodactylus.util.xml.XML;
48
49 import com.google.common.base.Optional;
50 import com.google.common.collect.Maps;
51 import com.google.common.collect.Sets;
52 import com.google.common.primitives.Ints;
53 import org.w3c.dom.Document;
54
55 /**
56  * Parses the inserted XML representation of a {@link Sone} into a Sone.
57  *
58  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59  */
60 public class SoneParser {
61
62         private static final Logger logger = Logger.getLogger(SoneParser.class.getName());
63         private static final int MAX_PROTOCOL_VERSION = 0;
64
65         /**
66          * Parses a Sone from the given input stream and creates a new Sone from the
67          * parsed data.
68          *
69          * @param originalSone
70          *              The Sone to update
71          * @param soneInputStream
72          *              The input stream to parse the Sone from
73          * @return The parsed Sone
74          */
75         public Sone parseSone(Database database, Sone originalSone, InputStream soneInputStream) {
76                 /* TODO - impose a size limit? */
77
78                 Document document;
79                 /* XML parsing is not thread-safe. */
80                 synchronized (this) {
81                         document = XML.transformToDocument(soneInputStream);
82                 }
83                 if (document == null) {
84                         /* TODO - mark Sone as bad. */
85                         logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone.getId()));
86                         throw new InvalidXml();
87                 }
88
89                 SimpleXML soneXml = SimpleXML.fromDocument(document);
90                 Optional<Client> parsedClient = parseClient(originalSone, soneXml);
91                 Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient()));
92
93                 verifyProtocolVersion(soneXml);
94
95                 parseTime(soneXml, sone);
96
97                 SimpleXML profileXml = soneXml.getNode("profile");
98                 if (profileXml == null) {
99                         /* TODO - mark Sone as bad. */
100                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone));
101                         throw new MalformedXml();
102                 }
103
104                 /* parse albums. */
105                 SimpleXML albumsXml = soneXml.getNode("albums");
106                 Map<String, Album> albums = Maps.newHashMap();
107                 Set<String> images = Sets.newHashSet();
108                 if (albumsXml != null) {
109                         for (SimpleXML albumXml : albumsXml.getNodes("album")) {
110                                 String id = albumXml.getValue("id", null);
111                                 String parentId = albumXml.getValue("parent", null);
112                                 String title = albumXml.getValue("title", null);
113                                 String description = albumXml.getValue("description", "");
114                                 String albumImageId = albumXml.getValue("album-image", null);
115                                 if ((id == null) || (title == null) || (description == null)) {
116                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
117                                         throw new MalformedXml();
118                                 }
119                                 Album parent = sone.getRootAlbum();
120                                 if (parentId != null) {
121                                         parent = albums.get(parentId);
122                                         if (parent == null) {
123                                                 logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
124                                                 throw new InvalidParentAlbum();
125                                         }
126                                 }
127                                 Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update();
128                                 albums.put(album.getId(), album);
129                                 SimpleXML imagesXml = albumXml.getNode("images");
130                                 if (imagesXml != null) {
131                                         for (SimpleXML imageXml : imagesXml.getNodes("image")) {
132                                                 String imageId = imageXml.getValue("id", null);
133                                                 String imageCreationTimeString = imageXml.getValue("creation-time", null);
134                                                 String imageKey = imageXml.getValue("key", null);
135                                                 String imageTitle = imageXml.getValue("title", null);
136                                                 String imageDescription = imageXml.getValue("description", "");
137                                                 String imageWidthString = imageXml.getValue("width", null);
138                                                 String imageHeightString = imageXml.getValue("height", null);
139                                                 if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) {
140                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone));
141                                                         throw new MalformedXml();
142                                                 }
143                                                 long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L);
144                                                 int imageWidth = Numbers.safeParseInteger(imageWidthString, 0);
145                                                 int imageHeight = Numbers.safeParseInteger(imageHeightString, 0);
146                                                 if ((imageWidth < 1) || (imageHeight < 1)) {
147                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
148                                                         throw new MalformedDimension();
149                                                 }
150                                                 Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.<ImageCreated>absent());
151                                                 image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
152                                                 images.add(imageId);
153                                         }
154                                 }
155                                 album.modify().setAlbumImage(albumImageId).update();
156                         }
157                 }
158
159                 /* parse profile. */
160                 String profileFirstName = profileXml.getValue("first-name", null);
161                 String profileMiddleName = profileXml.getValue("middle-name", null);
162                 String profileLastName = profileXml.getValue("last-name", null);
163                 Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
164                 Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
165                 Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
166                 Profile profile = new Profile(sone).modify().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName).update();
167                 profile.modify().setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear).update();
168
169                 /* avatar is processed after images are loaded. */
170                 String avatarId = profileXml.getValue("avatar", null);
171                 if ((avatarId != null) && !images.contains(avatarId)) {
172                         throw new InvalidAvatarId();
173                 }
174                 profile.setAvatar(fromNullable(avatarId));
175
176                 /* parse profile fields. */
177                 SimpleXML profileFieldsXml = profileXml.getNode("fields");
178                 if (profileFieldsXml != null) {
179                         for (SimpleXML fieldXml : profileFieldsXml.getNodes("field")) {
180                                 String fieldName = fieldXml.getValue("field-name", null);
181                                 String fieldValue = fieldXml.getValue("field-value", "");
182                                 if (fieldName == null) {
183                                         logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", sone, fieldName, fieldValue));
184                                         throw new MalformedXml();
185                                 }
186                                 try {
187                                         profile.setField(profile.addField(fieldName), fieldValue);
188                                 } catch (IllegalArgumentException iae1) {
189                                         logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
190                                         throw new DuplicateField();
191                                 }
192                         }
193                 }
194
195                 /* parse posts. */
196                 SimpleXML postsXml = soneXml.getNode("posts");
197                 Set<Post> posts = new HashSet<Post>();
198                 if (postsXml == null) {
199                         /* TODO - mark Sone as bad. */
200                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone));
201                 } else {
202                         for (SimpleXML postXml : postsXml.getNodes("post")) {
203                                 String postId = postXml.getValue("id", null);
204                                 String postRecipientId = postXml.getValue("recipient", null);
205                                 String postTime = postXml.getValue("time", null);
206                                 String postText = postXml.getValue("text", null);
207                                 if ((postId == null) || (postTime == null) || (postText == null)) {
208                                         /* TODO - mark Sone as bad. */
209                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", sone, postId, postTime, postText));
210                                         throw new MalformedXml();
211                                 }
212                                 try {
213                                         PostBuilder postBuilder = sone.newPostBuilder();
214                                         /* TODO - parse time correctly. */
215                                         postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
216                                         if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
217                                                 postBuilder.to(of(postRecipientId));
218                                         }
219                                         posts.add(postBuilder.build(Optional.<PostCreated>absent()));
220                                 } catch (NumberFormatException nfe1) {
221                                         /* TODO - mark Sone as bad. */
222                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime));
223                                         throw new MalformedTime();
224                                 }
225                         }
226                 }
227
228                 /* parse replies. */
229                 SimpleXML repliesXml = soneXml.getNode("replies");
230                 Set<PostReply> replies = new HashSet<PostReply>();
231                 if (repliesXml == null) {
232                         /* TODO - mark Sone as bad. */
233                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
234                 } else {
235                         for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
236                                 String replyId = replyXml.getValue("id", null);
237                                 String replyPostId = replyXml.getValue("post-id", null);
238                                 String replyTime = replyXml.getValue("time", null);
239                                 String replyText = replyXml.getValue("text", null);
240                                 if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
241                                         /* TODO - mark Sone as bad. */
242                                         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));
243                                         throw new MalformedXml();
244                                 }
245                                 try {
246                                         /* TODO - parse time correctly. */
247                                         PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(replyPostId).withId(replyId).withTime(Long.parseLong(replyTime)).withText(replyText);
248                                         replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
249                                 } catch (NumberFormatException nfe1) {
250                                         /* TODO - mark Sone as bad. */
251                                         logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
252                                         throw new MalformedTime();
253                                 }
254                         }
255                 }
256
257                 /* parse liked post IDs. */
258                 SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
259                 Set<String> likedPostIds = new HashSet<String>();
260                 if (likePostIdsXml == null) {
261                         /* TODO - mark Sone as bad. */
262                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no post likes!", sone));
263                 } else {
264                         for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) {
265                                 String postId = likedPostIdXml.getValue();
266                                 likedPostIds.add(postId);
267                         }
268                 }
269
270                 /* parse liked reply IDs. */
271                 SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes");
272                 Set<String> likedReplyIds = new HashSet<String>();
273                 if (likeReplyIdsXml == null) {
274                         /* TODO - mark Sone as bad. */
275                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no reply likes!", sone));
276                 } else {
277                         for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) {
278                                 String replyId = likedReplyIdXml.getValue();
279                                 likedReplyIds.add(replyId);
280                         }
281                 }
282
283                 /* okay, apparently everything was parsed correctly. Now import. */
284                 sone.setProfile(profile);
285                 sone.setPosts(posts);
286                 sone.setReplies(replies);
287                 sone.setLikePostIds(likedPostIds);
288                 sone.setLikeReplyIds(likedReplyIds);
289
290                 return sone;
291         }
292
293         private void parseTime(SimpleXML soneXml, Sone sone) {
294                 String soneTime = soneXml.getValue("time", null);
295                 if (soneTime == null) {
296                         /* TODO - mark Sone as bad. */
297                         logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
298                         throw new MalformedXml();
299                 }
300                 try {
301                         sone.setTime(Long.parseLong(soneTime));
302                 } catch (NumberFormatException nfe1) {
303                         /* TODO - mark Sone as bad. */
304                         logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
305                         throw new MalformedTime();
306                 }
307         }
308
309         private void verifyProtocolVersion(SimpleXML soneXml) {
310                 Optional<Integer> protocolVersion = parseProtocolVersion(soneXml);
311                 if (protocolVersion.isPresent()) {
312                         if (protocolVersion.get() < 0) {
313                                 logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion.get()));
314                                 throw new InvalidProtocolVersion();
315                         }
316                         if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
317                                 logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
318                                 throw new SoneTooNew();
319                         }
320                 }
321         }
322
323         private Optional<Integer> parseProtocolVersion(SimpleXML soneXml) {
324                 String soneProtocolVersion = soneXml.getValue("protocol-version", null);
325                 if (soneProtocolVersion == null) {
326                         logger.log(Level.INFO, "No protocol version found, assuming 0.");
327                         return absent();
328                 }
329                 return fromNullable(Ints.tryParse(soneProtocolVersion));
330         }
331
332         private Optional<Client> parseClient(Sone sone, SimpleXML soneXml) {
333                 SimpleXML clientXml = soneXml.getNode("client");
334                 if (clientXml == null) {
335                         return absent();
336                 }
337                 String clientName = clientXml.getValue("name", null);
338                 String clientVersion = clientXml.getValue("version", null);
339                 if ((clientName == null) || (clientVersion == null)) {
340                         logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone));
341                         return absent();
342                 }
343                 return of(new Client(clientName, clientVersion));
344         }
345
346         public static class InvalidXml extends RuntimeException {
347
348         }
349
350         public static class InvalidProtocolVersion extends RuntimeException {
351
352         }
353
354         public static class SoneTooNew extends RuntimeException {
355
356         }
357
358         public static class MalformedXml extends RuntimeException {
359
360         }
361
362         public static class InvalidAvatarId extends RuntimeException {
363
364         }
365
366         public static class DuplicateField extends RuntimeException {
367
368         }
369
370         public static class MalformedTime extends RuntimeException {
371
372         }
373
374         public static class InvalidParentAlbum extends RuntimeException {
375
376         }
377
378         public static class MalformedDimension extends RuntimeException {
379
380         }
381
382 }