Explorar el Código

Move static styliser methods to new class.

The styliser needs an interface extracting into the API, and
we don't really want huge static methods defined on that.
pull/722/head
Chris Smith hace 7 años
padre
commit
8343350cec

+ 5
- 8
src/main/java/com/dmdirc/Channel.java Ver fichero

@@ -44,10 +44,8 @@ import com.dmdirc.parser.interfaces.Parser;
44 44
 import com.dmdirc.ui.core.components.WindowComponent;
45 45
 import com.dmdirc.ui.input.TabCompletionType;
46 46
 import com.dmdirc.ui.messages.BackBufferFactory;
47
-import com.dmdirc.ui.messages.Styliser;
48
-
47
+import com.dmdirc.ui.messages.StyledMessageUtils;
49 48
 import com.google.common.collect.EvictingQueue;
50
-
51 49
 import java.util.ArrayList;
52 50
 import java.util.Arrays;
53 51
 import java.util.Collection;
@@ -56,7 +54,6 @@ import java.util.List;
56 54
 import java.util.Optional;
57 55
 import java.util.Queue;
58 56
 import java.util.stream.Collectors;
59
-
60 57
 import javax.annotation.Nullable;
61 58
 
62 59
 /**
@@ -98,7 +95,7 @@ public class Channel extends FrameContainer implements GroupChat {
98 95
             final GroupChatUserManager groupChatUserManager) {
99 96
         super("channel-inactive",
100 97
                 newChannelInfo.getName(),
101
-                Styliser.stipControlCodes(newChannelInfo.getName()),
98
+                new StyledMessageUtils().stripControlCodes(newChannelInfo.getName()), // TODO: Inject this
102 99
                 configMigrator.getConfigProvider(),
103 100
                 backBufferFactory,
104 101
                 connection.getWindowModel().getEventBus(),
@@ -220,12 +217,12 @@ public class Channel extends FrameContainer implements GroupChat {
220 217
      * Updates the title of the channel window, and of the main window if appropriate.
221 218
      */
222 219
     private void updateTitle() {
223
-        String temp = Styliser.stipControlCodes(channelInfo.getName());
220
+        final StyledMessageUtils styleUtils = new StyledMessageUtils();
224 221
 
222
+        String temp = styleUtils.stripControlCodes(channelInfo.getName());
225 223
         if (!channelInfo.getTopic().isEmpty()) {
226
-            temp += " - " + Styliser.stipControlCodes(channelInfo.getTopic());
224
+            temp += " - " + styleUtils.stripControlCodes(channelInfo.getTopic());
227 225
         }
228
-
229 226
         setTitle(temp);
230 227
     }
231 228
 

+ 2
- 1
src/main/java/com/dmdirc/ui/messages/IRCLine.java Ver fichero

@@ -35,6 +35,7 @@ public class IRCLine implements Line {
35 35
     private final String timestamp;
36 36
     private final String text;
37 37
     private final Styliser styliser;
38
+    private final StyledMessageUtils styleUtils = new StyledMessageUtils(); // TODO: Inject
38 39
     private final DisplayPropertyMap displayProperties;
39 40
     private int fontSize;
40 41
     private String fontName;
@@ -94,7 +95,7 @@ public class IRCLine implements Line {
94 95
 
95 96
     @Override
96 97
     public String getText() {
97
-        return Styliser.stipControlCodes(timestamp + text);
98
+        return styleUtils.stripControlCodes(timestamp + text);
98 99
     }
99 100
 
100 101
     @Override

+ 100
- 0
src/main/java/com/dmdirc/ui/messages/StyledMessageUtils.java Ver fichero

@@ -0,0 +1,100 @@
1
+package com.dmdirc.ui.messages;
2
+
3
+import javax.inject.Inject;
4
+import javax.inject.Singleton;
5
+
6
+import static com.dmdirc.ui.messages.Styliser.CODE_CHANNEL;
7
+import static com.dmdirc.ui.messages.Styliser.CODE_HYPERLINK;
8
+import static com.dmdirc.ui.messages.Styliser.CODE_NICKNAME;
9
+import static com.dmdirc.ui.messages.Styliser.CODE_SMILIE;
10
+import static com.dmdirc.ui.messages.Styliser.CODE_TOOLTIP;
11
+import static com.google.common.base.Preconditions.checkArgument;
12
+
13
+/**
14
+ * Utilities for dealing with styled messages.
15
+ */
16
+@Singleton
17
+public class StyledMessageUtils {
18
+
19
+    @Inject
20
+    public StyledMessageUtils() {
21
+    }
22
+
23
+    /**
24
+     * Strips all recognised control codes from the input string.
25
+     *
26
+     * @param input the String to be stripped
27
+     *
28
+     * @return a copy of the input with control codes removed
29
+     */
30
+    public String stripControlCodes(final String input) {
31
+        return input.replaceAll("[" + IRCControlCodes.BOLD + CODE_CHANNEL + IRCControlCodes.FIXED
32
+                + CODE_HYPERLINK + IRCControlCodes.ITALIC + IRCControlCodes.NEGATE + CODE_NICKNAME
33
+                + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + "]|"
34
+                + IRCControlCodes.COLOUR_HEX + "([A-Za-z0-9]{6}(,[A-Za-z0-9]{6})?)?|"
35
+                + IRCControlCodes.COLOUR + "([0-9]{1,2}(,[0-9]{1,2})?)?", "")
36
+                .replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)" + CODE_TOOLTIP, "$1");
37
+    }
38
+
39
+    /**
40
+     * Retrieves the styled String contained within the unstyled offsets specified. That is, the
41
+     * <code>from</code> and <code>to</code> arguments correspond to indexes in an unstyled version
42
+     * of the <code>styled</code> string. The unstyled indices are translated to offsets within the
43
+     * styled String, and the return value includes all text and control codes between those
44
+     * indices.
45
+     * <p>
46
+     * The index translation is left-biased; that is, the indices are translated to be as far left
47
+     * as they possibly can be. This means that the start of the string will include any control
48
+     * codes immediately preceding the desired text, and the end will not include any trailing
49
+     * codes.
50
+     * <p>
51
+     * This method will NOT include "internal" control codes in the output.
52
+     *
53
+     * @param styled The styled String to be operated on
54
+     * @param from   The starting index in the unstyled string
55
+     * @param to     The ending index in the unstyled string
56
+     *
57
+     * @return The corresponding text between the two indices
58
+     */
59
+    public String getStyledText(final String styled, final int from, final int to) {
60
+        checkArgument(from < to, "'from' (" + from + ") must be less than 'to' (" + to + ')');
61
+        checkArgument(from >= 0, "'from' (" + from + ") must be non-negative");
62
+
63
+        final String unstyled = stripControlCodes(styled);
64
+
65
+        checkArgument(to <= unstyled.length(), "'to' (" + to + ") must be less than or equal to "
66
+                + "the unstyled length (" + unstyled.length() + ')');
67
+
68
+        final String startBit = unstyled.substring(0, from);
69
+        final String middleBit = unstyled.substring(from, to);
70
+        final String sanitised = stripInternalControlCodes(styled);
71
+        int start = from;
72
+
73
+        while (!stripControlCodes(sanitised.substring(0, start)).equals(startBit)) {
74
+            start++;
75
+        }
76
+
77
+        int end = to + start - from;
78
+
79
+        while (!stripControlCodes(sanitised.substring(start, end)).equals(middleBit)) {
80
+            end++;
81
+        }
82
+
83
+        return sanitised.substring(start, end);
84
+    }
85
+
86
+    /**
87
+     * Strips all recognised internal control codes from the input string.
88
+     *
89
+     * @param input the String to be stripped
90
+     *
91
+     * @return a copy of the input with control codes removed
92
+     */
93
+    private String stripInternalControlCodes(final String input) {
94
+        return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME
95
+                + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + ']', "")
96
+                .replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)"
97
+                        + CODE_TOOLTIP, "$1");
98
+    }
99
+
100
+}

+ 10
- 3
src/main/java/com/dmdirc/ui/messages/Styliser.java Ver fichero

@@ -22,16 +22,17 @@
22 22
 
23 23
 package com.dmdirc.ui.messages;
24 24
 
25
-import static com.google.common.base.Preconditions.checkArgument;
26
-
27 25
 import com.dmdirc.interfaces.Connection;
28 26
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 27
 import com.dmdirc.interfaces.config.ConfigChangeListener;
30 28
 import com.dmdirc.util.colours.Colour;
29
+import com.google.common.annotations.VisibleForTesting;
31 30
 import java.util.Locale;
32 31
 import java.util.regex.Pattern;
33 32
 import javax.annotation.Nullable;
34 33
 
34
+import static com.google.common.base.Preconditions.checkArgument;
35
+
35 36
 /**
36 37
  * The styliser applies IRC styles to text. Styles are indicated by various control codes which are
37 38
  * a de-facto IRC standard.
@@ -195,7 +196,9 @@ public class Styliser implements ConfigChangeListener {
195 196
      * @return The corresponding text between the two indices
196 197
      *
197 198
      * @since 0.6.3
199
+     * @deprecated Use {@link StyledMessageUtils}
198 200
      */
201
+    @Deprecated
199 202
     public static String getStyledText(final String styled, final int from, final int to) {
200 203
         checkArgument(from < to, "'from' (" + from + ") must be less than 'to' (" + to + ')');
201 204
         checkArgument(from >= 0, "'from' (" + from + ") must be non-negative");
@@ -289,7 +292,9 @@ public class Styliser implements ConfigChangeListener {
289 292
      * @param input the String to be stripped
290 293
      *
291 294
      * @return a copy of the input with control codes removed
295
+     * @deprecated Use {@link StyledMessageUtils}
292 296
      */
297
+    @Deprecated
293 298
     public static String stipControlCodes(final String input) {
294 299
         return input.replaceAll("[" + IRCControlCodes.BOLD + CODE_CHANNEL + IRCControlCodes.FIXED
295 300
                 + CODE_HYPERLINK + IRCControlCodes.ITALIC + IRCControlCodes.NEGATE + CODE_NICKNAME
@@ -308,6 +313,7 @@ public class Styliser implements ConfigChangeListener {
308 313
      *
309 314
      * @since 0.6.5
310 315
      */
316
+    @Deprecated
311 317
     private static String stipInternalControlCodes(final String input) {
312 318
         return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME
313 319
                 + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + ']', "")
@@ -324,7 +330,8 @@ public class Styliser implements ConfigChangeListener {
324 330
      *
325 331
      * @return A substring of the input containing no control characters
326 332
      */
327
-    public static String readUntilControl(final String input) {
333
+    @VisibleForTesting
334
+    static String readUntilControl(final String input) {
328 335
         int pos = input.length();
329 336
 
330 337
         pos = checkChar(pos, input.indexOf(IRCControlCodes.BOLD));

+ 58
- 0
src/test/java/com/dmdirc/ui/messages/StyledMessageUtilsTest.java Ver fichero

@@ -0,0 +1,58 @@
1
+/*
2
+ * Copyright (c) 2006-2016 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
+
26
+import org.junit.Before;
27
+import org.junit.Test;
28
+
29
+import static org.junit.Assert.assertEquals;
30
+
31
+public class StyledMessageUtilsTest {
32
+
33
+    private StyledMessageUtils styleUtils;
34
+
35
+    @Before
36
+    public void setUp() {
37
+        styleUtils = new StyledMessageUtils();
38
+    }
39
+
40
+    @Test
41
+    public void testStripControlCodes1() {
42
+        final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test";
43
+
44
+        final String expResult = "This is a test";
45
+        final String result = styleUtils.stripControlCodes(input);
46
+        assertEquals(expResult, result);
47
+    }
48
+
49
+    @Test
50
+    public void testStripControlCodes2() {
51
+        final String input = "This is "+ (char) 3 +"5a "+ (char) 4 +"FF0000test";
52
+
53
+        final String expResult = "This is a test";
54
+        final String result = styleUtils.stripControlCodes(input);
55
+        assertEquals(expResult, result);
56
+    }
57
+
58
+}

+ 3
- 3
src/test/java/com/dmdirc/ui/messages/StyliserIndicesTest.java Ver fichero

@@ -34,8 +34,8 @@ import static org.junit.Assert.*;
34 34
 @RunWith(Parameterized.class)
35 35
 public class StyliserIndicesTest {
36 36
 
37
-    protected String input, output;
38
-    protected int start, end;
37
+    private String input, output;
38
+    private int start, end;
39 39
 
40 40
     public StyliserIndicesTest(final String input, final int start, final int end, final String output) {
41 41
         this.input = input;
@@ -46,7 +46,7 @@ public class StyliserIndicesTest {
46 46
 
47 47
     @Test
48 48
     public void testStyle() {
49
-        assertEquals(output, Styliser.getStyledText(input, start, end));
49
+        assertEquals(output, new StyledMessageUtils().getStyledText(input, start, end));
50 50
     }
51 51
 
52 52
     @Parameterized.Parameters

+ 0
- 18
src/test/java/com/dmdirc/ui/messages/StyliserTest.java Ver fichero

@@ -39,24 +39,6 @@ public class StyliserTest {
39 39
 
40 40
     @Mock private EventBus eventBus;
41 41
 
42
-    @Test
43
-    public void testStripControlCodes1() {
44
-        final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test";
45
-
46
-        final String expResult = "This is a test";
47
-        final String result = Styliser.stipControlCodes(input);
48
-        assertEquals(expResult, result);
49
-    }
50
-
51
-    @Test
52
-    public void testStripControlCodes2() {
53
-        final String input = "This is "+ (char) 3 +"5a "+ (char) 4 +"FF0000test";
54
-
55
-        final String expResult = "This is a test";
56
-        final String result = Styliser.stipControlCodes(input);
57
-        assertEquals(expResult, result);
58
-    }
59
-
60 42
     @Test
61 43
     public void testReadUntilControl1() {
62 44
         final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test";

Loading…
Cancelar
Guardar