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 @Deprecated
28 public class CleanArgument {
29 public static String[] cleanArgs(String[] args) {
30 try {
31 return doCleanArgs(args);
32 } catch (RuntimeException e) {
33 for (String a : args) {
34 System.out.println("Arg: '" + a + "'");
35 }
36 throw e;
37 }
38 }
39
40 private static String[] doCleanArgs(String[] args) {
41 List<String> cleaned = new ArrayList<>();
42
43 StringBuilder currentArg = null;
44
45 for (String arg : args) {
46 boolean addedToBuffer = false;
47
48 if (arg.startsWith("\"")) {
49 // if we're in the process of building up another arg, push it and start over.
50 // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
51 if (currentArg != null) {
52 cleaned.add(currentArg.toString());
53 }
54
55 // start building an argument here.
56 currentArg = new StringBuilder(arg.substring(1));
57 addedToBuffer = true;
58 }
59
60 // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
61 if (addedToBuffer && arg.endsWith("\"")) {
62 // if we're building an argument, keep doing so.
63 // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
64 if (!currentArg.isEmpty()) {
65 currentArg.setLength(currentArg.length() - 1);
66 }
67
68 cleaned.add(currentArg.toString());
69
70 currentArg = null;
71 addedToBuffer = false;
72 continue;
73 }
74
75 // if we haven't added this arg to the buffer, and we ARE building an argument
76 // buffer, then append it with a preceding space...again, not sure what else to
77 // do other than collapse whitespace.
78 // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
79 if (!addedToBuffer) {
80 if (currentArg != null) {
81 currentArg.append(' ').append(arg);
82 } else {
83 cleaned.add(arg);
84 }
85 }
86 }
87
88 if (currentArg != null) {
89 cleaned.add(currentArg.toString());
90 }
91
92 int cleanedSz = cleaned.size();
93
94 String[] cleanArgs;
95
96 if (cleanedSz == 0) {
97 cleanArgs = args;
98 } else {
99 cleanArgs = cleaned.toArray(new String[0]);
100 }
101
102 return cleanArgs;
103 }
104 }