View Javadoc
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                  // if we're building an argument, keep doing so.
51                  // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
52                  currentArg.setLength(currentArg.length() - 1);
53  
54                  cleaned.add(currentArg.toString());
55  
56                  currentArg = null;
57                  addedToBuffer = false;
58                  continue;
59              }
60  
61              // if we haven't added this arg to the buffer, and we ARE building an argument
62              // buffer, then append it with a preceding space...again, not sure what else to
63              // do other than collapse whitespace.
64              // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
65              if (!addedToBuffer) {
66                  if (currentArg != null) {
67                      currentArg.append(' ').append(arg);
68                  } else {
69                      cleaned.add(arg);
70                  }
71              }
72          }
73  
74          if (currentArg != null) {
75              cleaned.add(currentArg.toString());
76          }
77  
78          int cleanedSz = cleaned.size();
79  
80          String[] cleanArgs;
81  
82          if (cleanedSz == 0) {
83              cleanArgs = args;
84          } else {
85              cleanArgs = cleaned.toArray(new String[0]);
86          }
87  
88          return cleanArgs;
89      }
90  }