1 package org.apache.maven.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41 if ( currentArg != null )
42 {
43 cleaned.add( currentArg.toString() );
44 }
45
46
47 currentArg = new StringBuilder( arg.substring( 1 ) );
48 addedToBuffer = true;
49 }
50
51
52 if ( addedToBuffer && arg.endsWith( "\"" ) )
53 {
54 String cleanArgPart = arg.substring( 0, arg.length() - 1 );
55
56
57 if ( currentArg != null )
58 {
59
60 if ( addedToBuffer )
61 {
62 currentArg.setLength( currentArg.length() - 1 );
63 }
64
65 else
66 {
67
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
84
85
86
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 }