Coverage Report - org.apache.maven.doxia.cli.CLIManager
 
Classes in this File Line Coverage Branch Coverage Complexity
CLIManager
0%
0/58
0%
0/22
3
 
 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.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  
  * Manager for Doxia converter CLI options.
 39  
  *
 40  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 41  
  * @version $Id: CLIManager.java 712860 2008-11-10 22:54:37Z hboutemy $
 42  
  */
 43  0
 class CLIManager
 44  
 {
 45  
     /** h character */
 46  
     static final char HELP = 'h';
 47  
 
 48  
     /** v character */
 49  
     static final char VERSION = 'v';
 50  
 
 51  
     /** in String */
 52  
     static final String IN = "in";
 53  
 
 54  
     /** out String */
 55  
     static final String OUT = "out";
 56  
 
 57  
     /** from String */
 58  
     static final String FROM = "from";
 59  
 
 60  
     /** to String */
 61  
     static final String TO = "to";
 62  
 
 63  
     /** inEncoding String */
 64  
     static final String INENCODING = "inEncoding";
 65  
 
 66  
     /** f character */
 67  
     static final char FORMAT = 'f';
 68  
 
 69  
     /** outEncoding String */
 70  
     static final String OUTENCODING = "outEncoding";
 71  
 
 72  
     /** X character */
 73  
     static final char DEBUG = 'X';
 74  
 
 75  
     /** e character */
 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  
      * @param args not null.
 117  
      * @return a not null command line.
 118  
      * @throws ParseException if any
 119  
      * @throws IllegalArgumentException is args is null
 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  
         // We need to eat any quotes surrounding arguments...
 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  
                 // if we're in the process of building up another arg, push it and start over.
 177  
                 // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
 178  0
                 if ( currentArg != null )
 179  
                 {
 180  0
                     cleaned.add( currentArg.toString() );
 181  
                 }
 182  
 
 183  
                 // start building an argument here.
 184  0
                 currentArg = new StringBuffer( arg.substring( 1 ) );
 185  0
                 addedToBuffer = true;
 186  
             }
 187  
 
 188  
             // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
 189  0
             if ( arg.endsWith( "\"" ) )
 190  
             {
 191  0
                 String cleanArgPart = arg.substring( 0, arg.length() - 1 );
 192  
 
 193  
                 // if we're building an argument, keep doing so.
 194  0
                 if ( currentArg != null )
 195  
                 {
 196  
                     // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
 197  0
                     if ( addedToBuffer )
 198  
                     {
 199  0
                         currentArg.setLength( currentArg.length() - 1 );
 200  
                     }
 201  
                     // otherwise, we trim the trailing " and append to the buffer.
 202  
                     else
 203  
                     {
 204  
                         // TODO: introducing a space here...not sure what else to do but collapse whitespace
 205  0
                         currentArg.append( ' ' ).append( cleanArgPart );
 206  
                     }
 207  
 
 208  
                     // we're done with this argument, so add it.
 209  0
                     cleaned.add( currentArg.toString() );
 210  
                 }
 211  
                 else
 212  
                 {
 213  
                     // this is a simple argument...just add it.
 214  0
                     cleaned.add( cleanArgPart );
 215  
                 }
 216  
 
 217  
                 // the currentArg MUST be finished when this completes.
 218  0
                 currentArg = null;
 219  0
                 continue;
 220  
             }
 221  
 
 222  
             // if we haven't added this arg to the buffer, and we ARE building an argument
 223  
             // buffer, then append it with a preceding space...again, not sure what else to
 224  
             // do other than collapse whitespace.
 225  
             // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
 226  0
             if ( !addedToBuffer )
 227  
             {
 228  
                 // append to the argument we're building, collapsing whitespace to a single space.
 229  0
                 if ( currentArg != null )
 230  
                 {
 231  0
                     currentArg.append( ' ' ).append( arg );
 232  
                 }
 233  
                 // this is a loner, just add it directly.
 234  
                 else
 235  
                 {
 236  0
                     cleaned.add( arg );
 237  
                 }
 238  
             }
 239  
         }
 240  
 
 241  
         // clean up.
 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  
             // if we didn't have any arguments to clean, simply pass the original array through
 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  
 }