Browse Source

Initial work on EventFormatters.

These will be used to format events for displaying in a window.

Change-Id: I30381c2adc7bd77de486d6c0410a0a6395a7ea8d
Reviewed-on: http://gerrit.dmdirc.com/3835
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
pull/1/head
Chris Smith 9 years ago
parent
commit
0b42c771be

+ 87
- 0
src/com/dmdirc/ui/messages/EventFormatter.java View File

@@ -0,0 +1,87 @@
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.ui.messages;
24
+
25
+import com.dmdirc.events.DisplayableEvent;
26
+
27
+import com.google.common.base.Optional;
28
+
29
+/**
30
+ * Formats an event into a text string, based on a user-defined template.
31
+ *
32
+ * <p>Template tags start with <code>{{</code> and end with <code>}}</code>. The start of the
33
+ * tag should be the property of the event that will be displayed. Properties can be chained using
34
+ * a <code>.</code> character, e.g. <code>{{user.hostname}}</code>. One or more functions can
35
+ * be applied to the result of this, to change the appearance of the output. Functions are
36
+ * separated from their argument with a <code>|</code> character,
37
+ * e.g. <code>{{user.hostname|toCapitals}}</code>.
38
+ *
39
+ * <p>Properties and functions are case-insensitive.
40
+ */
41
+public class EventFormatter {
42
+
43
+    private final EventPropertyManager propertyManager;
44
+    private final EventTemplateProvider templateProvider;
45
+
46
+    public EventFormatter(final EventPropertyManager propertyManager,
47
+            final EventTemplateProvider templateProvider) {
48
+        this.propertyManager = propertyManager;
49
+        this.templateProvider = templateProvider;
50
+    }
51
+
52
+    public Optional<String> format(final DisplayableEvent event) {
53
+        final Optional<String> template = templateProvider.getTemplate(event.getClass());
54
+
55
+        if (template.isPresent()) {
56
+            final StringBuilder builder = new StringBuilder(template.get());
57
+            int tagStart = builder.indexOf("{{");
58
+            while (tagStart > -1) {
59
+                final int tagEnd = builder.indexOf("}}", tagStart);
60
+                final String tag = builder.substring(tagStart + 2, tagEnd);
61
+                builder.replace(tagStart, tagEnd + 2, getReplacement(event, tag));
62
+                tagStart = builder.indexOf("{{");
63
+            }
64
+            return Optional.fromNullable(builder.toString());
65
+        }
66
+
67
+        return Optional.absent();
68
+    }
69
+
70
+    private String getReplacement(final DisplayableEvent event, final String tag) {
71
+        final String[] functionParts = tag.split("\\|");
72
+        final String[] dataParts = functionParts[0].split("\\.");
73
+
74
+        Object target = event;
75
+        for (String part : dataParts) {
76
+            target = propertyManager.getProperty(target, target.getClass(), part);
77
+        }
78
+
79
+        String value = target.toString();
80
+        for (int i = 1; i < functionParts.length; i++) {
81
+            value = propertyManager.applyFunction(value, functionParts[i]);
82
+        }
83
+
84
+        return value;
85
+    }
86
+
87
+}

+ 36
- 0
src/com/dmdirc/ui/messages/EventPropertyManager.java View File

@@ -0,0 +1,36 @@
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.ui.messages;
24
+
25
+public class EventPropertyManager {
26
+
27
+    public <S, T> T getProperty(final S object, final Class<? extends S> type,
28
+            final String property) {
29
+        return null;
30
+    }
31
+
32
+    public String applyFunction(final String input, final String property) {
33
+        return null;
34
+    }
35
+
36
+}

+ 42
- 0
src/com/dmdirc/ui/messages/EventTemplateProvider.java View File

@@ -0,0 +1,42 @@
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.ui.messages;
24
+
25
+import com.dmdirc.events.DisplayableEvent;
26
+
27
+import com.google.common.base.Optional;
28
+
29
+/**
30
+ * Provides templates to be used for displayable events.
31
+ */
32
+public interface EventTemplateProvider {
33
+
34
+    /**
35
+     * Gets the template to be used, if any, for the given event type.
36
+     *
37
+     * @param eventType The type of event to retrieve a template for.
38
+     * @return The template to use, or an absent optional if no template is defined.
39
+     */
40
+    Optional<String> getTemplate(final Class<? extends DisplayableEvent> eventType);
41
+
42
+}

+ 64
- 0
test/com/dmdirc/ui/messages/EventFormatterTest.java View File

@@ -0,0 +1,64 @@
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.ui.messages;
24
+
25
+import com.dmdirc.Channel;
26
+import com.dmdirc.events.ChannelMessageEvent;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import org.junit.Before;
31
+import org.junit.Test;
32
+import org.junit.runner.RunWith;
33
+import org.mockito.Mock;
34
+import org.mockito.runners.MockitoJUnitRunner;
35
+
36
+import static org.junit.Assert.assertEquals;
37
+import static org.mockito.Mockito.when;
38
+
39
+@RunWith(MockitoJUnitRunner.class)
40
+public class EventFormatterTest {
41
+
42
+    @Mock private EventPropertyManager propertyManager;
43
+    @Mock private EventTemplateProvider templateProvider;
44
+    @Mock private Channel channel;
45
+    private ChannelMessageEvent messageEvent;
46
+    private EventFormatter formatter;
47
+
48
+    @Before
49
+    public void setup() {
50
+        formatter = new EventFormatter(propertyManager, templateProvider);
51
+        messageEvent = new ChannelMessageEvent(channel, null, null);
52
+
53
+        when(templateProvider.getTemplate(ChannelMessageEvent.class))
54
+                .thenReturn(Optional.fromNullable("Template {{channel}} meep"));
55
+        when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
56
+                .thenReturn("MONKEY");
57
+    }
58
+
59
+    @Test
60
+    public void testBasicFormat() {
61
+        assertEquals("Template MONKEY meep", formatter.format(messageEvent).orNull());
62
+    }
63
+
64
+}

Loading…
Cancel
Save