1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling.invoker;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27 public class CleanArgument {
28 public static String[] cleanArgs(String[] args) {
29 try {
30 return doCleanArgs(args);
31 } catch (RuntimeException e) {
32 for (String a : args) {
33 System.out.println("Arg: '" + a + "'");
34 }
35 throw e;
36 }
37 }
38
39 private static String[] doCleanArgs(String[] args) {
40 List<String> cleaned = new ArrayList<>();
41
42 StringBuilder currentArg = null;
43
44 for (String arg : args) {
45 boolean addedToBuffer = false;
46
47 if (arg.startsWith("\"")) {
48
49
50 if (currentArg != null) {
51 cleaned.add(currentArg.toString());
52 }
53
54
55 currentArg = new StringBuilder(arg.substring(1));
56 addedToBuffer = true;
57 }
58
59
60 if (addedToBuffer && arg.endsWith("\"")) {
61
62
63 if (!currentArg.isEmpty()) {
64 currentArg.setLength(currentArg.length() - 1);
65 }
66
67 cleaned.add(currentArg.toString());
68
69 currentArg = null;
70 addedToBuffer = false;
71 continue;
72 }
73
74
75
76
77
78 if (!addedToBuffer) {
79 if (currentArg != null) {
80 currentArg.append(' ').append(arg);
81 } else {
82 cleaned.add(arg);
83 }
84 }
85 }
86
87 if (currentArg != null) {
88 cleaned.add(currentArg.toString());
89 }
90
91 int cleanedSz = cleaned.size();
92
93 String[] cleanArgs;
94
95 if (cleanedSz == 0) {
96 cleanArgs = args;
97 } else {
98 cleanArgs = cleaned.toArray(new String[0]);
99 }
100
101 return cleanArgs;
102 }
103 }