KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > CommandLineBuilder


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Grzegorz Lukasik
5  *
6  * Note: This file is dual licensed under the GPL and the Apache
7  * Source License (so that it can be used from both the main
8  * Cobertura classes and the ant tasks).
9  *
10  * Cobertura is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2 of the License,
13  * or (at your option) any later version.
14  *
15  * Cobertura is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Cobertura; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  */

25
26 package net.sourceforge.cobertura.util;
27
28 import java.io.BufferedReader JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.FileWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.apache.log4j.Logger;
37
38 /**
39  * Helper class for storing long command lines inside temporary file.
40  * <p>
41  * Typical usage:
42  *
43  * <pre>
44  * builder = new CommandLineBuilder();
45  * builder.addArg(&quot;--someoption&quot;);
46  * builder.addArg(&quot;optionValue&quot;);
47  * ...
48  * builder.saveArgs();
49  * doSomething(builder.getCommandLineFile());
50  * builder.dispose();
51  * </pre>
52  *
53  * It will save options in <code>builder.getCommandLineFile()</code>. Options
54  * will be stored one in a line. To retrieve options from file helper method can
55  * be used (see documentation):
56  *
57  * <pre>
58  * String[] args = CommandLineBuilder.preprocessCommandLineArguments(args);
59  * </pre>
60  *
61  * </p>
62  *
63  * <p>
64  * NOTICE: No protection against line separators in arguments, should be OK for
65  * Cobertura needs.
66  * </p>
67  * <p>
68  * NOTICE: This class depends on local machine settings (line separator, default
69  * encoding). If arguments are saved on different machine than they are loaded,
70  * results are unspecified. No problem in Cobertura.
71  * </p>
72  *
73  * @author Grzegorz Lukasik
74  */

75 public class CommandLineBuilder {
76     private static final Logger logger = Logger
77             .getLogger(CommandLineBuilder.class);
78
79     private static final String JavaDoc LINESEP = System.getProperty("line.separator");
80
81     // File that will be used to store arguments
82
private File JavaDoc commandLineFile = null;
83
84     // Writer that will be used to write arguments to the file
85
private FileWriter JavaDoc commandLineWriter = null;
86
87     /**
88      * Creates a new instance of the builder. Instances of this class should not
89      * be reused to create many command lines.
90      *
91      * @throws IOException
92      * if problems with creating temporary file for storing command
93      * line occur
94      */

95     public CommandLineBuilder() throws IOException JavaDoc {
96         commandLineFile = File.createTempFile("cobertura.", ".cmdline");
97         commandLineFile.deleteOnExit();
98         commandLineWriter = new FileWriter JavaDoc(commandLineFile);
99     }
100
101     /**
102      * Adds command line argument. Each argument can be thought as a single cell
103      * in array passed to main method. This method should not be used after
104      * arguments were saved.
105      *
106      * @param arg command line argument to save
107      * @throws IOException
108      * if problems with temporary file occur
109      * @throws NullPointerException
110      * if <code>arg</code> is <code>null</code>
111      */

112     public void addArg(String JavaDoc arg) throws IOException JavaDoc {
113         if( arg==null)
114             throw new NullPointerException JavaDoc();
115         commandLineWriter.write(arg + LINESEP);
116     }
117
118     
119     /**
120      * Adds two command line arguments. Convienience function, calls
121      * {@link #addArg(String)} two times.
122      *
123      * @param arg1 first command line argument to save
124      * @param arg2 second command line argument to save
125      * @throws IOException
126      * if problems with temporary file occur
127      * @throws NullPointerException
128      * if any <code>arg</code> is <code>null</code>
129      */

130     public void addArg(String JavaDoc arg1, String JavaDoc arg2) throws IOException JavaDoc {
131         addArg(arg1);
132         addArg(arg2);
133     }
134
135     
136     /**
137      * Saves options and made file available to use. Use method
138      * {@link #getCommandLineFile} to get the file the arguments are saved in.
139      *
140      * @throws IOException
141      * if problems with temporary file occur
142      */

143     public void saveArgs() throws IOException JavaDoc {
144         commandLineWriter.flush();
145         commandLineWriter.close();
146     }
147
148     /**
149      * Gets absolute path to the file with saved arguments. Notice, that however
150      * this method can be used as soon as an instance of this class is created,
151      * arguments should be read from the file after a call to
152      * {@link #saveArgs} method.
153      *
154      * @return absolute path to the file with arguments
155      */

156     public String JavaDoc getCommandLineFile() {
157         return commandLineFile.getAbsolutePath();
158     }
159
160     /**
161      * Explicity frees all resources associated with this instance. Result of
162      * any other method call after disposing an instance of this class is
163      * unspecified.
164      */

165     public void dispose() {
166         commandLineFile.delete();
167     }
168
169     /**
170      * Loads arguments from file if <code>--commandsfile</code> option is used. Checks
171      * if passed array contains <code>--commandsfile</code> String, and if
172      * so arguments from file specified in the very next array cell are read. If
173      * there are more then one <code>--commandsfile</code> the result is unspecified.
174      *
175      * @return The list of arguments read from commandsfile, or
176      * <code>args</code> if commandsfile option was not specified
177      * or the file cannot be read.
178      * @throws NullPointerException if args is null, or any argument is null
179      * @throws IllegalArgumentException if --commandsfile is specified as last option
180      * @throws IOException if I/O related error with temporary command line file occur
181      */

182     public static String JavaDoc[] preprocessCommandLineArguments(String JavaDoc[] args) throws IOException JavaDoc {
183         boolean hasCommandsFile = false;
184         String JavaDoc commandsFileName = null;
185         for (int i = 0; i < args.length; i++) {
186             if ( args[i].equals( "--commandsfile")) {
187                 if( i==args.length-1) {
188                     throw new IllegalArgumentException JavaDoc("'--commandsfile' specified as last option.");
189                 }
190                 hasCommandsFile = true;
191                 commandsFileName = args[++i];
192             }
193         }
194
195         if (hasCommandsFile) {
196             List JavaDoc arglist = new ArrayList JavaDoc();
197             BufferedReader JavaDoc bufferedReader = null;
198
199             try {
200                 bufferedReader = new BufferedReader JavaDoc(new FileReader JavaDoc(
201                         commandsFileName));
202                 String JavaDoc line;
203
204                 while ((line = bufferedReader.readLine()) != null)
205                     arglist.add(line);
206
207             } catch (IOException JavaDoc e) {
208                 logger.info( "I/O error when reading temporary commands file", e);
209                 throw new IOException JavaDoc( "Unable to read temporary commands file "
210                         + commandsFileName + ".");
211             } finally {
212                 if (bufferedReader != null) {
213                     try {
214                         bufferedReader.close();
215                     } catch (IOException JavaDoc e) {
216                     }
217                 }
218             }
219
220             args = (String JavaDoc[]) arglist.toArray(new String JavaDoc[arglist.size()]);
221         }
222         return args;
223     }
224 }
225
Popular Tags