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 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 OPTIONS = new Options();
83
84 OPTIONS.addOption( OptionBuilder.withLongOpt( "help" ).withDescription( "Display help information." )
85 .create( HELP ) );
86 OPTIONS.addOption( OptionBuilder.withLongOpt( "version" ).withDescription( "Display version information." )
87 .create( VERSION ) );
88
89 OPTIONS.addOption( OptionBuilder.withLongOpt( "input" ).withDescription( "Input file or directory." )
90 .hasArg().create( IN ) );
91 OPTIONS.addOption( OptionBuilder.withLongOpt( "output" ).withDescription( "Output file or directory." )
92 .hasArg().create( OUT ) );
93 OPTIONS.addOption( OptionBuilder.withDescription( "From format. If not specified, try to autodetect it." )
94 .hasArg().create( FROM ) );
95 OPTIONS.addOption( OptionBuilder.withDescription( "To format." ).hasArg().create( TO ) );
96 OPTIONS.addOption( OptionBuilder.withLongOpt( "inputEncoding" )
97 .withDescription( "Input file encoding. "
98 + "If not specified, try to autodetect it." )
99 .hasArg().create( INENCODING ) );
100 OPTIONS.addOption( OptionBuilder.withLongOpt( "format" )
101 .withDescription( "Format the output (actually only xml based outputs) "
102 + " to be human readable." )
103 .create( FORMAT ) );
104 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 OPTIONS.addOption( OptionBuilder.withLongOpt( "debug" )
110 .withDescription( "Produce execution debug output." ).create( DEBUG ) );
111 OPTIONS.addOption( OptionBuilder.withLongOpt( "errors" )
112 .withDescription( "Produce execution error messages." ).create( ERRORS ) );
113 }
114
115
116
117
118
119
120
121 CommandLine parse( String[] args )
122 throws ParseException
123 {
124 if ( args == null )
125 {
126 throw new IllegalArgumentException( "args is required." );
127 }
128
129
130 String[] cleanArgs = cleanArgs( args );
131
132 CommandLineParser parser = new GnuParser();
133 return parser.parse( OPTIONS, cleanArgs );
134 }
135
136 static void displayHelp()
137 {
138 System.out.println();
139
140 HelpFormatter formatter = new HelpFormatter();
141 formatter.printHelp( "doxia [options] -in <arg> [-from <arg>] [-inEncoding <arg>] -out <arg> "
142 + "-to <arg> [-outEncoding <arg>]\n", "\nOptions:", OPTIONS, getSupportedFormatAndEncoding() );
143 }
144
145 private static String getSupportedFormatAndEncoding()
146 {
147 return getSupportedFormat() + "\n" + getSupportedEncoding();
148 }
149
150 private static String getSupportedFormat()
151 {
152 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 return "\nSupported Encoding:\n " + StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " );
160 }
161
162 private String[] cleanArgs( String[] args )
163 {
164 List cleaned = new ArrayList();
165
166 StringBuffer currentArg = null;
167
168 for ( int i = 0; i < args.length; i++ )
169 {
170 String arg = args[i];
171
172 boolean addedToBuffer = false;
173
174 if ( arg.startsWith( "\"" ) )
175 {
176
177
178 if ( currentArg != null )
179 {
180 cleaned.add( currentArg.toString() );
181 }
182
183
184 currentArg = new StringBuffer( arg.substring( 1 ) );
185 addedToBuffer = true;
186 }
187
188
189 if ( arg.endsWith( "\"" ) )
190 {
191 String cleanArgPart = arg.substring( 0, arg.length() - 1 );
192
193
194 if ( currentArg != null )
195 {
196
197 if ( addedToBuffer )
198 {
199 currentArg.setLength( currentArg.length() - 1 );
200 }
201
202 else
203 {
204
205 currentArg.append( ' ' ).append( cleanArgPart );
206 }
207
208
209 cleaned.add( currentArg.toString() );
210 }
211 else
212 {
213
214 cleaned.add( cleanArgPart );
215 }
216
217
218 currentArg = null;
219 continue;
220 }
221
222
223
224
225
226 if ( !addedToBuffer )
227 {
228
229 if ( currentArg != null )
230 {
231 currentArg.append( ' ' ).append( arg );
232 }
233
234 else
235 {
236 cleaned.add( arg );
237 }
238 }
239 }
240
241
242 if ( currentArg != null )
243 {
244 cleaned.add( currentArg.toString() );
245 }
246
247 int cleanedSz = cleaned.size();
248 String[] cleanArgs = null;
249
250 if ( cleanedSz == 0 )
251 {
252
253 cleanArgs = args;
254 }
255 else
256 {
257 cleanArgs = (String[]) cleaned.toArray( new String[cleanedSz] );
258 }
259
260 return cleanArgs;
261 }
262 }