|
@@ -0,0 +1,153 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.config.profiles;
|
|
24
|
+
|
|
25
|
+import com.google.common.base.Splitter;
|
|
26
|
+
|
|
27
|
+import java.io.IOException;
|
|
28
|
+import java.io.InputStream;
|
|
29
|
+import java.io.InputStreamReader;
|
|
30
|
+import java.io.OutputStream;
|
|
31
|
+import java.io.OutputStreamWriter;
|
|
32
|
+import java.nio.file.Files;
|
|
33
|
+import java.nio.file.Path;
|
|
34
|
+import java.util.ArrayList;
|
|
35
|
+import java.util.Collection;
|
|
36
|
+import java.util.HashMap;
|
|
37
|
+import java.util.HashSet;
|
|
38
|
+import java.util.List;
|
|
39
|
+import java.util.Map;
|
|
40
|
+import java.util.Optional;
|
|
41
|
+import java.util.Set;
|
|
42
|
+
|
|
43
|
+import org.slf4j.Logger;
|
|
44
|
+import org.slf4j.LoggerFactory;
|
|
45
|
+
|
|
46
|
+import com.esotericsoftware.yamlbeans.YamlReader;
|
|
47
|
+import com.esotericsoftware.yamlbeans.YamlWriter;
|
|
48
|
+
|
|
49
|
+import static com.dmdirc.util.YamlReaderUtils.asList;
|
|
50
|
+import static com.dmdirc.util.YamlReaderUtils.asMap;
|
|
51
|
+import static com.dmdirc.util.YamlReaderUtils.optionalString;
|
|
52
|
+import static com.dmdirc.util.YamlReaderUtils.requiredString;
|
|
53
|
+
|
|
54
|
+/**
|
|
55
|
+ * Store that reads and writes profiles from a YAML file on disk.
|
|
56
|
+ */
|
|
57
|
+public class YamlProfileStore implements ProfileStore {
|
|
58
|
+
|
|
59
|
+ private static final Logger LOG = LoggerFactory.getLogger(YamlProfileStore.class);
|
|
60
|
+ /** The charset to use when reading and writing files. */
|
|
61
|
+ private static final String CHARSET = "UTF-8";
|
|
62
|
+
|
|
63
|
+ /** The path to the file to read and write auto commands in. */
|
|
64
|
+ private final Path path;
|
|
65
|
+
|
|
66
|
+ /**
|
|
67
|
+ * Creates a new YAML profiles store.
|
|
68
|
+ *
|
|
69
|
+ * @param path The path to the YAML file to read and write profiles in.
|
|
70
|
+ */
|
|
71
|
+ public YamlProfileStore(final Path path) {
|
|
72
|
+ this.path = path;
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ @Override
|
|
76
|
+ public Set<Profile> readProfiles() {
|
|
77
|
+ final Set<Profile> commands = new HashSet<>();
|
|
78
|
+ if (Files.exists(path)) {
|
|
79
|
+ try (final InputStream stream = Files.newInputStream(path);
|
|
80
|
+ final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
|
|
81
|
+ final YamlReader yamlReader = new YamlReader(reader);
|
|
82
|
+ final Object root = yamlReader.read();
|
|
83
|
+ commands.addAll(readProfilesFromYaml(asList(root)));
|
|
84
|
+ yamlReader.close();
|
|
85
|
+ } catch (IOException | IllegalArgumentException ex) {
|
|
86
|
+ LOG.warn("Unable to read auto commands", ex);
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ return commands;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ @Override
|
|
94
|
+ public void writeProfiles(final Set<Profile> profiles) {
|
|
95
|
+ final List<Object> list = getProfilesForYaml(profiles);
|
|
96
|
+ try (final OutputStream stream = Files.newOutputStream(path);
|
|
97
|
+ final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
|
|
98
|
+ final YamlWriter yamlWriter = new YamlWriter(writer);
|
|
99
|
+ yamlWriter.write(list);
|
|
100
|
+ yamlWriter.close();
|
|
101
|
+ } catch (IOException ex) {
|
|
102
|
+ LOG.error("Unable to write auto commands", ex);
|
|
103
|
+ }
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ /**
|
|
107
|
+ * Builds a set of auto commands from the given YAML representations.
|
|
108
|
+ *
|
|
109
|
+ * @param objects The raw objects read from the YAML file.
|
|
110
|
+ *
|
|
111
|
+ * @return A list of corresponding auto commands.
|
|
112
|
+ */
|
|
113
|
+ private Collection<Profile> readProfilesFromYaml(final Collection<Object> objects) {
|
|
114
|
+ final Collection<Profile> res = new ArrayList<>(objects.size());
|
|
115
|
+ for (Object profile : objects) {
|
|
116
|
+ try {
|
|
117
|
+ final Map<Object, Object> map = asMap(profile);
|
|
118
|
+ final String name = requiredString(map, "name");
|
|
119
|
+ final String realname = requiredString(map, "realname");
|
|
120
|
+ final Optional<String> ident =
|
|
121
|
+ Optional.ofNullable(optionalString(map, "ident"));
|
|
122
|
+ final List<String> nicknames =
|
|
123
|
+ Splitter.on('\n').splitToList(requiredString(map, "nicknames"));
|
|
124
|
+ res.add(new Profile(name, realname, ident, nicknames));
|
|
125
|
+ } catch (IllegalArgumentException ex) {
|
|
126
|
+ LOG.info("Unable to read alias", ex);
|
|
127
|
+ }
|
|
128
|
+ }
|
|
129
|
+ return res;
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ /**
|
|
133
|
+ * Gets a set of maps that can be written to a YAML file.
|
|
134
|
+ *
|
|
135
|
+ * @param profiles The profiles to be stored.
|
|
136
|
+ *
|
|
137
|
+ * @return A list of corresponding maps.
|
|
138
|
+ */
|
|
139
|
+ private List<Object> getProfilesForYaml(final Collection<Profile> profiles) {
|
|
140
|
+ final List<Object> res = new ArrayList<>(profiles.size());
|
|
141
|
+ for (Profile profile : profiles) {
|
|
142
|
+ final Map<Object, Object> map = new HashMap<>();
|
|
143
|
+ map.put("name", profile.getName());
|
|
144
|
+ map.put("realname", profile.getRealname());
|
|
145
|
+ if (profile.getIdent().isPresent()) {
|
|
146
|
+ map.put("ident", profile.getIdent().get());
|
|
147
|
+ }
|
|
148
|
+ map.put("nicknames", profile.getNicknames());
|
|
149
|
+ res.add(map);
|
|
150
|
+ }
|
|
151
|
+ return res;
|
|
152
|
+ }
|
|
153
|
+}
|