Просмотр исходного кода

Minor action substitutor changes

Keep patterns statically compiled for efficiency
Better separation of server components and undefined config subs

Fixes issue 3961

Change-Id: Ib500352d03ba42400242002822908a1957003089
Reviewed-on: http://gerrit.dmdirc.com/1067
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4rc1
Chris Smith 14 лет назад
Родитель
Сommit
9d6d8ca3d3

+ 24
- 18
src/com/dmdirc/actions/ActionSubstitutor.java Просмотреть файл

@@ -53,6 +53,22 @@ public class ActionSubstitutor {
53 53
     private static final String ERR_NOT_DEFINED = "not_defined";
54 54
     /** Substitution to use to replace subs with illegal components. */
55 55
     private static final String ERR_ILLEGAL_COMPONENT = "illegal_component";
56
+
57
+    /** Pattern used to match braced substitutions. */
58
+    private static final Pattern BRACES_PATTERN = Pattern.compile("(?<!\\\\)((?:\\\\\\\\)*)"
59
+            + "(\\$\\{([^{}]*?)\\})");
60
+    /** Pattern used to match all other substitutions. */
61
+    private static final Pattern OTHER_PATTERN = Pattern.compile("(?<!\\\\)((?:\\\\\\\\)*)(\\$("
62
+            + "[0-9]+(-([0-9]+)?)?|" // Word subs - $1, $1-, $1-2
63
+            + "[0-9]+(\\.([A-Z_]+))+|" // Component subs - 2.FOO_BAR
64
+            + "[a-z0-9A-Z_\\.]+" // Config/server subs
65
+            + "))");
66
+    /** Pattern to determine if a substitution is a word number type. */
67
+    private final static Pattern NUMBER_PATTERN = Pattern.compile("([0-9]+)(-([0-9]+)?)?");
68
+    /** Pattern to determine if a substitution is an argument+component type. */
69
+    private final static Pattern COMP_PATTERN = Pattern.compile("([0-9]+)\\.([A-Z_]+(\\.[A-Z_]+)*)");
70
+    /** Pattern to determine if a substitution is a server component type. */
71
+    private final static Pattern SERVER_PATTERN = Pattern.compile("[A-Z_]+(\\.[A-Z_]+)*");
56 72
     
57 73
     /** The action type this substitutor is for. */
58 74
     private final ActionType type;
@@ -174,16 +190,8 @@ public class ActionSubstitutor {
174 190
 
175 191
         final StringBuilder res = new StringBuilder(target);
176 192
 
177
-        final Pattern bracesPattern = Pattern.compile("(?<!\\\\)((?:\\\\\\\\)*)"
178
-                + "(\\$\\{([^{}]*?)\\})");
179
-        final Pattern otherPattern = Pattern.compile("(?<!\\\\)((?:\\\\\\\\)*)(\\$("
180
-                + "[0-9]+(-([0-9]+)?)?|" // Word subs - $1, $1-, $1-2
181
-                + "[0-9]+(\\.([A-Z_]+))+|" // Component subs - 2.FOO_BAR
182
-                + "[a-z0-9A-Z_\\.]+" // Config/server subs
183
-                + "))");
184
-
185
-        Matcher bracesMatcher = bracesPattern.matcher(res);
186
-        Matcher otherMatcher = otherPattern.matcher(res);
193
+        Matcher bracesMatcher = BRACES_PATTERN.matcher(res);
194
+        Matcher otherMatcher = OTHER_PATTERN.matcher(res);
187 195
 
188 196
         boolean first;
189 197
 
@@ -196,8 +204,8 @@ public class ActionSubstitutor {
196 204
             res.delete(start, end);
197 205
             res.insert(start, getSubstitution(doSubstitution(group, args), args));
198 206
             
199
-            bracesMatcher = bracesPattern.matcher(res);
200
-            otherMatcher = otherPattern.matcher(res);
207
+            bracesMatcher = BRACES_PATTERN.matcher(res);
208
+            otherMatcher = OTHER_PATTERN.matcher(res);
201 209
         }
202 210
 
203 211
         return res.toString().replaceAll("\\\\(.)", "$1");
@@ -211,11 +219,9 @@ public class ActionSubstitutor {
211 219
      * @return The substitution to be used
212 220
      */
213 221
     private String getSubstitution(final String substitution, final Object ... args) {
214
-        final Pattern numberPattern = Pattern.compile("([0-9]+)(-([0-9]+)?)?");
215
-        final Matcher numberMatcher = numberPattern.matcher(substitution);
216
-
217
-        final Pattern compPattern = Pattern.compile("([0-9]+)\\.([A-Z_]+(\\.[A-Z_]+)*)");
218
-        final Matcher compMatcher = compPattern.matcher(substitution);
222
+        final Matcher numberMatcher = NUMBER_PATTERN.matcher(substitution);
223
+        final Matcher compMatcher = COMP_PATTERN.matcher(substitution);
224
+        final Matcher serverMatcher = SERVER_PATTERN.matcher(substitution);
219 225
 
220 226
         if (usesWordSubstitutions() && numberMatcher.matches()) {
221 227
             final CommandArguments words = args[2] instanceof String ?
@@ -253,7 +259,7 @@ public class ActionSubstitutor {
253 259
             return manager.getOption("actions", substitution);
254 260
         }
255 261
 
256
-        if (hasFrameContainer()) {
262
+        if (hasFrameContainer() && serverMatcher.matches()) {
257 263
             final Server server = ((FrameContainer) args[0]).getServer();
258 264
 
259 265
             if (server != null) {

+ 5
- 1
test/com/dmdirc/actions/ActionSubstitutorTest.java Просмотреть файл

@@ -77,7 +77,7 @@ public class ActionSubstitutorTest {
77 77
 
78 78
         when(channel.getServer()).thenReturn(server);
79 79
         when(channel.getConfigManager()).thenReturn(manager);
80
-        when(server.getState()).thenReturn(ServerState.CONNECTED);
80
+        when(server.getState()).thenReturn(ServerState.DISCONNECTED);
81 81
         when(server.getAwayMessage()).thenReturn("foo");
82 82
 
83 83
         when(manager.getOptions(eq("actions"))).thenReturn(settings);
@@ -129,6 +129,7 @@ public class ActionSubstitutorTest {
129 129
             {"$bravo", "A"},
130 130
             {"$charlie", "A"},
131 131
             {"$delta", "win!"},
132
+            {"$sigma", "not_defined"},
132 133
             // ---- Word subs --------------------------------------------------
133 134
             {"$5-6", "5 6"},
134 135
             {"${5-6}", "5 6"},
@@ -136,6 +137,9 @@ public class ActionSubstitutorTest {
136 137
             {"${5-${${6}}}", "5 6"},
137 138
             // ---- Component subs ---------------------------------------------
138 139
             {"${2.STRING_STRING.STRING_LENGTH}", "28"},
140
+            {"${2.STRING_FLUB.STRING_LENGTH}", "illegal_component"},
141
+            {"${SERVER_NETWORKFOO}", "illegal_component"},
142
+            {"${SERVER_NETWORK}", "not_connected"},
139 143
             // ---- Escaping ---------------------------------------------------
140 144
             {"\\$1", "$1"},
141 145
             {"\\$alpha $alpha", "$alpha A"},

Загрузка…
Отмена
Сохранить