KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jlink > JlinkTask


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.jlink;
19
20 import java.io.File JavaDoc;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.taskdefs.MatchingTask;
24 import org.apache.tools.ant.types.Path;
25
26 /**
27  * This class defines objects that can link together various jar and
28  * zip files.
29  *
30  * <p>It is basically a wrapper for the jlink code written originally
31  * by <a HREF="mailto:beard@netscape.com">Patrick Beard</a>. The
32  * classes org.apache.tools.ant.taskdefs.optional.jlink.Jlink and
33  * org.apache.tools.ant.taskdefs.optional.jlink.ClassNameReader
34  * support this class.</p>
35  *
36  * <p>For example:
37  * <code>
38  * <pre>
39  * &lt;jlink compress=&quot;false&quot; outfile=&quot;out.jar&quot;/&gt;
40  * &lt;mergefiles&gt;
41  * &lt;pathelement path=&quot;${build.dir}/mergefoo.jar&quot;/&gt;
42  * &lt;pathelement path=&quot;${build.dir}/mergebar.jar&quot;/&gt;
43  * &lt;/mergefiles&gt;
44  * &lt;addfiles&gt;
45  * &lt;pathelement path=&quot;${build.dir}/mac.jar&quot;/&gt;
46  * &lt;pathelement path=&quot;${build.dir}/pc.zip&quot;/&gt;
47  * &lt;/addfiles&gt;
48  * &lt;/jlink&gt;
49  * </pre>
50  * </code>
51  *
52  * @ant.task ignore="true"
53  */

54 public class JlinkTask extends MatchingTask {
55
56     /**
57      * The output file for this run of jlink. Usually a jar or zip file.
58      * @param outfile the output file
59      */

60     public void setOutfile(File JavaDoc outfile) {
61         this.outfile = outfile;
62     }
63
64     /**
65      * Establishes the object that contains the files to
66      * be merged into the output.
67      * @return a path to be configured
68      */

69     public Path createMergefiles() {
70         if (this.mergefiles == null) {
71             this.mergefiles = new Path(getProject());
72         }
73         return this.mergefiles.createPath();
74     }
75
76     /**
77      * Sets the files to be merged into the output.
78      * @param mergefiles a path
79      */

80     public void setMergefiles(Path mergefiles) {
81         if (this.mergefiles == null) {
82             this.mergefiles = mergefiles;
83         } else {
84             this.mergefiles.append(mergefiles);
85         }
86     }
87
88     /**
89      * Establishes the object that contains the files to
90      * be added to the output.
91      * @return a path to be configured
92      */

93     public Path createAddfiles() {
94         if (this.addfiles == null) {
95             this.addfiles = new Path(getProject());
96         }
97         return this.addfiles.createPath();
98     }
99
100     /**
101      * Sets the files to be added into the output.
102      * @param addfiles a path
103      */

104     public void setAddfiles(Path addfiles) {
105         if (this.addfiles == null) {
106             this.addfiles = addfiles;
107         } else {
108             this.addfiles.append(addfiles);
109         }
110     }
111
112     /**
113      * Defines whether or not the output should be compacted.
114      * @param compress a <code>boolean</code> value
115      */

116     public void setCompress(boolean compress) {
117         this.compress = compress;
118     }
119
120     /**
121      * Does the adding and merging.
122      * @throws BuildException on error
123      */

124     public void execute() throws BuildException {
125         //Be sure everything has been set.
126
if (outfile == null) {
127             throw new BuildException("outfile attribute is required! "
128                 + "Please set.");
129         }
130         if (!haveAddFiles() && !haveMergeFiles()) {
131             throw new BuildException("addfiles or mergefiles required! "
132                 + "Please set.");
133         }
134         log("linking: " + outfile.getPath());
135         log("compression: " + compress, Project.MSG_VERBOSE);
136         jlink linker = new jlink();
137         linker.setOutfile(outfile.getPath());
138         linker.setCompression(compress);
139         if (haveMergeFiles()) {
140             log("merge files: " + mergefiles.toString(), Project.MSG_VERBOSE);
141             linker.addMergeFiles(mergefiles.list());
142         }
143         if (haveAddFiles()) {
144             log("add files: " + addfiles.toString(), Project.MSG_VERBOSE);
145             linker.addAddFiles(addfiles.list());
146         }
147         try {
148             linker.link();
149         } catch (Exception JavaDoc ex) {
150             throw new BuildException(ex, getLocation());
151         }
152     }
153
154     private boolean haveAddFiles() {
155         return haveEntries(addfiles);
156     }
157
158     private boolean haveMergeFiles() {
159         return haveEntries(mergefiles);
160     }
161
162     private boolean haveEntries(Path p) {
163         if (p == null) {
164             return false;
165         }
166         if (p.size() > 0) {
167             return true;
168         }
169         return false;
170     }
171
172     private File JavaDoc outfile = null;
173
174     private Path mergefiles = null;
175
176     private Path addfiles = null;
177
178     private boolean compress = false;
179
180 }
181
182
183
Popular Tags