|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ManifestClassPath.java | 95% | 97.2% | 100% | 96.8% |
|
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;
|
|
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 _property;
|
|
38 |
private Path _classpath;
|
|
39 |
private File _directory;
|
|
40 |
|
|
41 | 5 |
public Path createClasspath()
|
42 |
{ |
|
43 | 5 |
_classpath = new Path(getProject());
|
44 |
|
|
45 | 5 |
return _classpath;
|
46 |
} |
|
47 |
|
|
48 | 3 |
public String getProperty()
|
49 |
{ |
|
50 | 3 |
return _property;
|
51 |
} |
|
52 |
|
|
53 | 5 |
public void setProperty(String string) |
54 |
{ |
|
55 | 5 |
_property = string; |
56 |
} |
|
57 |
|
|
58 | 6 |
public void execute() |
59 |
{ |
|
60 | 6 |
if (_classpath == null) |
61 | 1 |
throw new BuildException("You must specify a classpath to generate the manifest entry from"); |
62 |
|
|
63 | 5 |
if (_property == null) |
64 | 1 |
throw new BuildException("You must specify a property to assign the manifest classpath to"); |
65 |
|
|
66 | 4 |
StringBuffer buffer = new StringBuffer();
|
67 |
|
|
68 | 4 |
String[] paths = _classpath.list(); |
69 |
|
|
70 | 4 |
String stripPrefix = null;
|
71 |
|
|
72 | 4 |
if (_directory != null) |
73 | 2 |
stripPrefix = _directory.getPath(); |
74 |
|
|
75 |
// Will paths ever be null?
|
|
76 |
|
|
77 | 4 |
boolean needSep = false; |
78 |
|
|
79 | 4 |
for (int i = 0; i < paths.length; i++) |
80 |
{ |
|
81 | 7 |
String path = paths[i]; |
82 |
|
|
83 | 7 |
if (stripPrefix != null) |
84 |
{ |
|
85 | 5 |
if (!path.startsWith(stripPrefix))
|
86 | 1 |
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 | 4 |
if (path.length() == stripPrefix.length())
|
93 | 1 |
continue;
|
94 |
|
|
95 | 3 |
if (needSep)
|
96 | 1 |
buffer.append(' '); |
97 |
|
|
98 |
// Strip off the directory and the seperator, leaving
|
|
99 |
// just the relative path.
|
|
100 |
|
|
101 | 3 |
buffer.append(filter(path.substring(stripPrefix.length() + 1))); |
102 |
|
|
103 | 3 |
needSep = true;
|
104 |
|
|
105 |
} |
|
106 |
else
|
|
107 |
{ |
|
108 | 2 |
if (needSep)
|
109 | 1 |
buffer.append(' '); |
110 |
|
|
111 | 2 |
File f = new File(path);
|
112 |
|
|
113 | 2 |
buffer.append(f.getName()); |
114 |
|
|
115 | 2 |
needSep = true;
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 | 4 |
getProject().setProperty(_property, buffer.toString()); |
120 |
} |
|
121 |
|
|
122 | 1 |
public File getDirectory()
|
123 |
{ |
|
124 | 1 |
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 | 2 |
public void setDirectory(File file) |
136 |
{ |
|
137 | 2 |
_directory = file; |
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Classpath entries must use a forward slash, regardless of what the
|
|
142 |
* local filesystem uses.
|
|
143 |
*/
|
|
144 | 3 |
protected String filter(String value)
|
145 |
{ |
|
146 | 3 |
if (File.separatorChar == '/')
|
147 | 0 |
return value;
|
148 |
|
|
149 | 3 |
return value.replace(File.separatorChar, '/');
|
150 |
} |
|
151 |
} |
|
152 |
|
|