KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > ArgumentListBuilder


1 package hudson.util;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.StringTokenizer JavaDoc;
6 import java.util.Arrays JavaDoc;
7
8 /**
9  * Used to build up arguments for a process invocation.
10  *
11  * @author Kohsuke Kawaguchi
12  */

13 public class ArgumentListBuilder {
14     private final List JavaDoc<String JavaDoc> args = new ArrayList JavaDoc<String JavaDoc>();
15
16     public ArgumentListBuilder add(String JavaDoc a) {
17         args.add(a);
18         return this;
19     }
20
21     public ArgumentListBuilder prepend(String JavaDoc... args) {
22         this.args.addAll(0, Arrays.asList(args));
23         return this;
24     }
25
26     /**
27      * Adds an argument by quoting it.
28      * This is necessary only in a rare circumstance,
29      * such as when adding argument for ssh and rsh.
30      *
31      * Normal process invocations don't need it, because each
32      * argument is treated as its own string and never merged into one.
33      */

34     public ArgumentListBuilder addQuoted(String JavaDoc a) {
35         return add('"'+a+'"');
36     }
37
38     public ArgumentListBuilder add(String JavaDoc... args) {
39         for (String JavaDoc arg : args) {
40             add(arg);
41         }
42         return this;
43     }
44
45     /**
46      * Decomposes the given token into multiple arguments by splitting via whitespace.
47      */

48     public ArgumentListBuilder addTokenized(String JavaDoc s) {
49         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(s);
50         while(tokens.hasMoreTokens())
51             add(tokens.nextToken());
52         return this;
53     }
54
55     public String JavaDoc[] toCommandArray() {
56         return args.toArray(new String JavaDoc[args.size()]);
57     }
58     
59     public ArgumentListBuilder clone() {
60         ArgumentListBuilder r = new ArgumentListBuilder();
61         r.args.addAll(this.args);
62         return r;
63     }
64
65     public List JavaDoc<String JavaDoc> toList() {
66         return args;
67     }
68 }
69
Popular Tags