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.cling.invoker;
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 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 // if we're in the process of building up another arg, push it and start over.
49 // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
50 if (currentArg != null) {
51 cleaned.add(currentArg.toString());
52 }
53
54 // start building an argument here.
55 currentArg = new StringBuilder(arg.substring(1));
56 addedToBuffer = true;
57 }
58
59 // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
60 if (addedToBuffer && arg.endsWith("\"")) {
61 // if we're building an argument, keep doing so.
62 // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
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 // 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 }