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