1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.cli;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25 * CleanArgument
26 */
27 public class CleanArgument {
28 public static String[] cleanArgs(String[] args) {
29 List<String> cleaned = new ArrayList<>();
30
31 StringBuilder currentArg = null;
32
33 for (String arg : args) {
34 boolean addedToBuffer = false;
35
36 if (arg.startsWith("\"")) {
37 // if we're in the process of building up another arg, push it and start over.
38 // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
39 if (currentArg != null) {
40 cleaned.add(currentArg.toString());
41 }
42
43 // start building an argument here.
44 currentArg = new StringBuilder(arg.substring(1));
45 addedToBuffer = true;
46 }
47
48 // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
49 if (addedToBuffer && arg.endsWith("\"")) {
50 String cleanArgPart = arg.substring(0, arg.length() - 1);
51
52 // if we're building an argument, keep doing so.
53 if (currentArg != null) {
54 // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
55 if (addedToBuffer) {
56 currentArg.setLength(currentArg.length() - 1);
57 }
58 // otherwise, we trim the trailing " and append to the buffer.
59 else {
60 // TODO introducing a space here...not sure what else to do but collapse whitespace
61 currentArg.append(' ').append(cleanArgPart);
62 }
63
64 cleaned.add(currentArg.toString());
65 } else {
66 cleaned.add(cleanArgPart);
67 }
68
69 currentArg = null;
70 addedToBuffer = false;
71 continue;
72 }
73
74 // if we haven't added this arg to the buffer, and we ARE building an argument
75 // buffer, then append it with a preceding space...again, not sure what else to
76 // do other than collapse whitespace.
77 // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
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 }