KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > ManifestTask


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
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.util.FileUtils;
34 import org.apache.tools.ant.types.EnumeratedAttribute;
35
36 /**
37  * Creates a manifest file for inclusion in a JAR, Ant task wrapper
38  * around {@link Manifest Manifest}. This task can be used to write a
39  * Manifest file, optionally replacing or updating an existing file.
40  *
41  * @since Ant 1.5
42  *
43  * @ant.task category="java"
44  */

45 public class ManifestTask extends Task {
46
47     /**
48      * Holds the real data.
49      */

50     private Manifest nestedManifest = new Manifest();
51
52     /**
53      * The file to which the manifest should be written when used as a task
54      */

55     private File JavaDoc manifestFile;
56
57     /**
58      * The mode with which the manifest file is written
59      */

60     private Mode mode;
61
62     /**
63      * The encoding of the manifest file
64      */

65     private String JavaDoc encoding;
66
67     /**
68      * Helper class for Manifest's mode attribute.
69      */

70     public static class Mode extends EnumeratedAttribute {
71         /**
72          * Get Allowed values for the mode attribute.
73          *
74          * @return a String array of the allowed values.
75          */

76         public String JavaDoc[] getValues() {
77             return new String JavaDoc[] {"update", "replace"};
78         }
79     }
80
81     /**
82      * Default constructor
83      */

84     public ManifestTask() {
85         mode = new Mode();
86         mode.setValue("replace");
87     }
88
89     /**
90      * Add a section to the manifest
91      *
92      * @param section the manifest section to be added
93      *
94      * @exception ManifestException if the section is not valid.
95      */

96     public void addConfiguredSection(Manifest.Section section)
97          throws ManifestException {
98         nestedManifest.addConfiguredSection(section);
99     }
100
101     /**
102      * Add an attribute to the manifest - it is added to the main section.
103      *
104      * @param attribute the attribute to be added.
105      *
106      * @exception ManifestException if the attribute is not valid.
107      */

108     public void addConfiguredAttribute(Manifest.Attribute attribute)
109          throws ManifestException {
110         nestedManifest.addConfiguredAttribute(attribute);
111     }
112
113     /**
114      * The name of the manifest file to create/update.
115      * Required if used as a task.
116      * @param f the Manifest file to be written
117      */

118     public void setFile(File JavaDoc f) {
119         manifestFile = f;
120     }
121
122     /**
123      * The encoding to use for reading in an existing manifest file
124      * @param encoding the manifest file encoding.
125      */

126     public void setEncoding(String JavaDoc encoding) {
127         this.encoding = encoding;
128     }
129
130     /**
131      * Update policy: either "update" or "replace"; default is "replace".
132      * @param m the mode value - update or replace.
133      */

134     public void setMode(Mode m) {
135         mode = m;
136     }
137
138     /**
139      * Create or update the Manifest when used as a task.
140      *
141      * @throws BuildException if the manifest cannot be written.
142      */

143     public void execute() throws BuildException {
144         if (manifestFile == null) {
145             throw new BuildException("the file attribute is required");
146         }
147
148         Manifest toWrite = Manifest.getDefaultManifest();
149         Manifest current = null;
150         BuildException error = null;
151
152         if (manifestFile.exists()) {
153             FileInputStream JavaDoc fis = null;
154             InputStreamReader JavaDoc isr = null;
155             try {
156                 fis = new FileInputStream JavaDoc(manifestFile);
157                 if (encoding == null) {
158                     isr = new InputStreamReader JavaDoc(fis, "UTF-8");
159                 } else {
160                     isr = new InputStreamReader JavaDoc(fis, encoding);
161                 }
162                 current = new Manifest(isr);
163             } catch (ManifestException m) {
164                 error = new BuildException("Existing manifest " + manifestFile
165                                            + " is invalid", m, getLocation());
166             } catch (IOException JavaDoc e) {
167                 error = new BuildException("Failed to read " + manifestFile,
168                                            e, getLocation());
169             } finally {
170                 FileUtils.close(isr);
171             }
172         }
173
174         //look for and print warnings
175
for (Enumeration JavaDoc e = nestedManifest.getWarnings();
176                 e.hasMoreElements();) {
177             log("Manifest warning: " + (String JavaDoc) e.nextElement(),
178                     Project.MSG_WARN);
179         }
180         try {
181             if (mode.getValue().equals("update") && manifestFile.exists()) {
182                 if (current != null) {
183                     toWrite.merge(current);
184                 } else if (error != null) {
185                     throw error;
186                 }
187             }
188
189             toWrite.merge(nestedManifest);
190         } catch (ManifestException m) {
191             throw new BuildException("Manifest is invalid", m, getLocation());
192         }
193
194         if (toWrite.equals(current)) {
195             log("Manifest has not changed, do not recreate",
196                 Project.MSG_VERBOSE);
197             return;
198         }
199
200         PrintWriter JavaDoc w = null;
201         try {
202             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(manifestFile);
203             OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(fos, Manifest.JAR_ENCODING);
204             w = new PrintWriter JavaDoc(osw);
205             toWrite.write(w);
206         } catch (IOException JavaDoc e) {
207             throw new BuildException("Failed to write " + manifestFile,
208                                      e, getLocation());
209         } finally {
210             if (w != null) {
211                 w.close();
212             }
213         }
214     }
215
216 }
217
Popular Tags