001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright © 2002 Jesse Stockall. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Apache Software Foundation (http://www.apache.org/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
026: * Foundation" must not be used to endorse or promote products derived
027: * from this software without prior written permission. For written
028: * permission, please contact apache@apache.org.
029: *
030: * 5. Products derived from this software may not be called "Apache"
031: * nor may "Apache" appear in their names without prior written
032: * permission of the Apache Group.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
038: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: * SUCH DAMAGE.
046: * ====================================================================
047: *
048: * This software consists of voluntary contributions made by many
049: * individuals on behalf of the Apache Software Foundation. For more
050: * information on the Apache Software Foundation, please see
051: * <http://www.apache.org/>.
052: */
053: package org.apache.tools.ant.taskdefs.optional.genjar;
054:
055: import java.io.ByteArrayOutputStream;
056: import java.io.File;
057: import java.io.FileInputStream;
058: import java.io.IOException;
059: import java.io.InputStream;
060: import java.io.OutputStream;
061: import java.util.ArrayList;
062: import java.util.HashMap;
063: import java.util.Iterator;
064: import java.util.List;
065: import java.util.Properties;
066: import java.util.jar.Attributes;
067: import java.util.jar.Manifest;
068:
069: import org.apache.tools.ant.BuildException;
070:
071: /**
072: * Encapsulates the data destined for the jar's Manifest file.<p>
073: *
074: * An Mft instance represents the <manifest> element in the project file.
075: * An Mft is always created by GenJar (whether or not there's a manifest
076: * element) to handle the Manifest creation duties.
077: *
078: * @author Original Code: <a href="mailto:jake@riggshill.com">John W. Kohler
079: * </a>
080: * @author Jesse Stockall
081: * @version $Revision: 1.2 $ $Date: 2002/09/28 03:41:07 $
082: */
083: public class Mft {
084: File baseDir = null;
085: File file = null;
086: List attrs = new ArrayList(32);
087: List mimes = new ArrayList(32);
088: java.util.Map mimeMap = null;
089: boolean genEntryAtts = true;
090: Manifest man = null;
091:
092: /** */
093: public Mft() {
094: man = new Manifest();
095: //
096: // the version attribute MUST be present
097: //
098: Attributes atts = man.getMainAttributes();
099: atts.put(Attributes.Name.MANIFEST_VERSION, "1.0");
100: atts.putValue("Created-By", "Jakarta Ant " + getAntVersion());
101: }
102:
103: //
104: // I lifted this directly from the ant Main class - there should be a way
105: // for a task to determine what version of Ant it's operating within
106: //
107: /**
108: * Gets the antVersion attribute of the Mft object
109: *
110: * @return The antVersion value
111: */
112: String getAntVersion() {
113: String ret = "Version Unavailable";
114:
115: try {
116: Properties props = new Properties();
117: InputStream in = org.apache.tools.ant.Main.class
118: .getResourceAsStream("/org/apache/tools/ant/version.txt");
119: props.load(in);
120: in.close();
121: StringBuffer msg = new StringBuffer();
122: msg.append(props.getProperty("VERSION"));
123: msg.append(" (");
124: msg.append(props.getProperty("DATE"));
125: msg.append(")");
126: ret = msg.toString();
127: } catch (Exception e) {
128: ;
129: } finally {
130: return ret;
131: }
132: }
133:
134: /**
135: * this simply adds all defined attributes into the manifest
136: *
137: * @param logger Description of the Parameter
138: * @throws BuildException Description of the Exception
139: */
140: void execute(Logger logger) throws BuildException {
141: //
142: // if they gave us a file, then load the file
143: // first
144: //
145: if (file != null) {
146: if (!file.exists()) {
147: logger.error("specified manifest file not found:"
148: + file);
149: throw new BuildException("manifest file " + file
150: + " not found");
151: }
152: try {
153: man.read(new FileInputStream(file));
154: } catch (IOException ioe) {
155: logger
156: .error("I/O Error loading manifest file: "
157: + file);
158: throw new BuildException("can't load manifest file "
159: + file + ":" + ioe);
160: }
161: }
162: //
163: // rip over our attribute values and
164: // insert them into the manifest
165: //
166: for (Iterator it = attrs.iterator(); it.hasNext();) {
167: MftAttr attr = (MftAttr) it.next();
168: logger.verbose("Attribute:" + attr);
169: attr.add(man);
170: }
171:
172: //
173: // we're done with our attributes so....
174: //
175: attrs = null;
176: //
177: // transfer all the mime type definitions to
178: // a map for quick lookup
179: //
180: mimeMap = new HashMap();
181: for (Iterator it = mimes.iterator(); it.hasNext();) {
182: Mime m = (Mime) it.next();
183: mimeMap.put(m.getExt(), m.getType());
184: }
185: mimes = null;
186: }
187:
188: /**
189: * Sets the baseDir attribute of the Mft object
190: *
191: * @param baseDir The new baseDir value
192: */
193: void setBaseDir(File baseDir) {
194: this .baseDir = baseDir;
195: }
196:
197: /**
198: * @return The manifest value
199: */
200: public Manifest getManifest() {
201: return man;
202: }
203:
204: /**
205: * @param f The new template value
206: */
207: public void setTemplate(String f) {
208: file = new File(f);
209: if (!file.isAbsolute()) {
210: file = new File(baseDir, f);
211: }
212: //file = new File( baseDir, f );
213: }
214:
215: /**
216: * Sets the generateEntryAttributes attribute of the Mft object
217: *
218: * @param val The new generateEntryAttributes value
219: */
220: public void setGenerateEntryAttributes(String val) {
221: genEntryAtts = "yes".equalsIgnoreCase(val);
222: }
223:
224: /**
225: * @return Description of the Return Value
226: */
227: public Object createMime() {
228: Mime m = new Mime();
229: mimes.add(m);
230: return m;
231: }
232:
233: /**
234: * @return Description of the Return Value
235: */
236: public Object createAttribute() {
237: MftAttr attr = new MftAttr();
238: attrs.add(attr);
239: return attr;
240: }
241:
242: /**
243: * @param name The feature to be added to the Entry attribute
244: * @param newAtts The feature to be added to the Entry attribute
245: */
246: public void addEntry(String name, Attributes newAtts) {
247: if (!genEntryAtts) {
248: return;
249: }
250:
251: Attributes atts = man.getAttributes(name);
252: if (atts != null) {
253: atts.putAll(newAtts);
254: } else {
255: atts = newAtts;
256: }
257:
258: int idx = name.lastIndexOf('.');
259: if (idx > 0) {
260: String ext = name.substring(idx + 1);
261: String type = (String) mimeMap.get(ext);
262: if (type != null) {
263: atts.putValue("Content-Type", type);
264: }
265: }
266: //
267: // replace the existing with itself or
268: // the new ones
269: //
270: man.getEntries().put(name, atts);
271:
272: }
273:
274: /**
275: * @return Description of the Return Value
276: */
277: public String toString() {
278: String retVal = "";
279: OutputStream out = new ByteArrayOutputStream();
280:
281: try {
282: man.write(out);
283: retVal = out.toString();
284: } catch (IOException ioe) {
285: retVal = "IO Exception writing manifest";
286: } finally {
287: return retVal;
288: }
289: }
290:
291: /**
292: * Instantiated when Ant encounters an <attribute> element.<p>
293: *
294: *
295: *
296: * @author Original Code: <a href="mailto:jake@riggshill.com">John W.
297: * Kohler</a>
298: * @version @version@
299: */
300: public class MftAttr {
301: private String key = null;
302: private String name = null;
303: private String value = null;
304:
305: /**
306: * @param man Description of the Parameter
307: */
308: public void add(Manifest man) {
309: Attributes atts;
310:
311: if (key == null) {
312: atts = man.getMainAttributes();
313: } else {
314: atts = man.getAttributes(key);
315: if (atts == null) {
316: atts = new Attributes();
317: man.getEntries().put(key, atts);
318: }
319: }
320: atts.putValue(name, value);
321: }
322:
323: /**
324: * @param e The new entry value
325: */
326: public void setEntry(String e) {
327: if (!"main".equals(e)) {
328: this .key = e;
329: }
330: }
331:
332: /**
333: * @param n The new name value
334: */
335: public void setName(String n) {
336: this .name = n;
337: }
338:
339: /**
340: * @param v The new value value
341: */
342: public void setValue(String v) {
343: value = v;
344: }
345:
346: /**
347: * @return The name value
348: */
349: public String getName() {
350: return name;
351: }
352:
353: /**
354: * @return The value value
355: */
356: public String getValue() {
357: return value;
358: }
359:
360: /**
361: * @return Description of the Return Value
362: */
363: public String toString() {
364: return "(" + (key == null ? "Main" : key) + ") " + name
365: + ": " + value;
366: }
367: }
368:
369: /**
370: * Instantiated when Ant encounters an <mime> element.<p>
371: *
372: * This class is used to get the MIME mapping info from the project file
373: * into the Mft's type map.
374: *
375: * @author Original Code: <a href="mailto:jake@riggshill.com">John W.
376: * Kohler</a>
377: * @version @version@
378: */
379: public class Mime {
380: private String type = null;
381: private String ext = null;
382:
383: /**
384: * @param t The new type value
385: */
386: public void setType(String t) {
387: type = t;
388: }
389:
390: /**
391: * @param e The new ext value
392: */
393: public void setExt(String e) {
394: ext = e;
395: }
396:
397: /**
398: * @return The type value
399: */
400: public String getType() {
401: return type;
402: }
403:
404: /**
405: * @return The ext value
406: */
407: public String getExt() {
408: return ext;
409: }
410: }
411: }
|