Coverage Report - org.apache.maven.doxia.cli.ConverterCli
 
Classes in this File Line Coverage Branch Coverage Complexity
ConverterCli
0%
0/83
0%
0/26
8,25
 
 1  
 package org.apache.maven.doxia.cli;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
  * Doxia converter CLI.
 42  
  *
 43  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 44  
  * @version $Id: ConverterCli.java 786981 2009-06-21 10:01:58Z ltheussl $
 45  
  */
 46  0
 public class ConverterCli
 47  
 {
 48  
     /**
 49  
      * Default main which terminates the JVM with <code>0</code> if no errors occurs.
 50  
      *
 51  
      * @param args command line args.
 52  
      * @see #doMain(String[])
 53  
      * @see System#exit(int)
 54  
      */
 55  
     public static void main( String[] args )
 56  
     {
 57  0
         if ( args == null || args.length == 0 )
 58  
         {
 59  0
             args = new String[] { "-h" };
 60  
         }
 61  0
         System.exit( ConverterCli.doMain( args ) );
 62  0
     }
 63  
 
 64  
     /**
 65  
      * @param args
 66  
      */
 67  
     private static int doMain( String[] args )
 68  
     {
 69  
         // ----------------------------------------------------------------------
 70  
         // Setup the command line parser
 71  
         // ----------------------------------------------------------------------
 72  
 
 73  0
         CLIManager cliManager = new CLIManager();
 74  
 
 75  
         CommandLine commandLine;
 76  
         try
 77  
         {
 78  0
             commandLine = cliManager.parse( args );
 79  
         }
 80  0
         catch ( ParseException e )
 81  
         {
 82  0
             System.err.println( "Unable to parse command line options: " + e.getMessage() );
 83  0
             CLIManager.displayHelp();
 84  
 
 85  0
             return 1;
 86  0
         }
 87  
 
 88  0
         if ( "1.4".compareTo( System.getProperty( "java.specification.version" ) ) > 0 )
 89  
         {
 90  0
             System.err.println( "Sorry, but JDK 1.4 or above is required to execute Doxia. You appear to be using "
 91  
                 + "Java:" );
 92  0
             System.err.println( "java version \"" + System.getProperty( "java.version", "<unknown java version>" )
 93  
                 + "\"" );
 94  0
             System.err.println( System.getProperty( "java.runtime.name", "<unknown runtime name>" ) + " (build "
 95  
                 + System.getProperty( "java.runtime.version", "<unknown runtime version>" ) + ")" );
 96  0
             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  0
             return 1;
 101  
         }
 102  
 
 103  0
         if ( commandLine.hasOption( CLIManager.HELP ) )
 104  
         {
 105  0
             CLIManager.displayHelp();
 106  
 
 107  0
             return 0;
 108  
         }
 109  
 
 110  0
         if ( commandLine.hasOption( CLIManager.VERSION ) )
 111  
         {
 112  0
             showVersion();
 113  
 
 114  0
             return 0;
 115  
         }
 116  
 
 117  0
         boolean debug = commandLine.hasOption( CLIManager.DEBUG );
 118  
 
 119  0
         boolean showErrors = debug || commandLine.hasOption( CLIManager.ERRORS );
 120  
 
 121  0
         if ( showErrors )
 122  
         {
 123  0
             System.out.println( "+ Error stacktraces are turned on." );
 124  
         }
 125  
 
 126  0
         Converter converter = new DefaultConverter();
 127  0
         Log log = new SystemStreamLog();
 128  0
         if ( debug )
 129  
         {
 130  0
             log.setLogLevel( Log.LEVEL_DEBUG );
 131  
         }
 132  0
         converter.enableLogging( log );
 133  
 
 134  
         InputFileWrapper input;
 135  
         OutputFileWrapper output;
 136  
         try
 137  
         {
 138  0
             input =
 139  
                 InputFileWrapper.valueOf( commandLine.getOptionValue( CLIManager.IN ),
 140  
                                           commandLine.getOptionValue( CLIManager.FROM ),
 141  
                                           commandLine.getOptionValue( CLIManager.INENCODING ),
 142  
                                           converter.getInputFormats() );
 143  0
             output =
 144  
                 OutputFileWrapper.valueOf( commandLine.getOptionValue( CLIManager.OUT ),
 145  
                                            commandLine.getOptionValue( CLIManager.TO ),
 146  
                                            commandLine.getOptionValue( CLIManager.OUTENCODING ),
 147  
                                            converter.getOutputFormats() );
 148  
         }
 149  0
         catch ( IllegalArgumentException e )
 150  
         {
 151  0
             showFatalError( "Illegal argument: " + e.getMessage(), e, showErrors );
 152  
 
 153  0
             CLIManager.displayHelp();
 154  
 
 155  0
             return 1;
 156  
         }
 157  0
         catch ( UnsupportedEncodingException e )
 158  
         {
 159  0
             showFatalError( e.getMessage(), e, showErrors );
 160  
 
 161  0
             return 1;
 162  
         }
 163  0
         catch ( FileNotFoundException e )
 164  
         {
 165  0
             showFatalError( e.getMessage(), e, showErrors );
 166  
 
 167  0
             return 1;
 168  0
         }
 169  
 
 170  0
         boolean format = commandLine.hasOption( CLIManager.FORMAT );
 171  0
         converter.setFormatOutput( format );
 172  
 
 173  
         try
 174  
         {
 175  0
             converter.convert( input, output );
 176  
         }
 177  0
         catch ( UnsupportedFormatException e )
 178  
         {
 179  0
             showFatalError( e.getMessage(), e, showErrors );
 180  
 
 181  0
             return 1;
 182  
         }
 183  0
         catch ( ConverterException e )
 184  
         {
 185  0
             showFatalError( "Converter exception: " + e.getMessage(), e, showErrors );
 186  
 
 187  0
             return 1;
 188  
         }
 189  0
         catch ( IllegalArgumentException e )
 190  
         {
 191  0
             showFatalError( "Illegal argument: " + e.getMessage(), e, showErrors );
 192  
 
 193  0
             return 1;
 194  
         }
 195  0
         catch ( RuntimeException e )
 196  
         {
 197  0
             showFatalError( "Runtime exception: " + e.getMessage(), e, showErrors );
 198  
 
 199  0
             return 1;
 200  0
         }
 201  
 
 202  0
         return 0;
 203  
     }
 204  
 
 205  
     private static void showVersion()
 206  
     {
 207  
         InputStream resourceAsStream;
 208  
         try
 209  
         {
 210  0
             Properties properties = new Properties();
 211  0
             resourceAsStream = ConverterCli.class.getClassLoader()
 212  
                 .getResourceAsStream( "META-INF/maven/org.apache.maven.doxia/doxia-converter/pom.properties" );
 213  
 
 214  0
             if ( resourceAsStream != null )
 215  
             {
 216  0
                 properties.load( resourceAsStream );
 217  
 
 218  0
                 if ( properties.getProperty( "builtOn" ) != null )
 219  
                 {
 220  0
                     System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" )
 221  
                         + " built on " + properties.getProperty( "builtOn" ) );
 222  
                 }
 223  
                 else
 224  
                 {
 225  0
                     System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" ) );
 226  
                 }
 227  
             }
 228  
             else
 229  
             {
 230  0
                 System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" ) );
 231  
             }
 232  
 
 233  0
             System.out.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" ) );
 234  
 
 235  0
             System.out.println( "OS name: \"" + Os.OS_NAME + "\" version: \"" + Os.OS_VERSION + "\" arch: \""
 236  
                 + Os.OS_ARCH + "\" family: \"" + Os.OS_FAMILY + "\"" );
 237  
 
 238  
         }
 239  0
         catch ( IOException e )
 240  
         {
 241  0
             System.err.println( "Unable to determine version from JAR file: " + e.getMessage() );
 242  0
         }
 243  0
     }
 244  
 
 245  
     private static void showFatalError( String message, Exception e, boolean show )
 246  
     {
 247  0
         System.err.println( "FATAL ERROR: " + message );
 248  0
         if ( show )
 249  
         {
 250  0
             System.err.println( "Error stacktrace:" );
 251  
 
 252  0
             e.printStackTrace();
 253  
         }
 254  
         else
 255  
         {
 256  0
             System.err.println( "For more information, run with the -e flag" );
 257  
         }
 258  0
     }
 259  
 }