View Javadoc
1   /*
2    * #%L
3    * Mojo's Maven plugin for Cobertura
4    * %%
5    * Copyright (C) 2005 - 2013 Codehaus
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.codehaus.mojo.cobertura.tasks;
21  
22  import net.sourceforge.cobertura.util.CommandLineBuilder;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  /**
30   * CommandLineArguments allows for arbitrarily long command line argument lists.
31   *
32   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
33   */
34  public class CommandLineArguments
35  {
36      private List<String> args;
37  
38      private boolean useCommandsFile;
39  
40      /**
41       * Construct empty arg set.
42       */
43      public CommandLineArguments()
44      {
45          this.args = new ArrayList<String>();
46          this.useCommandsFile = false;
47      }
48  
49      /**
50       * Append an option.
51       *
52       * @param arg the argument.
53       */
54      public void addArg( String arg )
55      {
56          this.args.add( arg );
57      }
58  
59      /**
60       * Append two arguments (e.g. -x y)
61       *
62       * @param arg1 first arg
63       * @param arg2 second arg.
64       */
65      public void addArg( String arg1, String arg2 )
66      {
67          this.args.add( arg1 );
68          this.args.add( arg2 );
69      }
70  
71      /**
72       * @return the list of arg strings.
73       */
74      public List<String> getArgs()
75      {
76          return this.args;
77      }
78  
79      /**
80       * Generate the Commands file and return the filename to it.
81       *
82       * @return the commands filename.
83       * @throws IOException error writing the file.
84       */
85      public String getCommandsFile()
86          throws IOException
87      {
88          CommandLineBuilder builder = new CommandLineBuilder();
89          for ( String arg : this.args )
90          {
91              builder.addArg( arg );
92          }
93          builder.saveArgs();
94          return builder.getCommandLineFile();
95      }
96  
97      /**
98       * @return an iterator over the arg strings.
99       */
100     public Iterator<String> iterator()
101     {
102         return this.args.iterator();
103     }
104 
105     /**
106      * @param useCommandsFile The useCommandsFile to set.
107      */
108     public void setUseCommandsFile( boolean useCommandsFile )
109     {
110         this.useCommandsFile = useCommandsFile;
111     }
112 
113     /**
114      * @return Returns the useCommandsFile.
115      */
116     public boolean useCommandsFile()
117     {
118         return useCommandsFile;
119     }
120 }