View Javadoc
1   package org.codehaus.mojo.siteskinner;
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.OptionBuilder;
29  import org.apache.commons.cli.Options;
30  import org.apache.commons.cli.ParseException;
31  
32  /**
33   * Subset of org.apache.maven.cli.CLIManager, which in part of Maven3.
34   * 
35   * When migrating this plugin to M3, this class could be removed. 
36   * 
37   * @author Robert Scholte
38   * @since 1.1
39   */
40  public class CLIManager
41  {
42      public static final char SET_SYSTEM_PROPERTY = 'D';
43  
44      public static final char DEBUG = 'X';
45  
46      public static final char ACTIVATE_PROFILES = 'P';
47      
48      private Options options;
49  
50      @SuppressWarnings( "static-access" )
51      public CLIManager()
52      {
53          options = new Options();
54          options.addOption( OptionBuilder.withLongOpt( "define" ).hasArg().create( SET_SYSTEM_PROPERTY ) );
55          options.addOption( OptionBuilder.withLongOpt( "debug" ).create( DEBUG ) );
56          options.addOption( OptionBuilder.withLongOpt( "activate-profiles" ).hasArg().create( ACTIVATE_PROFILES ) );
57      }
58  
59      public CommandLine parse( String[] args )
60          throws ParseException
61      {
62          // We need to eat any quotes surrounding arguments...
63          String[] cleanArgs = cleanArgs( args );
64  
65          CommandLineParser parser = new GnuParser();
66  
67          return parser.parse( options, cleanArgs );
68      }
69  
70      private String[] cleanArgs( String[] args )
71      {
72          List<String> cleaned = new ArrayList<String>();
73  
74          StringBuilder currentArg = null;
75  
76          for ( int i = 0; i < args.length; i++ )
77          {
78              String arg = args[i];
79  
80              boolean addedToBuffer = false;
81  
82              if ( arg.startsWith( "\"" ) )
83              {
84                  // if we're in the process of building up another arg, push it and start over.
85                  // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
86                  if ( currentArg != null )
87                  {
88                      cleaned.add( currentArg.toString() );
89                  }
90  
91                  // start building an argument here.
92                  currentArg = new StringBuilder( arg.substring( 1 ) );
93                  addedToBuffer = true;
94              }
95  
96              // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
97              if ( arg.endsWith( "\"" ) )
98              {
99                  String cleanArgPart = arg.substring( 0, arg.length() - 1 );
100 
101                 // if we're building an argument, keep doing so.
102                 if ( currentArg != null )
103                 {
104                     // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
105                     if ( addedToBuffer )
106                     {
107                         currentArg.setLength( currentArg.length() - 1 );
108                     }
109                     // otherwise, we trim the trailing " and append to the buffer.
110                     else
111                     {
112                         // TODO: introducing a space here...not sure what else to do but collapse whitespace
113                         currentArg.append( ' ' ).append( cleanArgPart );
114                     }
115 
116                     cleaned.add( currentArg.toString() );
117                 }
118                 else
119                 {
120                     cleaned.add( cleanArgPart );
121                 }
122 
123                 currentArg = null;
124 
125                 continue;
126             }
127 
128             // if we haven't added this arg to the buffer, and we ARE building an argument
129             // buffer, then append it with a preceding space...again, not sure what else to
130             // do other than collapse whitespace.
131             // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
132             if ( !addedToBuffer )
133             {
134                 if ( currentArg != null )
135                 {
136                     currentArg.append( ' ' ).append( arg );
137                 }
138                 else
139                 {
140                     cleaned.add( arg );
141                 }
142             }
143         }
144 
145         if ( currentArg != null )
146         {
147             cleaned.add( currentArg.toString() );
148         }
149 
150         int cleanedSz = cleaned.size();
151 
152         String[] cleanArgs = null;
153 
154         if ( cleanedSz == 0 )
155         {
156             cleanArgs = args;
157         }
158         else
159         {
160             cleanArgs = cleaned.toArray( new String[cleanedSz] );
161         }
162 
163         return cleanArgs;
164     }
165 
166 }