Browse Source

Handle url handlers (heh) with quotes in properly.

Issue 1228

git-svn-id: http://svn.dmdirc.com/trunk@4064 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
90b24d3077
2 changed files with 87 additions and 3 deletions
  1. 40
    3
      src/com/dmdirc/util/URLHandler.java
  2. 47
    0
      test/com/dmdirc/util/URLHandlerTest.java

+ 40
- 3
src/com/dmdirc/util/URLHandler.java View File

@@ -33,7 +33,9 @@ import java.io.IOException;
33 33
 import java.net.URI;
34 34
 import java.net.URISyntaxException;
35 35
 import java.net.URL;
36
+import java.util.ArrayList;
36 37
 import java.util.Date;
38
+import java.util.List;
37 39
 
38 40
 /** Handles URLs. */
39 41
 public class URLHandler {
@@ -223,15 +225,50 @@ public class URLHandler {
223 225
      * @param command Application and arguments
224 226
      */
225 227
     private void execApp(final String command) {
226
-        String[] commandLine = command.split(" ");
227
-
228 228
         try {
229
-            Runtime.getRuntime().exec(commandLine);
229
+            Runtime.getRuntime().exec(parseArguments(command));
230 230
         } catch (IOException ex) {
231 231
             Logger.userError(ErrorLevel.LOW, "Unable to run application: ",
232 232
                     ex.getMessage());
233 233
         }
234 234
     }
235
+    
236
+    /**
237
+     * Parses the specified command into an array of arguments. Arguments are
238
+     * separated by spaces. Multi-word arguments may be specified by starting
239
+     * the argument with a quote (") and finishing it with a quote (").
240
+     * 
241
+     * @param command The command to parse
242
+     * @return An array of arguments corresponding to the command
243
+     */
244
+    protected static String[] parseArguments(final String command) {
245
+        final List<String> args = new ArrayList<String>();
246
+        final StringBuilder builder = new StringBuilder();
247
+        boolean inquote = false;
248
+        
249
+        for (String word : command.split(" ")) {
250
+            if (word.endsWith("\"") && inquote) {
251
+                args.add(builder.toString() + ' ' + word.substring(0, word.length() - 1));
252
+                builder.delete(0, builder.length());
253
+                inquote = false;
254
+            } else if (inquote) {
255
+                if (builder.length() > 0) {
256
+                    builder.append(' ');
257
+                }
258
+                
259
+                builder.append(word);
260
+            } else if (word.startsWith("\"") && !word.endsWith("\"")) {
261
+                inquote = true;
262
+                builder.append(word.substring(1));
263
+            } else if (word.startsWith("\"") && word.endsWith("\"")) {
264
+                args.add(word.substring(1, word.length() - 1));
265
+            } else {
266
+                args.add(word);
267
+            }
268
+        }
269
+        
270
+        return args.toArray(new String[0]);
271
+    }
235 272
 
236 273
     /**
237 274
      * Opens the specified URL in the users browser.

+ 47
- 0
test/com/dmdirc/util/URLHandlerTest.java View File

@@ -0,0 +1,47 @@
1
+/*
2
+ * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.util;
24
+
25
+import java.util.Arrays;
26
+import org.junit.Test;
27
+import static org.junit.Assert.*;
28
+
29
+public class URLHandlerTest extends junit.framework.TestCase {
30
+    
31
+    @Test
32
+    public void testParseArguments() {
33
+        final String[][][] tests = new String[][][]{
34
+            {{"abcdef abcdef abcdef"}, {"abcdef", "abcdef", "abcdef"}},
35
+            {{"abcdef \"abcdef abcdef\""}, {"abcdef", "abcdef abcdef"}},
36
+            {{"abcdef \"abcdef\" \"abcdef\""}, {"abcdef", "abcdef", "abcdef"}},
37
+        };
38
+        
39
+        for (String[][] test : tests) {
40
+            final String[] res = URLHandler.parseArguments(test[0][0]);
41
+            assertTrue(test[0][0] + " - " + Arrays.toString(test[1]) + " - "
42
+                    + Arrays.toString(res),
43
+                    Arrays.equals(test[1], res));
44
+        }
45
+    }
46
+    
47
+}

Loading…
Cancel
Save