浏览代码

Add YAML store for auto commands.

Change-Id: Ice4334772007337164a2fa3a21037bc168f86675
Reviewed-on: http://gerrit.dmdirc.com/3751
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 9 年前
父节点
当前提交
d75a119c2b

+ 2
- 2
src/com/dmdirc/commandparser/auto/AutoCommandStore.java 查看文件

@@ -39,8 +39,8 @@ public interface AutoCommandStore {
39 39
     /**
40 40
      * Writes all the given auto commands to the store, replacing any existing commands.
41 41
      *
42
-     * @param aliases The set of auto command to be written to the store.
42
+     * @param commands The set of auto command to be written to the store.
43 43
      */
44
-    void writeAutoCommands(Set<AutoCommand> aliases);
44
+    void writeAutoCommands(Set<AutoCommand> commands);
45 45
 
46 46
 }

+ 173
- 0
src/com/dmdirc/commandparser/auto/YamlAutoCommandStore.java 查看文件

@@ -0,0 +1,173 @@
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.auto;
24
+
25
+import com.google.common.base.Optional;
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.HashMap;
36
+import java.util.HashSet;
37
+import java.util.List;
38
+import java.util.Map;
39
+import java.util.Set;
40
+
41
+import org.slf4j.Logger;
42
+import org.slf4j.LoggerFactory;
43
+
44
+import com.esotericsoftware.yamlbeans.YamlReader;
45
+import com.esotericsoftware.yamlbeans.YamlWriter;
46
+
47
+import static com.dmdirc.util.YamlReaderUtils.asList;
48
+import static com.dmdirc.util.YamlReaderUtils.asMap;
49
+import static com.dmdirc.util.YamlReaderUtils.optionalString;
50
+import static com.dmdirc.util.YamlReaderUtils.requiredString;
51
+
52
+/**
53
+ * Store that reads and writes auto-commands from a YAML file on disk.
54
+ * <p>
55
+ * Auto-commands are written as a list of maps:
56
+ * <pre><code>
57
+ * ---
58
+ * - server: irc.foo.bar
59
+ *   network: quakenet
60
+ *   profile: bob
61
+ *   command: msg Q@Cserve.quakenet.org ...
62
+ * </code></pre>
63
+ */
64
+public class YamlAutoCommandStore implements AutoCommandStore {
65
+
66
+    /** The charset to use when reading and writing files. */
67
+    private static final String CHARSET = "UTF-8";
68
+
69
+    private static final Logger LOG = LoggerFactory.getLogger(YamlAutoCommandStore.class);
70
+
71
+    /** The path to the file to read and write auto commands in. */
72
+    private final Path path;
73
+
74
+    /**
75
+     * Creates a new YAML auto command store.
76
+     *
77
+     * @param path    The path to the YAML file to read and write auto commands in.
78
+     */
79
+    public YamlAutoCommandStore(final Path path) {
80
+        this.path = path;
81
+    }
82
+
83
+    @Override
84
+    public Set<AutoCommand> readAutoCommands() {
85
+        final Set<AutoCommand> commands = new HashSet<>();
86
+
87
+        if (Files.exists(path)) {
88
+            try (final InputStream stream = Files.newInputStream(path);
89
+                    final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
90
+                final YamlReader yamlReader = new YamlReader(reader);
91
+                final Object root = yamlReader.read();
92
+                commands.addAll(getCommands(asList(root)));
93
+                yamlReader.close();
94
+            } catch (IOException | IllegalArgumentException ex) {
95
+                LOG.warn("Unable to read auto commands", ex);
96
+            }
97
+        }
98
+
99
+        return commands;
100
+    }
101
+
102
+    @Override
103
+    public void writeAutoCommands(final Set<AutoCommand> commands) {
104
+        final List<Object> list = getCommands(commands);
105
+
106
+        try (final OutputStream stream = Files.newOutputStream(path);
107
+                final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
108
+            final YamlWriter yamlWriter = new YamlWriter(writer);
109
+            yamlWriter.write(list);
110
+            yamlWriter.close();
111
+        } catch (IOException ex) {
112
+            LOG.error("Unable to write auto commands", ex);
113
+        }
114
+    }
115
+
116
+    /**
117
+     * Builds a set of auto commands from the given YAML representations.
118
+     *
119
+     * @param objects The raw objects read from the YAML file.
120
+     *
121
+     * @return A list of corresponding auto commands.
122
+     */
123
+    private List<AutoCommand> getCommands(final List<Object> objects) {
124
+        final List<AutoCommand> res = new ArrayList<>(objects.size());
125
+        for (Object autoCommand : objects) {
126
+            try {
127
+                final Map<Object, Object> map = asMap(autoCommand);
128
+                final Optional<String> server =
129
+                        Optional.fromNullable(optionalString(map, "server"));
130
+                final Optional<String> network =
131
+                        Optional.fromNullable(optionalString(map, "network"));
132
+                final Optional<String> profile =
133
+                        Optional.fromNullable(optionalString(map, "profile"));
134
+                final String command = requiredString(map, "command");
135
+                res.add(new AutoCommand(server, network, profile, command));
136
+            } catch (IllegalArgumentException ex) {
137
+                LOG.info("Unable to read alias", ex);
138
+            }
139
+        }
140
+        return res;
141
+    }
142
+
143
+    /**
144
+     * Gets a set of maps that can be written to a YAML file.
145
+     *
146
+     * @param commands The auto commands to be stored.
147
+     *
148
+     * @return A list of corresponding maps.
149
+     */
150
+    private List<Object> getCommands(final Set<AutoCommand> commands) {
151
+        final List<Object> res = new ArrayList<>(commands.size());
152
+        for (AutoCommand command : commands) {
153
+            final Map<Object, Object> map = new HashMap<>();
154
+
155
+            if (command.getServer().isPresent()) {
156
+                map.put("server", command.getServer().get());
157
+            }
158
+
159
+            if (command.getNetwork().isPresent()) {
160
+                map.put("network", command.getNetwork().get());
161
+            }
162
+
163
+            if (command.getProfile().isPresent()) {
164
+                map.put("profile", command.getProfile().get());
165
+            }
166
+
167
+            map.put("command", command.getResponse());
168
+            res.add(map);
169
+        }
170
+        return res;
171
+    }
172
+
173
+}

正在加载...
取消
保存