|
@@ -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
|
+}
|