KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > ant > ManifestClassPath


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

15 package org.apache.hivemind.ant;
16
17 import java.io.File JavaDoc;
18
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.Task;
21 import org.apache.tools.ant.types.Path;
22
23 /**
24  * Utility used to create a manifest class path.
25  * It takes, as input, a reference to a path. It converts this
26  * into a space-separated list of file names. The default
27  * behavior is to simply strip off the directory portion of
28  * each file entirely.
29  *
30  * <p>
31  * The final result is assigned to the property.
32  *
33  * @author Howard Lewis Ship
34  */

35 public class ManifestClassPath extends Task
36 {
37     private String JavaDoc _property;
38     private Path _classpath;
39     private File JavaDoc _directory;
40
41     public Path createClasspath()
42     {
43         _classpath = new Path(getProject());
44
45         return _classpath;
46     }
47
48     public String JavaDoc getProperty()
49     {
50         return _property;
51     }
52
53     public void setProperty(String JavaDoc string)
54     {
55         _property = string;
56     }
57
58     public void execute()
59     {
60         if (_classpath == null)
61             throw new BuildException("You must specify a classpath to generate the manifest entry from");
62
63         if (_property == null)
64             throw new BuildException("You must specify a property to assign the manifest classpath to");
65
66         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
67
68         String JavaDoc[] paths = _classpath.list();
69
70         String JavaDoc stripPrefix = null;
71
72         if (_directory != null)
73             stripPrefix = _directory.getPath();
74
75         // Will paths ever be null?
76

77         boolean needSep = false;
78
79         for (int i = 0; i < paths.length; i++)
80         {
81             String JavaDoc path = paths[i];
82
83             if (stripPrefix != null)
84             {
85                 if (!path.startsWith(stripPrefix))
86                     continue;
87
88                 // Sometimes, people put the prefix directory in as a
89
// classpath entry; we ignore it (otherwise
90
// we get a IndexOutOfBoundsException
91

92                 if (path.length() == stripPrefix.length())
93                     continue;
94
95                 if (needSep)
96                     buffer.append(' ');
97
98                 // Strip off the directory and the seperator, leaving
99
// just the relative path.
100

101                 buffer.append(filter(path.substring(stripPrefix.length() + 1)));
102
103                 needSep = true;
104
105             }
106             else
107             {
108                 if (needSep)
109                     buffer.append(' ');
110
111                 File JavaDoc f = new File JavaDoc(path);
112
113                 buffer.append(f.getName());
114
115                 needSep = true;
116             }
117         }
118
119         getProject().setProperty(_property, buffer.toString());
120     }
121
122     public File JavaDoc getDirectory()
123     {
124         return _directory;
125     }
126
127     /**
128      * Sets a containing directory. This has two effects:
129      * <ul>
130      * <li>Only files in the classpath that are contained by the directory are included.
131      * <li>The directory path is stripped from each path, leaving a relative path
132      * to the file.
133      * </ul>
134      */

135     public void setDirectory(File JavaDoc file)
136     {
137         _directory = file;
138     }
139
140     /**
141      * Classpath entries must use a forward slash, regardless of what the
142      * local filesystem uses.
143      */

144     protected String JavaDoc filter(String JavaDoc value)
145     {
146         if (File.separatorChar == '/')
147             return value;
148
149         return value.replace(File.separatorChar, '/');
150     }
151 }
152
Popular Tags