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