1 | |
package org.apache.maven.doxia.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 | |
import org.apache.commons.cli.CommandLine; |
26 | |
import org.apache.commons.cli.CommandLineParser; |
27 | |
import org.apache.commons.cli.GnuParser; |
28 | |
import org.apache.commons.cli.HelpFormatter; |
29 | |
import org.apache.commons.cli.OptionBuilder; |
30 | |
import org.apache.commons.cli.Options; |
31 | |
import org.apache.commons.cli.ParseException; |
32 | |
import org.apache.maven.doxia.DefaultConverter; |
33 | |
import org.codehaus.plexus.util.StringUtils; |
34 | |
|
35 | |
import com.ibm.icu.text.CharsetDetector; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | class CLIManager |
44 | |
{ |
45 | |
|
46 | |
static final char HELP = 'h'; |
47 | |
|
48 | |
|
49 | |
static final char VERSION = 'v'; |
50 | |
|
51 | |
|
52 | |
static final String IN = "in"; |
53 | |
|
54 | |
|
55 | |
static final String OUT = "out"; |
56 | |
|
57 | |
|
58 | |
static final String FROM = "from"; |
59 | |
|
60 | |
|
61 | |
static final String TO = "to"; |
62 | |
|
63 | |
|
64 | |
static final String INENCODING = "inEncoding"; |
65 | |
|
66 | |
|
67 | |
static final char FORMAT = 'f'; |
68 | |
|
69 | |
|
70 | |
static final String OUTENCODING = "outEncoding"; |
71 | |
|
72 | |
|
73 | |
static final char DEBUG = 'X'; |
74 | |
|
75 | |
|
76 | |
static final char ERRORS = 'e'; |
77 | |
|
78 | |
private static final Options OPTIONS; |
79 | |
|
80 | |
static |
81 | |
{ |
82 | 0 | OPTIONS = new Options(); |
83 | |
|
84 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "help" ).withDescription( "Display help information." ) |
85 | |
.create( HELP ) ); |
86 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "version" ).withDescription( "Display version information." ) |
87 | |
.create( VERSION ) ); |
88 | |
|
89 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "input" ).withDescription( "Input file or directory." ) |
90 | |
.hasArg().create( IN ) ); |
91 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "output" ).withDescription( "Output file or directory." ) |
92 | |
.hasArg().create( OUT ) ); |
93 | 0 | OPTIONS.addOption( OptionBuilder.withDescription( "From format. If not specified, try to autodetect it." ) |
94 | |
.hasArg().create( FROM ) ); |
95 | 0 | OPTIONS.addOption( OptionBuilder.withDescription( "To format." ).hasArg().create( TO ) ); |
96 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "inputEncoding" ) |
97 | |
.withDescription( "Input file encoding. " |
98 | |
+ "If not specified, try to autodetect it." ) |
99 | |
.hasArg().create( INENCODING ) ); |
100 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "format" ) |
101 | |
.withDescription( "Format the output (actually only xml based outputs) " |
102 | |
+ " to be human readable." ) |
103 | |
.create( FORMAT ) ); |
104 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "outputEncoding" ) |
105 | |
.withDescription( "Output file encoding. If not specified, use the " |
106 | |
+ "input encoding (or autodetected)." ).hasArg() |
107 | |
.create( OUTENCODING ) ); |
108 | |
|
109 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "debug" ) |
110 | |
.withDescription( "Produce execution debug output." ).create( DEBUG ) ); |
111 | 0 | OPTIONS.addOption( OptionBuilder.withLongOpt( "errors" ) |
112 | |
.withDescription( "Produce execution error messages." ).create( ERRORS ) ); |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
CommandLine parse( String[] args ) |
122 | |
throws ParseException |
123 | |
{ |
124 | 0 | if ( args == null ) |
125 | |
{ |
126 | 0 | throw new IllegalArgumentException( "args is required." ); |
127 | |
} |
128 | |
|
129 | |
|
130 | 0 | String[] cleanArgs = cleanArgs( args ); |
131 | |
|
132 | 0 | CommandLineParser parser = new GnuParser(); |
133 | 0 | return parser.parse( OPTIONS, cleanArgs ); |
134 | |
} |
135 | |
|
136 | |
static void displayHelp() |
137 | |
{ |
138 | 0 | System.out.println(); |
139 | |
|
140 | 0 | HelpFormatter formatter = new HelpFormatter(); |
141 | 0 | formatter.printHelp( "doxia [options] -in <arg> [-from <arg>] [-inEncoding <arg>] -out <arg> " |
142 | |
+ "-to <arg> [-outEncoding <arg>]\n", "\nOptions:", OPTIONS, getSupportedFormatAndEncoding() ); |
143 | 0 | } |
144 | |
|
145 | |
private static String getSupportedFormatAndEncoding() |
146 | |
{ |
147 | 0 | return getSupportedFormat() + "\n" + getSupportedEncoding(); |
148 | |
} |
149 | |
|
150 | |
private static String getSupportedFormat() |
151 | |
{ |
152 | 0 | return "\nSupported Formats:\n from: " + StringUtils.join( DefaultConverter.SUPPORTED_FROM_FORMAT, ", " ) |
153 | |
+ " or autodetect" + "\n out: " + StringUtils.join( DefaultConverter.SUPPORTED_TO_FORMAT, ", " ) |
154 | |
+ "\n"; |
155 | |
} |
156 | |
|
157 | |
private static String getSupportedEncoding() |
158 | |
{ |
159 | 0 | return "\nSupported Encoding:\n " + StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ); |
160 | |
} |
161 | |
|
162 | |
private String[] cleanArgs( String[] args ) |
163 | |
{ |
164 | 0 | List cleaned = new ArrayList(); |
165 | |
|
166 | 0 | StringBuffer currentArg = null; |
167 | |
|
168 | 0 | for ( int i = 0; i < args.length; i++ ) |
169 | |
{ |
170 | 0 | String arg = args[i]; |
171 | |
|
172 | 0 | boolean addedToBuffer = false; |
173 | |
|
174 | 0 | if ( arg.startsWith( "\"" ) ) |
175 | |
{ |
176 | |
|
177 | |
|
178 | 0 | if ( currentArg != null ) |
179 | |
{ |
180 | 0 | cleaned.add( currentArg.toString() ); |
181 | |
} |
182 | |
|
183 | |
|
184 | 0 | currentArg = new StringBuffer( arg.substring( 1 ) ); |
185 | 0 | addedToBuffer = true; |
186 | |
} |
187 | |
|
188 | |
|
189 | 0 | if ( arg.endsWith( "\"" ) ) |
190 | |
{ |
191 | 0 | String cleanArgPart = arg.substring( 0, arg.length() - 1 ); |
192 | |
|
193 | |
|
194 | 0 | if ( currentArg != null ) |
195 | |
{ |
196 | |
|
197 | 0 | if ( addedToBuffer ) |
198 | |
{ |
199 | 0 | currentArg.setLength( currentArg.length() - 1 ); |
200 | |
} |
201 | |
|
202 | |
else |
203 | |
{ |
204 | |
|
205 | 0 | currentArg.append( ' ' ).append( cleanArgPart ); |
206 | |
} |
207 | |
|
208 | |
|
209 | 0 | cleaned.add( currentArg.toString() ); |
210 | |
} |
211 | |
else |
212 | |
{ |
213 | |
|
214 | 0 | cleaned.add( cleanArgPart ); |
215 | |
} |
216 | |
|
217 | |
|
218 | 0 | currentArg = null; |
219 | 0 | continue; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | 0 | if ( !addedToBuffer ) |
227 | |
{ |
228 | |
|
229 | 0 | if ( currentArg != null ) |
230 | |
{ |
231 | 0 | currentArg.append( ' ' ).append( arg ); |
232 | |
} |
233 | |
|
234 | |
else |
235 | |
{ |
236 | 0 | cleaned.add( arg ); |
237 | |
} |
238 | |
} |
239 | |
} |
240 | |
|
241 | |
|
242 | 0 | if ( currentArg != null ) |
243 | |
{ |
244 | 0 | cleaned.add( currentArg.toString() ); |
245 | |
} |
246 | |
|
247 | 0 | int cleanedSz = cleaned.size(); |
248 | 0 | String[] cleanArgs = null; |
249 | |
|
250 | 0 | if ( cleanedSz == 0 ) |
251 | |
{ |
252 | |
|
253 | 0 | cleanArgs = args; |
254 | |
} |
255 | |
else |
256 | |
{ |
257 | 0 | cleanArgs = (String[]) cleaned.toArray( new String[cleanedSz] ); |
258 | |
} |
259 | |
|
260 | 0 | return cleanArgs; |
261 | |
} |
262 | |
} |