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
26
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
43
44 if ( currentArg != null )
45 {
46 cleaned.add( currentArg.toString() );
47 }
48
49
50 currentArg = new StringBuilder( arg.substring( 1 ) );
51 addedToBuffer = true;
52 }
53
54
55 if ( addedToBuffer && arg.endsWith( "\"" ) )
56 {
57 String cleanArgPart = arg.substring( 0, arg.length() - 1 );
58
59
60 if ( currentArg != null )
61 {
62
63 if ( addedToBuffer )
64 {
65 currentArg.setLength( currentArg.length() - 1 );
66 }
67
68 else
69 {
70
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
87
88
89
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 }