1 package org.codehaus.mojo.macker;
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.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25
26 /**
27 * Helper class for storing long command lines inside a temporary file.
28 * <p>
29 * Typical usage:
30 *
31 * <pre>
32 * builder = new CommandLineBuilder();
33 * builder.addArg("--someoption");
34 * builder.addArg("optionValue");
35 * ...
36 * builder.saveArgs();
37 * doSomething(builder.getCommandLineFile());
38 * builder.dispose();
39 * </pre>
40 *
41 * It will save options in <code>builder.getCommandLineFile()</code>. Options
42 * will be stored one in a line. Options are saved in UTF-8 encoding.
43 *
44 * @author Grzegorz Lukasik (Cobertura)
45 */
46 public class CommandLineBuilder
47 {
48
49 private static final String LINESEP = System.getProperty( "line.separator" );
50
51 // File that will be used to store arguments
52 private final File commandLineFile;
53
54 // Writer that will be used to write arguments to the file
55 private final FileWriter commandLineWriter;
56
57 /**
58 * Creates a new instance of the builder. Instances of this class should not
59 * be reused to create many command lines.
60 * @throws IOException if problems with creating temporary file for storing command line occur
61 */
62 public CommandLineBuilder( String name )
63 throws IOException
64 {
65 commandLineFile = File.createTempFile( name + ".", ".cmdline" );
66 commandLineFile.deleteOnExit();
67 commandLineWriter = new FileWriter( commandLineFile );
68 }
69
70 /**
71 * Adds command line argument. Each argument can be thought as a single cell
72 * in array passed to main method. This method should not be used after
73 * arguments were saved.
74 *
75 * @param arg command line argument to save
76 * @throws IOException if problems with temporary file occur
77 */
78 public void addArg( String arg )
79 throws IOException
80 {
81 if ( arg == null )
82 {
83 throw new IllegalArgumentException( "arg is null" );
84 }
85 commandLineWriter.write( arg + LINESEP );
86 }
87
88 /**
89 * Adds two command line arguments. Convienience function, calls {@link #addArg(String)} two times.
90 *
91 * @param arg1 first command line argument to save
92 * @param arg2 second command line argument to save
93 * @throws IOException if problems with temporary file occur
94 */
95 public void addArg( String arg1, String arg2 )
96 throws IOException
97 {
98 addArg( arg1 );
99 addArg( arg2 );
100 }
101
102 /**
103 * Saves options and made file available to use. Use method
104 * {@link #getCommandLineFile} to get the file the arguments are saved in.
105 * @throws IOException if problems with temporary file occur
106 */
107 public void saveArgs()
108 throws IOException
109 {
110 commandLineWriter.flush();
111 commandLineWriter.close();
112 }
113
114 /**
115 * Gets absolute path to the file with saved arguments. Notice, that however
116 * this method can be used as soon as an instance of this class is created,
117 * arguments should be read from the file after a call to {@link #saveArgs} method.
118 * @return absolute path to the file with arguments
119 */
120 public String getCommandLineFile()
121 throws IOException
122 {
123 return commandLineFile.getCanonicalFile().getAbsolutePath();
124 }
125
126 /**
127 * Explicity frees all resources associated with this instance. Result of
128 * any other method call after disposing an instance of this class is unspecified.
129 */
130 public void dispose()
131 {
132 commandLineFile.delete();
133 }
134
135 }