Move parsing of Sone XML into its own method.
[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.ImageBuilder.ImageCreated;
40 import net.pterodactylus.sone.database.PostBuilder;
41 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
42 import net.pterodactylus.sone.database.PostReplyBuilder;
43 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
44 import net.pterodactylus.sone.database.memory.MemoryDatabase;
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 org.w3c.dom.Document;
52
53 /**
54  * Parses the inserted XML representation of a {@link Sone} into a Sone.
55  *
56  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57  */
58 public class SoneParser {
59
60         private static final Logger logger = Logger.getLogger(SoneParser.class.getName());
61         private static final int MAX_PROTOCOL_VERSION = 0;
62         private final Core core;
63
64         public SoneParser(Core core) {
65                 this.core = core;
66         }
67
68         /**
69          * Parses a Sone from the given input stream and creates a new Sone from the
70          * parsed data.
71          *
72          * @param originalSone
73          *              The Sone to update
74          * @param soneInputStream
75          *              The input stream to parse the Sone from
76          * @return The parsed Sone
77          * @throws SoneException
78          *              if a parse error occurs, or the protocol is invalid
79          */
80         public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException {
81                 /* TODO - impose a size limit? */
82
83                 Document document;
84                 /* XML parsing is not thread-safe. */
85                 synchronized (this) {
86                         document = XML.transformToDocument(soneInputStream);
87                 }
88                 if (document == null) {
89                         /* TODO - mark Sone as bad. */
90                         logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone));
91                         return null;
92                 }
93
94                 Optional<SimpleXML> soneXml = parseXml(originalSone, document);
95                 if (!soneXml.isPresent()) {
96                         logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone));
97                         return null;
98                 }
99
100                 Optional<Client> parsedClient = parseClient(originalSone, soneXml.get());
101                 Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient()));
102
103                 Integer protocolVersion = null;
104                 String soneProtocolVersion = soneXml.get().getValue("protocol-version", null);
105                 if (soneProtocolVersion != null) {
106                         protocolVersion = Numbers.safeParseInteger(soneProtocolVersion);
107                 }
108                 if (protocolVersion == null) {
109                         logger.log(Level.INFO, "No protocol version found, assuming 0.");
110                         protocolVersion = 0;
111                 }
112
113                 if (protocolVersion < 0) {
114                         logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion));
115                         return null;
116                 }
117
118                 /* check for valid versions. */
119                 if (protocolVersion > MAX_PROTOCOL_VERSION) {
120                         logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion));
121                         return null;
122                 }
123
124                 String soneTime = soneXml.get().getValue("time", null);
125                 if (soneTime == null) {
126                         /* TODO - mark Sone as bad. */
127                         logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
128                         return null;
129                 }
130                 try {
131                         sone.setTime(Long.parseLong(soneTime));
132                 } catch (NumberFormatException nfe1) {
133                         /* TODO - mark Sone as bad. */
134                         logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
135                         return null;
136                 }
137
138                 SimpleXML profileXml = soneXml.get().getNode("profile");
139                 if (profileXml == null) {
140                         /* TODO - mark Sone as bad. */
141                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone));
142                         return null;
143                 }
144
145                 /* parse profile. */
146                 String profileFirstName = profileXml.getValue("first-name", null);
147                 String profileMiddleName = profileXml.getValue("middle-name", null);
148                 String profileLastName = profileXml.getValue("last-name", null);
149                 Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
150                 Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
151                 Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
152                 Profile profile = new Profile(sone).modify().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName).update();
153                 profile.modify().setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear).update();
154                 /* avatar is processed after images are loaded. */
155                 String avatarId = profileXml.getValue("avatar", null);
156
157                 /* parse profile fields. */
158                 SimpleXML profileFieldsXml = profileXml.getNode("fields");
159                 if (profileFieldsXml != null) {
160                         for (SimpleXML fieldXml : profileFieldsXml.getNodes("field")) {
161                                 String fieldName = fieldXml.getValue("field-name", null);
162                                 String fieldValue = fieldXml.getValue("field-value", "");
163                                 if (fieldName == null) {
164                                         logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", sone, fieldName, fieldValue));
165                                         return null;
166                                 }
167                                 try {
168                                         profile.addField(fieldName).setValue(fieldValue);
169                                 } catch (IllegalArgumentException iae1) {
170                                         logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
171                                         return null;
172                                 }
173                         }
174                 }
175
176                 /* parse posts. */
177                 SimpleXML postsXml = soneXml.get().getNode("posts");
178                 Set<Post> posts = new HashSet<Post>();
179                 if (postsXml == null) {
180                         /* TODO - mark Sone as bad. */
181                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone));
182                 } else {
183                         for (SimpleXML postXml : postsXml.getNodes("post")) {
184                                 String postId = postXml.getValue("id", null);
185                                 String postRecipientId = postXml.getValue("recipient", null);
186                                 String postTime = postXml.getValue("time", null);
187                                 String postText = postXml.getValue("text", null);
188                                 if ((postId == null) || (postTime == null) || (postText == null)) {
189                                         /* TODO - mark Sone as bad. */
190                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", sone, postId, postTime, postText));
191                                         return null;
192                                 }
193                                 try {
194                                         PostBuilder postBuilder = sone.newPostBuilder();
195                                         /* TODO - parse time correctly. */
196                                         postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
197                                         if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
198                                                 postBuilder.to(of(postRecipientId));
199                                         }
200                                         posts.add(postBuilder.build(Optional.<PostCreated>absent()));
201                                 } catch (NumberFormatException nfe1) {
202                                         /* TODO - mark Sone as bad. */
203                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime));
204                                         return null;
205                                 }
206                         }
207                 }
208
209                 /* parse replies. */
210                 SimpleXML repliesXml = soneXml.get().getNode("replies");
211                 Set<PostReply> replies = new HashSet<PostReply>();
212                 if (repliesXml == null) {
213                         /* TODO - mark Sone as bad. */
214                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
215                 } else {
216                         for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
217                                 String replyId = replyXml.getValue("id", null);
218                                 String replyPostId = replyXml.getValue("post-id", null);
219                                 String replyTime = replyXml.getValue("time", null);
220                                 String replyText = replyXml.getValue("text", null);
221                                 if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
222                                         /* TODO - mark Sone as bad. */
223                                         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));
224                                         return null;
225                                 }
226                                 try {
227                                         /* TODO - parse time correctly. */
228                                         PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(replyPostId).withId(replyId).withTime(Long.parseLong(replyTime)).withText(replyText);
229                                         replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
230                                 } catch (NumberFormatException nfe1) {
231                                         /* TODO - mark Sone as bad. */
232                                         logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
233                                         return null;
234                                 }
235                         }
236                 }
237
238                 /* parse liked post IDs. */
239                 SimpleXML likePostIdsXml = soneXml.get().getNode("post-likes");
240                 Set<String> likedPostIds = new HashSet<String>();
241                 if (likePostIdsXml == null) {
242                         /* TODO - mark Sone as bad. */
243                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no post likes!", sone));
244                 } else {
245                         for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) {
246                                 String postId = likedPostIdXml.getValue();
247                                 likedPostIds.add(postId);
248                         }
249                 }
250
251                 /* parse liked reply IDs. */
252                 SimpleXML likeReplyIdsXml = soneXml.get().getNode("reply-likes");
253                 Set<String> likedReplyIds = new HashSet<String>();
254                 if (likeReplyIdsXml == null) {
255                         /* TODO - mark Sone as bad. */
256                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no reply likes!", sone));
257                 } else {
258                         for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) {
259                                 String replyId = likedReplyIdXml.getValue();
260                                 likedReplyIds.add(replyId);
261                         }
262                 }
263
264                 /* parse albums. */
265                 SimpleXML albumsXml = soneXml.get().getNode("albums");
266                 Map<String, Album> albums = Maps.newHashMap();
267                 if (albumsXml != null) {
268                         for (SimpleXML albumXml : albumsXml.getNodes("album")) {
269                                 String id = albumXml.getValue("id", null);
270                                 String parentId = albumXml.getValue("parent", null);
271                                 String title = albumXml.getValue("title", null);
272                                 String description = albumXml.getValue("description", "");
273                                 String albumImageId = albumXml.getValue("album-image", null);
274                                 if ((id == null) || (title == null) || (description == null)) {
275                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
276                                         return null;
277                                 }
278                                 Album parent = sone.getRootAlbum();
279                                 if (parentId != null) {
280                                         parent = albums.get(parentId);
281                                         if (parent == null) {
282                                                 logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
283                                                 return null;
284                                         }
285                                 }
286                                 Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update();
287                                 albums.put(album.getId(), album);
288                                 SimpleXML imagesXml = albumXml.getNode("images");
289                                 if (imagesXml != null) {
290                                         for (SimpleXML imageXml : imagesXml.getNodes("image")) {
291                                                 String imageId = imageXml.getValue("id", null);
292                                                 String imageCreationTimeString = imageXml.getValue("creation-time", null);
293                                                 String imageKey = imageXml.getValue("key", null);
294                                                 String imageTitle = imageXml.getValue("title", null);
295                                                 String imageDescription = imageXml.getValue("description", "");
296                                                 String imageWidthString = imageXml.getValue("width", null);
297                                                 String imageHeightString = imageXml.getValue("height", null);
298                                                 if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) {
299                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone));
300                                                         return null;
301                                                 }
302                                                 long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L);
303                                                 int imageWidth = Numbers.safeParseInteger(imageWidthString, 0);
304                                                 int imageHeight = Numbers.safeParseInteger(imageHeightString, 0);
305                                                 if ((imageWidth < 1) || (imageHeight < 1)) {
306                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
307                                                         return null;
308                                                 }
309                                                 Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.<ImageCreated>absent());
310                                                 image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
311                                         }
312                                 }
313                                 album.modify().setAlbumImage(albumImageId).update();
314                         }
315                 }
316
317                 /* process avatar. */
318                 if (avatarId != null) {
319                         profile.setAvatar(core.getImage(avatarId).orNull());
320                 }
321
322                 /* okay, apparently everything was parsed correctly. Now import. */
323                 sone.setProfile(profile);
324                 sone.setPosts(posts);
325                 sone.setReplies(replies);
326                 sone.setLikePostIds(likedPostIds);
327                 sone.setLikeReplyIds(likedReplyIds);
328
329                 return sone;
330         }
331
332         private Optional<SimpleXML> parseXml(Sone originalSone, Document document) {
333                 try {
334                         return fromNullable(SimpleXML.fromDocument(document));
335                 } catch (NullPointerException npe1) {
336                         /* for some reason, invalid XML can cause NPEs. */
337                         return absent();
338                 }
339         }
340
341         private Optional<Client> parseClient(Sone sone, SimpleXML soneXml) {
342                 SimpleXML clientXml = soneXml.getNode("client");
343                 if (clientXml == null) {
344                         return absent();
345                 }
346                 String clientName = clientXml.getValue("name", null);
347                 String clientVersion = clientXml.getValue("version", null);
348                 if ((clientName == null) || (clientVersion == null)) {
349                         logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone));
350                         return null;
351                 }
352                 return of(new Client(clientName, clientVersion));
353         }
354
355 }