From e8727d163ca9e91ed3ec00d814a5fb42b130b9d9 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Sun, 13 Feb 2022 12:33:02 +0000 Subject: [PATCH] Add initial version of serialisation to Song and Format. --- nb-configuration.xml | 18 ++++++++ src/main/java/moe/nekojimi/chords/Format.java | 22 ++++++++++ src/main/java/moe/nekojimi/chords/Song.java | 44 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 nb-configuration.xml diff --git a/nb-configuration.xml b/nb-configuration.xml new file mode 100644 index 0000000..98cd079 --- /dev/null +++ b/nb-configuration.xml @@ -0,0 +1,18 @@ + + + + + + gpl30 + + diff --git a/src/main/java/moe/nekojimi/chords/Format.java b/src/main/java/moe/nekojimi/chords/Format.java index 81a3808..47ec8d7 100644 --- a/src/main/java/moe/nekojimi/chords/Format.java +++ b/src/main/java/moe/nekojimi/chords/Format.java @@ -5,6 +5,8 @@ */ package moe.nekojimi.chords; +import com.amihaiemil.eoyaml.Yaml; +import com.amihaiemil.eoyaml.YamlMapping; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -31,6 +33,26 @@ class Format this.note = note; } + public static Format fromYaml(YamlMapping yaml) + { + Format format = new Format( + yaml.string("code"), + yaml.string("extension"), + yaml.string("resolution"), + yaml.string("note")); + return format; + } + + public YamlMapping toYaml() + { + return Yaml.createYamlMappingBuilder() + .add("code", code) + .add("extension", extension) + .add("resolution", resolution) + .add("note", note) + .build(); + } + public boolean isAudioOnly() { return resolution.trim().toLowerCase().contains("audio only"); diff --git a/src/main/java/moe/nekojimi/chords/Song.java b/src/main/java/moe/nekojimi/chords/Song.java index f9dd140..0eb2299 100644 --- a/src/main/java/moe/nekojimi/chords/Song.java +++ b/src/main/java/moe/nekojimi/chords/Song.java @@ -5,7 +5,12 @@ */ package moe.nekojimi.chords; +import com.amihaiemil.eoyaml.Yaml; +import com.amihaiemil.eoyaml.YamlMapping; +import com.amihaiemil.eoyaml.YamlSequence; +import com.amihaiemil.eoyaml.YamlSequenceBuilder; import java.io.File; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -32,6 +37,44 @@ public class Song this.url = url; } + public YamlMapping toYaml() + { + YamlSequenceBuilder build = Yaml.createYamlSequenceBuilder(); + + for (Format format : formats) + build = build.add(format.toYaml()); + + return Yaml.createYamlMappingBuilder() + .add("title", title) + .add("url", url.toExternalForm()) + .add("location", location.getAbsolutePath()) + .add("num", Integer.toString(number)) + .add("formats", build.build()) + .add("requestedBy", requestedBy) + .add("requestedIn", requestedIn) + .add("kept", Boolean.toString(kept)) + .build(); + } + + public static Song fromYaml(YamlMapping map) throws MalformedURLException + { + Song song = new Song(new URL(map.string("url"))); + song.setArtist(map.string("artist")); + song.setLocation(new File(map.string("location"))); + song.setNumber(map.integer("num")); + song.setKept(Boolean.parseBoolean(map.string("kept"))); + song.setRequestedBy(map.string("requestedBy")); + song.setRequestedIn(map.string("requestedIn")); + + List formats = new ArrayList<>(); + YamlSequence formatSeq = map.yamlSequence("formats"); + for (int i = 0; i < formats.size(); i++) + formats.add(Format.fromYaml(formatSeq.yamlMapping(i))); + song.setFormats(formats); + + return song; + } + public String getTitle() { return title; @@ -148,4 +191,5 @@ public class Song return formats.get(0); } + }