Parcourir la source

URLHandler now uses new CommandUtils.parseArguments

Related to issue CLIENT-224

Change-Id: Icbc7e67a96f6da77691626820c1dc2c3cb1f1cb4
Reviewed-on: http://gerrit.dmdirc.com/1895
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.6b1
Simon Mott il y a 13 ans
Parent
révision
ff126f3879

+ 3
- 45
src/com/dmdirc/ui/core/util/URLHandler.java Voir le fichier

@@ -30,15 +30,14 @@ import com.dmdirc.logger.Logger;
30 30
 import com.dmdirc.ui.StatusMessage;
31 31
 import com.dmdirc.ui.core.components.StatusBarManager;
32 32
 import com.dmdirc.ui.interfaces.UIController;
33
+import com.dmdirc.util.CommandUtils;
33 34
 
34 35
 import java.awt.Desktop;
35 36
 import java.io.IOException;
36 37
 import java.net.URI;
37 38
 import java.net.URISyntaxException;
38 39
 import java.net.URL;
39
-import java.util.ArrayList;
40 40
 import java.util.Date;
41
-import java.util.List;
42 41
 
43 42
 /** Handles URLs. */
44 43
 public class URLHandler {
@@ -249,54 +248,13 @@ public class URLHandler {
249 248
      */
250 249
     private void execApp(final String command) {
251 250
         try {
252
-            Runtime.getRuntime().exec(parseArguments(command));
251
+            Runtime.getRuntime().exec(CommandUtils.parseArguments(command));
253 252
         } catch (IOException ex) {
254 253
             Logger.userError(ErrorLevel.LOW, "Unable to run application: "
255 254
                     + ex.getMessage(), ex);
256 255
         }
257 256
     }
258 257
 
259
-    /**
260
-     * Parses the specified command into an array of arguments. Arguments are
261
-     * separated by spaces. Multi-word arguments may be specified by starting
262
-     * the argument with a quote (") and finishing it with a quote (").
263
-     *
264
-     * @param command The command to parse
265
-     * @return An array of arguments corresponding to the command
266
-     */
267
-    protected static String[] parseArguments(final String command) {
268
-        final List<String> args = new ArrayList<String>();
269
-        final StringBuilder builder = new StringBuilder();
270
-        boolean inquote = false;
271
-
272
-        for (String word : command.split(" ")) {
273
-            if (word.endsWith("\"") && inquote) {
274
-                args.add(builder.toString() + ' ' + word.substring(0, word.length() - 1));
275
-                builder.delete(0, builder.length());
276
-                inquote = false;
277
-            } else if (inquote) {
278
-                if (builder.length() > 0) {
279
-                    builder.append(' ');
280
-                }
281
-
282
-                builder.append(word);
283
-            } else if (word.startsWith("\"") && !word.endsWith("\"")) {
284
-                inquote = true;
285
-                builder.append(word.substring(1));
286
-            } else if (word.startsWith("\"") && word.endsWith("\"")) {
287
-                if (word.length() == 1) {
288
-                    inquote = true;
289
-                } else {
290
-                    args.add(word.substring(1, word.length() - 1));
291
-                }
292
-            } else {
293
-                args.add(word);
294
-            }
295
-        }
296
-
297
-        return args.toArray(new String[args.size()]);
298
-    }
299
-
300 258
     /**
301 259
      * Opens the specified URL in the users browser.
302 260
      *
@@ -341,4 +299,4 @@ public class URLHandler {
341 299
                     "manually");
342 300
         }
343 301
     }
344
-}
302
+}

+ 2
- 22
test/com/dmdirc/ui/core/util/URLHandlerTest.java Voir le fichier

@@ -25,31 +25,12 @@ package com.dmdirc.ui.core.util;
25 25
 import java.net.MalformedURLException;
26 26
 import java.net.URI;
27 27
 import java.net.URISyntaxException;
28
-import java.util.Arrays;
28
+
29 29
 import org.junit.Test;
30 30
 import static org.junit.Assert.*;
31 31
 
32 32
 public class URLHandlerTest {
33 33
     
34
-    @Test
35
-    public void testParseArguments() {
36
-        final String[][][] tests = new String[][][]{
37
-            {{"abcdef abcdef abcdef"}, {"abcdef", "abcdef", "abcdef"}},
38
-            {{"abcdef \"abcdef abcdef\""}, {"abcdef", "abcdef abcdef"}},
39
-            {{"abcdef \"abcdef foo abcdef\""}, {"abcdef", "abcdef foo abcdef"}},
40
-            {{"abcdef \"abcdef\" \"abcdef\""}, {"abcdef", "abcdef", "abcdef"}},
41
-            {{"abcdef \"\""}, {"abcdef", ""}},
42
-            {{"abcdef \" foo?\""}, {"abcdef", " foo?"}},
43
-        };
44
-        
45
-        for (String[][] test : tests) {
46
-            final String[] res = URLHandler.parseArguments(test[0][0]);
47
-            assertTrue(test[0][0] + " - " + Arrays.toString(test[1]) + " - "
48
-                    + Arrays.toString(res),
49
-                    Arrays.equals(test[1], res));
50
-        }
51
-    }
52
-
53 34
     @Test
54 35
     public void testSubstituteParams() throws MalformedURLException, URISyntaxException {
55 36
         final Object[][] tests = new Object[][]{
@@ -63,7 +44,7 @@ public class URLHandlerTest {
63 44
             {new URI("protocol://@host:33/?foo+bar#frag"), "$query $fragment", "foo+bar frag"},
64 45
             {new URI("host.com"), "$path $protocol$host", "host.com "},
65 46
         };
66
-        
47
+
67 48
         for (Object[] test : tests) {
68 49
             final String result = URLHandler.substituteParams((URI) test[0],
69 50
                     (String) test[1]);
@@ -71,5 +52,4 @@ public class URLHandlerTest {
71 52
                     (String) test[2], result);
72 53
         }
73 54
     }
74
-    
75 55
 }

Chargement…
Annuler
Enregistrer