001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.ant;
016:
017: import java.io.File;
018:
019: import org.apache.tools.ant.BuildException;
020: import org.apache.tools.ant.Task;
021: import org.apache.tools.ant.types.Path;
022:
023: /**
024: * Utility used to create a manifest class path.
025: * It takes, as input, a reference to a path. It converts this
026: * into a space-separated list of file names. The default
027: * behavior is to simply strip off the directory portion of
028: * each file entirely.
029: *
030: * <p>
031: * The final result is assigned to the property.
032: *
033: * @author Howard Lewis Ship
034: */
035: public class ManifestClassPath extends Task {
036: private String _property;
037: private Path _classpath;
038: private File _directory;
039:
040: public Path createClasspath() {
041: _classpath = new Path(getProject());
042:
043: return _classpath;
044: }
045:
046: public String getProperty() {
047: return _property;
048: }
049:
050: public void setProperty(String string) {
051: _property = string;
052: }
053:
054: public void execute() {
055: if (_classpath == null)
056: throw new BuildException(
057: "You must specify a classpath to generate the manifest entry from");
058:
059: if (_property == null)
060: throw new BuildException(
061: "You must specify a property to assign the manifest classpath to");
062:
063: StringBuffer buffer = new StringBuffer();
064:
065: String[] paths = _classpath.list();
066:
067: String stripPrefix = null;
068:
069: if (_directory != null)
070: stripPrefix = _directory.getPath();
071:
072: // Will paths ever be null?
073:
074: boolean needSep = false;
075:
076: for (int i = 0; i < paths.length; i++) {
077: String path = paths[i];
078:
079: if (stripPrefix != null) {
080: if (!path.startsWith(stripPrefix))
081: continue;
082:
083: // Sometimes, people put the prefix directory in as a
084: // classpath entry; we ignore it (otherwise
085: // we get a IndexOutOfBoundsException
086:
087: if (path.length() == stripPrefix.length())
088: continue;
089:
090: if (needSep)
091: buffer.append(' ');
092:
093: // Strip off the directory and the seperator, leaving
094: // just the relative path.
095:
096: buffer.append(filter(path.substring(stripPrefix
097: .length() + 1)));
098:
099: needSep = true;
100:
101: } else {
102: if (needSep)
103: buffer.append(' ');
104:
105: File f = new File(path);
106:
107: buffer.append(f.getName());
108:
109: needSep = true;
110: }
111: }
112:
113: getProject().setProperty(_property, buffer.toString());
114: }
115:
116: public File getDirectory() {
117: return _directory;
118: }
119:
120: /**
121: * Sets a containing directory. This has two effects:
122: * <ul>
123: * <li>Only files in the classpath that are contained by the directory are included.
124: * <li>The directory path is stripped from each path, leaving a relative path
125: * to the file.
126: * </ul>
127: */
128: public void setDirectory(File file) {
129: _directory = file;
130: }
131:
132: /**
133: * Classpath entries must use a forward slash, regardless of what the
134: * local filesystem uses.
135: */
136: protected String filter(String value) {
137: if (File.separatorChar == '/')
138: return value;
139:
140: return value.replace(File.separatorChar, '/');
141: }
142: }
|