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