Browse Source

Add a store that reads/writes aliases.

Change-Id: I24afd60eecf5d77e8ecaf23de584243806df8fdd
Reviewed-on: http://gerrit.dmdirc.com/3519
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 10 years ago
parent
commit
c2edb71dc6

+ 46
- 0
src/com/dmdirc/commandparser/aliases/AliasStore.java View File

@@ -0,0 +1,46 @@
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.commandparser.aliases;
24
+
25
+import java.util.Set;
26
+
27
+/**
28
+ * A store of {@link Alias}es that can read and write them to some kind of persistent media.
29
+ */
30
+public interface AliasStore {
31
+
32
+    /**
33
+     * Reads and returns all aliases from within the store.
34
+     *
35
+     * @return A set of known aliases, or an empty set if the store is uninitialised.
36
+     */
37
+    Set<Alias> readAliases();
38
+
39
+    /**
40
+     * Writes all the given aliases to the store, replacing any existing aliases.
41
+     *
42
+     * @param aliases The set of aliases to be written to the store.
43
+     */
44
+    void writeAliases(Set<Alias> aliases);
45
+
46
+}

+ 163
- 0
src/com/dmdirc/commandparser/aliases/YamlAliasStore.java View File

@@ -0,0 +1,163 @@
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.commandparser.aliases;
24
+
25
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
26
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
27
+import com.dmdirc.commandparser.CommandType;
28
+
29
+import java.io.File;
30
+import java.io.FileInputStream;
31
+import java.io.FileOutputStream;
32
+import java.io.IOException;
33
+import java.io.InputStream;
34
+import java.io.InputStreamReader;
35
+import java.io.OutputStream;
36
+import java.io.OutputStreamWriter;
37
+import java.util.ArrayList;
38
+import java.util.HashMap;
39
+import java.util.HashSet;
40
+import java.util.List;
41
+import java.util.Map;
42
+import java.util.Set;
43
+
44
+import javax.inject.Inject;
45
+
46
+import org.slf4j.Logger;
47
+import org.slf4j.LoggerFactory;
48
+
49
+import com.esotericsoftware.yamlbeans.YamlReader;
50
+import com.esotericsoftware.yamlbeans.YamlWriter;
51
+
52
+import static com.dmdirc.util.YamlReaderUtils.asList;
53
+import static com.dmdirc.util.YamlReaderUtils.asMap;
54
+import static com.dmdirc.util.YamlReaderUtils.requiredString;
55
+
56
+/**
57
+ * Store that reads and writes aliases from a YAML file on disk.
58
+ * <p>
59
+ * Aliases are written as a list of maps:
60
+ * <pre><code>
61
+ * ----
62
+ * - name: quit
63
+ *   min_arguments: 0
64
+ *   substitution: exit $1-
65
+ * </code></pre>
66
+ */
67
+public class YamlAliasStore implements AliasStore {
68
+
69
+    /** The charset to use when reading and writing files. */
70
+    private static final String CHARSET = "UTF-8";
71
+
72
+    private static final Logger LOG = LoggerFactory.getLogger(YamlAliasStore.class);
73
+
74
+    /** The directory to find the aliases file in. */
75
+    private final String directory;
76
+
77
+    @Inject
78
+    public YamlAliasStore(@Directory(DirectoryType.BASE) final String directory) {
79
+        this.directory = directory;
80
+    }
81
+
82
+    @Override
83
+    public Set<Alias> readAliases() {
84
+        final File file = new File(directory, "aliases.yml");
85
+        final Set<Alias> aliases = new HashSet<>();
86
+
87
+        if (file.exists()) {
88
+            try (final InputStream stream = new FileInputStream(file);
89
+                    final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
90
+                final YamlReader yamlReader = new YamlReader(reader);
91
+                final Object root = yamlReader.read();
92
+                aliases.addAll(getAliases(asList(root)));
93
+                yamlReader.close();
94
+            } catch (IOException | IllegalArgumentException ex) {
95
+                LOG.warn("Unable to read aliases", ex);
96
+            }
97
+        }
98
+
99
+        return aliases;
100
+    }
101
+
102
+    @Override
103
+    public void writeAliases(final Set<Alias> aliases) {
104
+        final File file = new File(directory, "aliases.yml");
105
+        final List<Object> list = getAliases(aliases);
106
+
107
+        try (final OutputStream stream = new FileOutputStream(file);
108
+                final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
109
+            final YamlWriter yamlWriter = new YamlWriter(writer);
110
+            yamlWriter.write(list);
111
+            yamlWriter.close();
112
+        } catch (IOException ex) {
113
+            LOG.error("Unable to write aliases", ex);
114
+        }
115
+    }
116
+
117
+    /**
118
+     * Builds a set of aliases from the given YAML representations.
119
+     *
120
+     * @param objects The raw objects read from the YAML file.
121
+     *
122
+     * @return A list of corresponding aliases.
123
+     */
124
+    private List<Alias> getAliases(final List<Object> objects) {
125
+        final List<Alias> res = new ArrayList<>(objects.size());
126
+        for (Object alias : objects) {
127
+            try {
128
+                final Map<Object, Object> map = asMap(alias);
129
+
130
+                // TODO: Infer command type from the substituted command
131
+                final CommandType commandType = CommandType.TYPE_GLOBAL;
132
+                final String name = requiredString(map, "name");
133
+                final int minArguments = Integer.parseInt(requiredString(map, "min_arguments"));
134
+                final String substitution = requiredString(map, "substitution");
135
+
136
+                res.add(new Alias(commandType, name, minArguments, substitution));
137
+            } catch (IllegalArgumentException ex) {
138
+                LOG.info("Unable to read alias", ex);
139
+            }
140
+        }
141
+        return res;
142
+    }
143
+
144
+    /**
145
+     * Gets a set of maps that can be written to a YAML file.
146
+     *
147
+     * @param aliases The aliases to be stored.
148
+     *
149
+     * @return A list of corresponding maps.
150
+     */
151
+    private List<Object> getAliases(final Set<Alias> aliases) {
152
+        final List<Object> res = new ArrayList<>(aliases.size());
153
+        for (Alias alias : aliases) {
154
+            final Map<Object, Object> map = new HashMap<>();
155
+            map.put("name", alias.getName());
156
+            map.put("min_arguments", String.valueOf(alias.getMinArguments()));
157
+            map.put("substitution", alias.getSubstitution());
158
+            res.add(map);
159
+        }
160
+        return res;
161
+    }
162
+
163
+}

Loading…
Cancel
Save