Przeglądaj źródła

Merge pull request #117 from csmith/master

Some work on event formatting.
pull/118/head
Greg Holmes 9 lat temu
rodzic
commit
fa14f44274

+ 15
- 2
src/com/dmdirc/ui/messages/EventFormatter.java Wyświetl plik

@@ -40,6 +40,8 @@ import java.util.Optional;
40 40
  */
41 41
 public class EventFormatter {
42 42
 
43
+    private static final String ERROR_STRING = "<FormatError>";
44
+
43 45
     private final EventPropertyManager propertyManager;
44 46
     private final EventTemplateProvider templateProvider;
45 47
 
@@ -73,12 +75,23 @@ public class EventFormatter {
73 75
 
74 76
         Object target = event;
75 77
         for (String part : dataParts) {
76
-            target = propertyManager.getProperty(target, target.getClass(), part);
78
+            final Optional<Object> result =
79
+                    propertyManager.getProperty(target, target.getClass(), part);
80
+            if (result.isPresent()) {
81
+                target = result.get();
82
+            } else {
83
+                return ERROR_STRING;
84
+            }
77 85
         }
78 86
 
79 87
         String value = target.toString();
80 88
         for (int i = 1; i < functionParts.length; i++) {
81
-            value = propertyManager.applyFunction(value, functionParts[i]);
89
+            final Optional<String> result = propertyManager.applyFunction(value, functionParts[i]);
90
+            if (result.isPresent()) {
91
+                value = result.get();
92
+            } else {
93
+                return ERROR_STRING;
94
+            }
82 95
         }
83 96
 
84 97
         return value;

+ 30
- 4
src/com/dmdirc/ui/messages/EventPropertyManager.java Wyświetl plik

@@ -22,15 +22,41 @@
22 22
 
23 23
 package com.dmdirc.ui.messages;
24 24
 
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.UserErrorEvent;
27
+import com.dmdirc.logger.ErrorLevel;
28
+
29
+import java.lang.reflect.Method;
30
+import java.util.Optional;
31
+
32
+import javax.inject.Inject;
33
+
25 34
 public class EventPropertyManager {
26 35
 
27
-    public <S, T> T getProperty(final S object, final Class<? extends S> type,
36
+    private final DMDircMBassador eventBus;
37
+
38
+    @Inject
39
+    public EventPropertyManager(final DMDircMBassador eventBus) {
40
+        this.eventBus = eventBus;
41
+    }
42
+
43
+    public <S> Optional<Object> getProperty(final S object, final Class<? extends S> type,
28 44
             final String property) {
29
-        return null;
45
+        final String methodName = "get" + property.substring(0, 1).toUpperCase()
46
+                + property.substring(1);
47
+        try {
48
+            final Method method = type.getMethod(methodName);
49
+            return Optional.ofNullable(method.invoke(object));
50
+        } catch (ReflectiveOperationException ex) {
51
+            eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
52
+                    "Unable to format event: could not retrieve property " + property,
53
+                    ex.getMessage()));
54
+        }
55
+        return Optional.empty();
30 56
     }
31 57
 
32
-    public String applyFunction(final String input, final String property) {
33
-        return null;
58
+    public Optional<String> applyFunction(final String input, final String function) {
59
+        return Optional.empty();
34 60
     }
35 61
 
36 62
 }

+ 1
- 1
test/com/dmdirc/ui/messages/EventFormatterTest.java Wyświetl plik

@@ -53,7 +53,7 @@ public class EventFormatterTest {
53 53
         when(templateProvider.getTemplate(ChannelMessageEvent.class))
54 54
                 .thenReturn(Optional.ofNullable("Template {{channel}} meep"));
55 55
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
56
-                .thenReturn("MONKEY");
56
+                .thenReturn(Optional.of("MONKEY"));
57 57
     }
58 58
 
59 59
     @Test

Ładowanie…
Anuluj
Zapisz