X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParser.java;h=8d4883dab107b0cf50452c690af5024d51de180c;hb=49ff797a72d192c6772cd3f9e201beb8146d2462;hp=2e8299f36084d43d21d57c5d150296c87f173b01;hpb=c1a038857688b13861214d8d08a29363cc5366df;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 2e8299f..8d4883d 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -17,8 +17,11 @@ package net.pterodactylus.sone.core; +import static com.google.common.base.Optional.absent; +import static com.google.common.base.Optional.fromNullable; +import static com.google.common.base.Optional.of; + import java.io.InputStream; -import java.net.MalformedURLException; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -43,8 +46,6 @@ import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; -import freenet.keys.FreenetURI; - import com.google.common.base.Optional; import com.google.common.collect.Maps; import org.w3c.dom.Document; @@ -90,19 +91,17 @@ public class SoneParser { return null; } - Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity()); - - SimpleXML soneXml; - try { - soneXml = SimpleXML.fromDocument(document); - } catch (NullPointerException npe1) { - /* for some reason, invalid XML can cause NPEs. */ - logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", sone), npe1); + Optional soneXml = parseXml(originalSone, document); + if (!soneXml.isPresent()) { + logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone)); return null; } + Optional parsedClient = parseClient(originalSone, soneXml.get()); + Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient())); + Integer protocolVersion = null; - String soneProtocolVersion = soneXml.getValue("protocol-version", null); + String soneProtocolVersion = soneXml.get().getValue("protocol-version", null); if (soneProtocolVersion != null) { protocolVersion = Numbers.safeParseInteger(soneProtocolVersion); } @@ -122,7 +121,7 @@ public class SoneParser { return null; } - String soneTime = soneXml.getValue("time", null); + String soneTime = soneXml.get().getValue("time", null); if (soneTime == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone)); @@ -136,33 +135,7 @@ public class SoneParser { return null; } - SimpleXML clientXml = soneXml.getNode("client"); - if (clientXml != null) { - String clientName = clientXml.getValue("name", null); - String clientVersion = clientXml.getValue("version", null); - if ((clientName == null) || (clientVersion == null)) { - logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone)); - return null; - } - sone.setClient(new Client(clientName, clientVersion)); - } - - String soneRequestUri = soneXml.getValue("request-uri", null); - if (soneRequestUri != null) { - try { - sone.setRequestUri(new FreenetURI(soneRequestUri)); - } catch (MalformedURLException mue1) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded Sone %s has invalid request URI: %s", sone, soneRequestUri), mue1); - return null; - } - } - - if (originalSone.getInsertUri() != null) { - sone.setInsertUri(originalSone.getInsertUri()); - } - - SimpleXML profileXml = soneXml.getNode("profile"); + SimpleXML profileXml = soneXml.get().getNode("profile"); if (profileXml == null) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone)); @@ -177,7 +150,7 @@ public class SoneParser { Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null)); Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null)); Profile profile = new Profile(sone).modify().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName).update(); - profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear); + profile.modify().setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear).update(); /* avatar is processed after images are loaded. */ String avatarId = profileXml.getValue("avatar", null); @@ -201,7 +174,7 @@ public class SoneParser { } /* parse posts. */ - SimpleXML postsXml = soneXml.getNode("posts"); + SimpleXML postsXml = soneXml.get().getNode("posts"); Set posts = new HashSet(); if (postsXml == null) { /* TODO - mark Sone as bad. */ @@ -222,7 +195,7 @@ public class SoneParser { /* TODO - parse time correctly. */ postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { - postBuilder.to(Optional.of(postRecipientId)); + postBuilder.to(of(postRecipientId)); } posts.add(postBuilder.build(Optional.absent())); } catch (NumberFormatException nfe1) { @@ -234,7 +207,7 @@ public class SoneParser { } /* parse replies. */ - SimpleXML repliesXml = soneXml.getNode("replies"); + SimpleXML repliesXml = soneXml.get().getNode("replies"); Set replies = new HashSet(); if (repliesXml == null) { /* TODO - mark Sone as bad. */ @@ -263,7 +236,7 @@ public class SoneParser { } /* parse liked post IDs. */ - SimpleXML likePostIdsXml = soneXml.getNode("post-likes"); + SimpleXML likePostIdsXml = soneXml.get().getNode("post-likes"); Set likedPostIds = new HashSet(); if (likePostIdsXml == null) { /* TODO - mark Sone as bad. */ @@ -276,7 +249,7 @@ public class SoneParser { } /* parse liked reply IDs. */ - SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes"); + SimpleXML likeReplyIdsXml = soneXml.get().getNode("reply-likes"); Set likedReplyIds = new HashSet(); if (likeReplyIdsXml == null) { /* TODO - mark Sone as bad. */ @@ -289,7 +262,7 @@ public class SoneParser { } /* parse albums. */ - SimpleXML albumsXml = soneXml.getNode("albums"); + SimpleXML albumsXml = soneXml.get().getNode("albums"); Map albums = Maps.newHashMap(); if (albumsXml != null) { for (SimpleXML albumXml : albumsXml.getNodes("album")) { @@ -355,4 +328,28 @@ public class SoneParser { return sone; } + + private Optional parseXml(Sone originalSone, Document document) { + try { + return fromNullable(SimpleXML.fromDocument(document)); + } catch (NullPointerException npe1) { + /* for some reason, invalid XML can cause NPEs. */ + return absent(); + } + } + + private Optional parseClient(Sone sone, SimpleXML soneXml) { + SimpleXML clientXml = soneXml.getNode("client"); + if (clientXml == null) { + return absent(); + } + String clientName = clientXml.getValue("name", null); + String clientVersion = clientXml.getValue("version", null); + if ((clientName == null) || (clientVersion == null)) { + logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone)); + return null; + } + return of(new Client(clientName, clientVersion)); + } + }