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.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.UnsupportedEncodingException;
26 import java.util.Properties;
27
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.ParseException;
30 import org.apache.maven.doxia.Converter;
31 import org.apache.maven.doxia.ConverterException;
32 import org.apache.maven.doxia.DefaultConverter;
33 import org.apache.maven.doxia.UnsupportedFormatException;
34 import org.apache.maven.doxia.logging.Log;
35 import org.apache.maven.doxia.logging.SystemStreamLog;
36 import org.apache.maven.doxia.wrapper.InputFileWrapper;
37 import org.apache.maven.doxia.wrapper.OutputFileWrapper;
38 import org.codehaus.plexus.util.Os;
39
40
41
42
43
44
45
46 public class ConverterCli
47 {
48
49
50
51
52
53
54
55 public static void main( String[] args )
56 {
57 if ( args == null || args.length == 0 )
58 {
59 args = new String[] { "-h" };
60 }
61 System.exit( ConverterCli.doMain( args ) );
62 }
63
64
65
66
67 private static int doMain( String[] args )
68 {
69
70
71
72
73 CLIManager cliManager = new CLIManager();
74
75 CommandLine commandLine;
76 try
77 {
78 commandLine = cliManager.parse( args );
79 }
80 catch ( ParseException e )
81 {
82 System.err.println( "Unable to parse command line options: " + e.getMessage() );
83 CLIManager.displayHelp();
84
85 return 1;
86 }
87
88 if ( "1.4".compareTo( System.getProperty( "java.specification.version" ) ) > 0 )
89 {
90 System.err.println( "Sorry, but JDK 1.4 or above is required to execute Doxia. You appear to be using "
91 + "Java:" );
92 System.err.println( "java version \"" + System.getProperty( "java.version", "<unknown java version>" )
93 + "\"" );
94 System.err.println( System.getProperty( "java.runtime.name", "<unknown runtime name>" ) + " (build "
95 + System.getProperty( "java.runtime.version", "<unknown runtime version>" ) + ")" );
96 System.err.println( System.getProperty( "java.vm.name", "<unknown vm name>" ) + " (build "
97 + System.getProperty( "java.vm.version", "<unknown vm version>" ) + ", "
98 + System.getProperty( "java.vm.info", "<unknown vm info>" ) + ")" );
99
100 return 1;
101 }
102
103 if ( commandLine.hasOption( CLIManager.HELP ) )
104 {
105 CLIManager.displayHelp();
106
107 return 0;
108 }
109
110 if ( commandLine.hasOption( CLIManager.VERSION ) )
111 {
112 showVersion();
113
114 return 0;
115 }
116
117 boolean debug = commandLine.hasOption( CLIManager.DEBUG );
118
119 boolean showErrors = debug || commandLine.hasOption( CLIManager.ERRORS );
120
121 if ( showErrors )
122 {
123 System.out.println( "+ Error stacktraces are turned on." );
124 }
125
126 Converter converter = new DefaultConverter();
127 Log log = new SystemStreamLog();
128 if ( debug )
129 {
130 log.setLogLevel( Log.LEVEL_DEBUG );
131 }
132 converter.enableLogging( log );
133
134 InputFileWrapper input;
135 OutputFileWrapper output;
136 try
137 {
138 input =
139 InputFileWrapper.valueOf( commandLine.getOptionValue( CLIManager.IN ),
140 commandLine.getOptionValue( CLIManager.FROM ),
141 commandLine.getOptionValue( CLIManager.INENCODING ),
142 converter.getInputFormats() );
143 output =
144 OutputFileWrapper.valueOf( commandLine.getOptionValue( CLIManager.OUT ),
145 commandLine.getOptionValue( CLIManager.TO ),
146 commandLine.getOptionValue( CLIManager.OUTENCODING ),
147 converter.getOutputFormats() );
148 }
149 catch ( IllegalArgumentException e )
150 {
151 showFatalError( "Illegal argument: " + e.getMessage(), e, showErrors );
152
153 CLIManager.displayHelp();
154
155 return 1;
156 }
157 catch ( UnsupportedEncodingException e )
158 {
159 showFatalError( e.getMessage(), e, showErrors );
160
161 return 1;
162 }
163 catch ( FileNotFoundException e )
164 {
165 showFatalError( e.getMessage(), e, showErrors );
166
167 return 1;
168 }
169
170 boolean format = commandLine.hasOption( CLIManager.FORMAT );
171 converter.setFormatOutput( format );
172
173 try
174 {
175 converter.convert( input, output );
176 }
177 catch ( UnsupportedFormatException e )
178 {
179 showFatalError( e.getMessage(), e, showErrors );
180
181 return 1;
182 }
183 catch ( ConverterException e )
184 {
185 showFatalError( "Converter exception: " + e.getMessage(), e, showErrors );
186
187 return 1;
188 }
189 catch ( IllegalArgumentException e )
190 {
191 showFatalError( "Illegal argument: " + e.getMessage(), e, showErrors );
192
193 return 1;
194 }
195 catch ( RuntimeException e )
196 {
197 showFatalError( "Runtime exception: " + e.getMessage(), e, showErrors );
198
199 return 1;
200 }
201
202 return 0;
203 }
204
205 private static void showVersion()
206 {
207 InputStream resourceAsStream;
208 try
209 {
210 Properties properties = new Properties();
211 resourceAsStream = ConverterCli.class.getClassLoader()
212 .getResourceAsStream( "META-INF/maven/org.apache.maven.doxia/doxia-converter/pom.properties" );
213
214 if ( resourceAsStream != null )
215 {
216 properties.load( resourceAsStream );
217
218 if ( properties.getProperty( "builtOn" ) != null )
219 {
220 System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" )
221 + " built on " + properties.getProperty( "builtOn" ) );
222 }
223 else
224 {
225 System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" ) );
226 }
227 }
228 else
229 {
230 System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" ) );
231 }
232
233 System.out.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" ) );
234
235 System.out.println( "OS name: \"" + Os.OS_NAME + "\" version: \"" + Os.OS_VERSION + "\" arch: \""
236 + Os.OS_ARCH + "\" family: \"" + Os.OS_FAMILY + "\"" );
237
238 }
239 catch ( IOException e )
240 {
241 System.err.println( "Unable to determine version from JAR file: " + e.getMessage() );
242 }
243 }
244
245 private static void showFatalError( String message, Exception e, boolean show )
246 {
247 System.err.println( "FATAL ERROR: " + message );
248 if ( show )
249 {
250 System.err.println( "Error stacktrace:" );
251
252 e.printStackTrace();
253 }
254 else
255 {
256 System.err.println( "For more information, run with the -e flag" );
257 }
258 }
259 }