X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FConfigurationSoneParser.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FConfigurationSoneParser.java;h=8f09b3b5ee5d5c329a56ebba8b79c8d8bdcbc0d7;hb=11f3457415c9122b5d11840d24186971af28add9;hp=0000000000000000000000000000000000000000;hpb=791ae7c17d02857541e10a5dd97af65f6f532056;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java new file mode 100644 index 0000000..8f09b3b --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java @@ -0,0 +1,111 @@ +package net.pterodactylus.sone.core; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import javax.annotation.Nullable; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.PostBuilderFactory; +import net.pterodactylus.util.config.Configuration; + +/** + * Parses a {@link Sone}’s data from a {@link Configuration}. + * + * @author David ‘Bombe’ Roden + */ +public class ConfigurationSoneParser { + + private final Configuration configuration; + private final Sone sone; + private final String sonePrefix; + + public ConfigurationSoneParser(Configuration configuration, Sone sone) { + this.configuration = configuration; + this.sone = sone; + sonePrefix = "Sone/" + sone.getId(); + } + + public Profile parseProfile() { + Profile profile = new Profile(sone); + profile.setFirstName(getString("/Profile/FirstName", null)); + profile.setMiddleName(getString("/Profile/MiddleName", null)); + profile.setLastName(getString("/Profile/LastName", null)); + profile.setBirthDay(getInt("/Profile/BirthDay", null)); + profile.setBirthMonth(getInt("/Profile/BirthMonth", null)); + profile.setBirthYear(getInt("/Profile/BirthYear", null)); + + /* load profile fields. */ + int fieldCount = 0; + while (true) { + String fieldPrefix = "/Profile/Fields/" + fieldCount++; + String fieldName = getString(fieldPrefix + "/Name", null); + if (fieldName == null) { + break; + } + String fieldValue = getString(fieldPrefix + "/Value", ""); + profile.addField(fieldName).setValue(fieldValue); + } + + return profile; + } + + private String getString(String nodeName, @Nullable String defaultValue) { + return configuration.getStringValue(sonePrefix + nodeName) + .getValue(defaultValue); + } + + private Integer getInt(String nodeName, @Nullable Integer defaultValue) { + return configuration.getIntValue(sonePrefix + nodeName) + .getValue(defaultValue); + } + + private Long getLong(String nodeName, @Nullable Long defaultValue) { + return configuration.getLongValue(sonePrefix + nodeName) + .getValue(defaultValue); + } + + public Collection parsePosts(PostBuilderFactory postBuilderFactory) + throws InvalidPostFound { + Set posts = new HashSet(); + while (true) { + String postPrefix = "/Posts/" + posts.size(); + String postId = getString(postPrefix + "/ID", null); + if (postId == null) { + break; + } + long postTime = getLong(postPrefix + "/Time", 0L); + String postText = getString(postPrefix + "/Text", null); + if (postAttributesAreInvalid(postTime, postText)) { + throw new InvalidPostFound(); + } + PostBuilder postBuilder = postBuilderFactory.newPostBuilder() + .withId(postId) + .from(sone.getId()) + .withTime(postTime) + .withText(postText); + String postRecipientId = + getString(postPrefix + "/Recipient", null); + if (postRecipientIsValid(postRecipientId)) { + postBuilder.to(postRecipientId); + } + posts.add(postBuilder.build()); + } + return posts; + } + + private boolean postAttributesAreInvalid(long postTime, String postText) { + return (postTime == 0) || (postText == null); + } + + private boolean postRecipientIsValid(String postRecipientId) { + return (postRecipientId != null) && (postRecipientId.length() == 43); + } + + public static class InvalidPostFound extends RuntimeException { } + +}