001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2000, 2001, 2002, 2003 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: */
049: package org.apache.tools.ant.taskdefs.optional.genjar;
050:
051: import java.io.File;
052: import java.io.IOException;
053: import java.util.ArrayList;
054: import java.util.Iterator;
055: import java.util.List;
056:
057: import org.apache.tools.ant.BuildException;
058: import org.apache.tools.ant.DirectoryScanner;
059: import org.apache.tools.ant.Project;
060:
061: import org.apache.tools.ant.types.DataType;
062: import org.apache.tools.ant.types.FileSet;
063:
064: /**
065: * Represents a <resource> element within the project file.</p> <p>
066: *
067: * In addition to holding the final <i>jar name</i> of the resource, it performs the actual
068: * resolution of file names along with expansion of <i>filesets</i> .
069: *
070: * @author Original Code: <a href="mailto:jake@riggshill.com">John W. Kohler </a>
071: * @author Jesse Stockall
072: * @version $Revision: 1.4 $ $Date: 2003/03/06 01:22:01 $
073: */
074: public class Resource extends DataType implements JarSpec {
075: private Project project;
076: private List jarEntries = new ArrayList();
077: private List filesets = new ArrayList(8);
078: private File file = null;
079: private String pkg = null;
080:
081: /**
082: * Constructs a new Resource element.
083: *
084: * @param proj the owning project
085: */
086: Resource(Project proj) {
087: project = proj;
088: }
089:
090: /**
091: * Returns a List of all JarEntry objects collected by this Resource
092: *
093: * @return List all collected JarEntry objects
094: */
095: public List getJarEntries() {
096: return jarEntries;
097: }
098:
099: /**
100: * Sets the value of the file attribute. When the resource element ha a single file attribute,
101: * Ant calls this to 'set' the value.
102: *
103: * @param f The file to be included in the jar as a resource.
104: */
105: public void setFile(File f) {
106: if (filesets.size() > 0) {
107: throw new BuildException(
108: "can't add 'file' - fileset already used");
109: }
110: file = f;
111: }
112:
113: /**
114: * Gets the name of the resource.
115: *
116: * @return Thr name
117: */
118: public String getName() {
119: return file.getName();
120: }
121:
122: /**
123: * synonym for 'file'
124: *
125: * @param f The new name value
126: * @deprecated Use "file" instead.
127: */
128: public void setName(File f) {
129: System.err.println("name is deprecated Use 'file' instead.");
130: setFile(f);
131: }
132:
133: /**
134: * set the package (path) that'll be applied to ALL resources in this resource set - make sure
135: * to handle the empty package properly
136: *
137: * @param pkg sets the value of the <code>package</code> attribute
138: */
139: public void setPackage(String pkg) {
140: pkg = pkg.replace('.', '/');
141: if (pkg.length() > 0 && !pkg.endsWith("/")) {
142: this .pkg = pkg + "/";
143: } else {
144: this .pkg = pkg;
145: }
146: }
147:
148: /**
149: * creates a FileSet - in response to the ant parse phase
150: *
151: * @return Description of the Return Value
152: */
153: public FileSet createFileset() {
154: if (file != null) {
155: throw new BuildException(
156: "can't add Fileset - file already set");
157: }
158: FileSet set = new FileSet();
159: filesets.add(set);
160: return set;
161: }
162:
163: /**
164: * changes the path portion of the file if the <var>pkg</var> variable is not null
165: *
166: * @param s Description of the Parameter
167: * @return Description of the Return Value
168: */
169: String repackage(String s) {
170: return repackage(new File(s));
171: }
172:
173: /**
174: * If <var>pkg</var> attrubute has been specified, it is prepended to the name of the file.
175: *
176: * @param f File to be operatred upon.
177: * @return The file name.
178: */
179: private String repackage(File f) {
180: if (pkg == null) {
181: if (file == null) {
182: // Using a fileset
183: return f.getPath();
184: } else {
185: return f.getName();
186: }
187: } else {
188: return pkg + f.getName();
189: }
190: }
191:
192: /**
193: * resolves the file attribute or fileset children into a collection of JarEntrySpec objects
194: *
195: * @param gj Description of the Parameter
196: * @throws IOException Description of the Exception
197: */
198: public void resolve(GenJar gj) throws IOException {
199: if (file != null) {
200: if (file.exists()) {
201: jarEntries.add(new JarEntrySpec(repackage(file), file
202: .getAbsoluteFile()));
203: } else {
204: jarEntries.add(new JarEntrySpec(repackage(file), null));
205: }
206: }
207:
208: for (Iterator it = filesets.iterator(); it.hasNext();) {
209: FileSet fs = (FileSet) it.next();
210: File dir = fs.getDir(project);
211:
212: DirectoryScanner ds = fs.getDirectoryScanner(project);
213:
214: String[] a = ds.getIncludedFiles();
215: for (int i = 0; i < a.length; ++i) {
216: jarEntries.add(new JarEntrySpec(repackage(a[i]),
217: new File(dir, a[i])));
218: }
219: }
220: }
221:
222: /**
223: * return a string representation of this resource set
224: *
225: * @return All the toString() methods form the jar entires.
226: */
227: public String toString() {
228: StringBuffer sb = new StringBuffer();
229: for (Iterator it = jarEntries.iterator(); it.hasNext();) {
230: sb.append("\n");
231: sb.append(it.next());
232: }
233: return sb.toString();
234: }
235: }
236: // vi:set ts=4 sw=4:
|