Browse 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 13 years ago
parent
commit
ff126f3879

+ 3
- 45
src/com/dmdirc/ui/core/util/URLHandler.java View File

30
 import com.dmdirc.ui.StatusMessage;
30
 import com.dmdirc.ui.StatusMessage;
31
 import com.dmdirc.ui.core.components.StatusBarManager;
31
 import com.dmdirc.ui.core.components.StatusBarManager;
32
 import com.dmdirc.ui.interfaces.UIController;
32
 import com.dmdirc.ui.interfaces.UIController;
33
+import com.dmdirc.util.CommandUtils;
33
 
34
 
34
 import java.awt.Desktop;
35
 import java.awt.Desktop;
35
 import java.io.IOException;
36
 import java.io.IOException;
36
 import java.net.URI;
37
 import java.net.URI;
37
 import java.net.URISyntaxException;
38
 import java.net.URISyntaxException;
38
 import java.net.URL;
39
 import java.net.URL;
39
-import java.util.ArrayList;
40
 import java.util.Date;
40
 import java.util.Date;
41
-import java.util.List;
42
 
41
 
43
 /** Handles URLs. */
42
 /** Handles URLs. */
44
 public class URLHandler {
43
 public class URLHandler {
249
      */
248
      */
250
     private void execApp(final String command) {
249
     private void execApp(final String command) {
251
         try {
250
         try {
252
-            Runtime.getRuntime().exec(parseArguments(command));
251
+            Runtime.getRuntime().exec(CommandUtils.parseArguments(command));
253
         } catch (IOException ex) {
252
         } catch (IOException ex) {
254
             Logger.userError(ErrorLevel.LOW, "Unable to run application: "
253
             Logger.userError(ErrorLevel.LOW, "Unable to run application: "
255
                     + ex.getMessage(), ex);
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
      * Opens the specified URL in the users browser.
259
      * Opens the specified URL in the users browser.
302
      *
260
      *
341
                     "manually");
299
                     "manually");
342
         }
300
         }
343
     }
301
     }
344
-}
302
+}

+ 2
- 22
test/com/dmdirc/ui/core/util/URLHandlerTest.java View File

25
 import java.net.MalformedURLException;
25
 import java.net.MalformedURLException;
26
 import java.net.URI;
26
 import java.net.URI;
27
 import java.net.URISyntaxException;
27
 import java.net.URISyntaxException;
28
-import java.util.Arrays;
28
+
29
 import org.junit.Test;
29
 import org.junit.Test;
30
 import static org.junit.Assert.*;
30
 import static org.junit.Assert.*;
31
 
31
 
32
 public class URLHandlerTest {
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
     @Test
34
     @Test
54
     public void testSubstituteParams() throws MalformedURLException, URISyntaxException {
35
     public void testSubstituteParams() throws MalformedURLException, URISyntaxException {
55
         final Object[][] tests = new Object[][]{
36
         final Object[][] tests = new Object[][]{
63
             {new URI("protocol://@host:33/?foo+bar#frag"), "$query $fragment", "foo+bar frag"},
44
             {new URI("protocol://@host:33/?foo+bar#frag"), "$query $fragment", "foo+bar frag"},
64
             {new URI("host.com"), "$path $protocol$host", "host.com "},
45
             {new URI("host.com"), "$path $protocol$host", "host.com "},
65
         };
46
         };
66
-        
47
+
67
         for (Object[] test : tests) {
48
         for (Object[] test : tests) {
68
             final String result = URLHandler.substituteParams((URI) test[0],
49
             final String result = URLHandler.substituteParams((URI) test[0],
69
                     (String) test[1]);
50
                     (String) test[1]);
71
                     (String) test[2], result);
52
                     (String) test[2], result);
72
         }
53
         }
73
     }
54
     }
74
-    
75
 }
55
 }

Loading…
Cancel
Save