587
|
1 |
package com.nokia.ant;
|
|
2 |
|
|
3 |
/*
|
|
4 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
5 |
* contributor license agreements. See the NOTICE file distributed with
|
|
6 |
* this work for additional information regarding copyright ownership.
|
|
7 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
8 |
* (the "License"); you may not use this file except in compliance with
|
|
9 |
* the License. You may obtain a copy of the License at
|
|
10 |
*
|
|
11 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12 |
*
|
|
13 |
* Unless required by applicable law or agreed to in writing, software
|
|
14 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16 |
* See the License for the specific language governing permissions and
|
|
17 |
* limitations under the License.
|
|
18 |
*
|
|
19 |
* package org.apache.tools.ant.taskdefs;
|
|
20 |
*/
|
|
21 |
|
|
22 |
/* * Portion Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
|
|
23 |
|
|
24 |
import java.io.File;
|
|
25 |
import java.io.IOException;
|
|
26 |
import java.io.InputStream;
|
628
|
27 |
import java.net.MalformedURLException;
|
587
|
28 |
import java.net.URL;
|
628
|
29 |
import java.util.Enumeration;
|
|
30 |
import java.util.HashMap;
|
|
31 |
import java.util.Locale;
|
587
|
32 |
import java.util.Map;
|
|
33 |
import java.util.NoSuchElementException;
|
|
34 |
import java.util.Properties;
|
|
35 |
|
628
|
36 |
import org.apache.tools.ant.AntTypeDefinition;
|
|
37 |
import org.apache.tools.ant.BuildException;
|
|
38 |
import org.apache.tools.ant.ComponentHelper;
|
|
39 |
import org.apache.tools.ant.Location;
|
|
40 |
import org.apache.tools.ant.MagicNames;
|
|
41 |
import org.apache.tools.ant.Project;
|
|
42 |
import org.apache.tools.ant.ProjectHelper;
|
|
43 |
import org.apache.tools.ant.taskdefs.Definer;
|
|
44 |
import org.apache.tools.ant.types.EnumeratedAttribute;
|
587
|
45 |
import org.apache.tools.ant.util.FileUtils;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Base class for Taskdef and Typedef - handles all
|
|
49 |
* the attributes for Typedef. The uri and class
|
|
50 |
* handling is handled by DefBase
|
|
51 |
* @since Ant 1.4
|
|
52 |
*/
|
|
53 |
public class TypedefURIFix extends Definer {
|
|
54 |
|
|
55 |
/**
|
|
56 |
* the extension of an antlib file for autoloading.
|
|
57 |
* {@value[
|
|
58 |
*/
|
|
59 |
private static final String ANTLIB_XML = "/antlib.xml";
|
|
60 |
|
|
61 |
private static class ResourceStack extends ThreadLocal<Map<URL, Location>> {
|
|
62 |
public Map<URL, Location> initialValue() {
|
|
63 |
return new HashMap<URL, Location>();
|
|
64 |
}
|
|
65 |
Map<URL, Location> getStack() {
|
|
66 |
return get();
|
|
67 |
}
|
|
68 |
}
|
|
69 |
private static ResourceStack resourceStack = new ResourceStack();
|
|
70 |
private String name;
|
|
71 |
private String classname;
|
|
72 |
private File file;
|
|
73 |
private String resource;
|
|
74 |
|
|
75 |
private int format = Format.PROPERTIES;
|
|
76 |
private boolean definerSet;
|
|
77 |
private int onError = OnError.FAIL;
|
|
78 |
private String adapter;
|
|
79 |
private String adaptTo;
|
|
80 |
|
|
81 |
private Class<?> adapterClass;
|
|
82 |
private Class<?> adaptToClass;
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Enumerated type for onError attribute
|
|
86 |
*
|
|
87 |
* @see EnumeratedAttribute
|
|
88 |
*/
|
|
89 |
public static class OnError extends EnumeratedAttribute {
|
|
90 |
/** Enumerated values */
|
|
91 |
public static final int FAIL = 0, REPORT = 1, IGNORE = 2, FAIL_ALL = 3;
|
|
92 |
|
|
93 |
/**
|
|
94 |
* text value of onerror option {@value}
|
|
95 |
*/
|
|
96 |
public static final String POLICY_FAIL = "fail";
|
|
97 |
/**
|
|
98 |
* text value of onerror option {@value}
|
|
99 |
*/
|
|
100 |
public static final String POLICY_REPORT = "report";
|
|
101 |
/**
|
|
102 |
* text value of onerror option {@value}
|
|
103 |
*/
|
|
104 |
public static final String POLICY_IGNORE = "ignore";
|
|
105 |
/**
|
|
106 |
* text value of onerror option {@value}
|
|
107 |
*/
|
|
108 |
public static final String POLICY_FAILALL = "failall";
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Constructor
|
|
112 |
*/
|
|
113 |
public OnError() {
|
|
114 |
super();
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Constructor using a string.
|
|
119 |
* @param value the value of the attribute
|
|
120 |
*/
|
|
121 |
public OnError(String value) {
|
|
122 |
setValue(value);
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* get the values
|
|
127 |
* @return an array of the allowed values for this attribute.
|
|
128 |
*/
|
|
129 |
public String[] getValues() {
|
|
130 |
return new String[] { POLICY_FAIL, POLICY_REPORT, POLICY_IGNORE, POLICY_FAILALL };
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Enumerated type for format attribute
|
|
136 |
*
|
|
137 |
* @see EnumeratedAttribute
|
|
138 |
*/
|
|
139 |
public static class Format extends EnumeratedAttribute {
|
|
140 |
/** Enumerated values */
|
|
141 |
public static final int PROPERTIES = 0, XML = 1;
|
|
142 |
|
|
143 |
/**
|
|
144 |
* get the values
|
|
145 |
* @return an array of the allowed values for this attribute.
|
|
146 |
*/
|
|
147 |
public String[] getValues() {
|
|
148 |
return new String[] {"properties", "xml"};
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* What to do if there is an error in loading the class.
|
|
154 |
* <dl>
|
|
155 |
* <li>error - throw build exception</li>
|
|
156 |
* <li>report - output at warning level</li>
|
|
157 |
* <li>ignore - output at debug level</li>
|
|
158 |
* </dl>
|
|
159 |
*
|
|
160 |
* @param onError an <code>OnError</code> value
|
|
161 |
*/
|
|
162 |
public void setOnError(OnError onError) {
|
|
163 |
this.onError = onError.getIndex();
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Sets the format of the file or resource
|
|
168 |
* @param format the enumerated value - xml or properties
|
|
169 |
*/
|
|
170 |
public void setFormat(Format format) {
|
|
171 |
this.format = format.getIndex();
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* @return the name for this definition
|
|
176 |
*/
|
|
177 |
public String getName() {
|
|
178 |
return name;
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* @return the file containing definitions
|
|
183 |
*/
|
|
184 |
public File getFile() {
|
|
185 |
return file;
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* @return the resource containing definitions
|
|
190 |
*/
|
|
191 |
public String getResource() {
|
|
192 |
return resource;
|
|
193 |
}
|
|
194 |
|
|
195 |
|
|
196 |
/**
|
|
197 |
* Run the definition.
|
|
198 |
*
|
|
199 |
* @exception BuildException if an error occurs
|
|
200 |
*/
|
|
201 |
public void execute() {
|
|
202 |
ClassLoader al = createLoader();
|
|
203 |
|
|
204 |
if (!definerSet) {
|
|
205 |
//we arent fully defined yet. this is an error unless
|
|
206 |
//we are in an antlib, in which case the resource name is determined
|
|
207 |
//automatically.
|
|
208 |
//NB: URIs in the ant core package"" at this point.
|
|
209 |
if (getURI() == null) {
|
|
210 |
throw new BuildException(
|
|
211 |
"name, file or resource attribute of "
|
|
212 |
+ getTaskName() + " is undefined",
|
|
213 |
getLocation());
|
|
214 |
}
|
|
215 |
|
|
216 |
if (getURI().startsWith(MagicNames.ANTLIB_PREFIX)) {
|
|
217 |
//convert the URI to a resource
|
|
218 |
String uri1 = getURI();
|
|
219 |
setResource(makeResourceFromURI(uri1));
|
|
220 |
} else {
|
|
221 |
throw new BuildException(
|
|
222 |
"Only antlib URIs can be located from the URI alone,"
|
|
223 |
+ "not the URI " + getURI());
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
if (name != null) {
|
|
228 |
if (classname == null) {
|
|
229 |
throw new BuildException(
|
|
230 |
"classname attribute of " + getTaskName() + " element "
|
|
231 |
+ "is undefined", getLocation());
|
|
232 |
}
|
|
233 |
addDefinition(al, name, classname);
|
|
234 |
} else {
|
|
235 |
if (classname != null) {
|
|
236 |
String msg = "You must not specify classname "
|
|
237 |
+ "together with file or resource.";
|
|
238 |
throw new BuildException(msg, getLocation());
|
|
239 |
}
|
|
240 |
Enumeration<URL> urls = null;
|
|
241 |
if (file != null) {
|
|
242 |
final URL url = fileToURL();
|
|
243 |
if (url == null) {
|
|
244 |
return;
|
|
245 |
}
|
|
246 |
urls = new Enumeration<URL>() {
|
|
247 |
private boolean more = true;
|
|
248 |
public boolean hasMoreElements() {
|
|
249 |
return more;
|
|
250 |
}
|
|
251 |
public URL nextElement() {
|
|
252 |
if (more) {
|
|
253 |
more = false;
|
|
254 |
return url;
|
|
255 |
} else {
|
|
256 |
throw new NoSuchElementException();
|
|
257 |
}
|
|
258 |
}
|
|
259 |
};
|
|
260 |
} else {
|
|
261 |
urls = resourceToURLs(al);
|
|
262 |
}
|
|
263 |
|
|
264 |
while (urls.hasMoreElements()) {
|
|
265 |
URL url = urls.nextElement();
|
|
266 |
|
|
267 |
int fmt = this.format;
|
|
268 |
if (url.toString().toLowerCase(Locale.US).endsWith(".xml")) {
|
|
269 |
fmt = Format.XML;
|
|
270 |
}
|
|
271 |
|
|
272 |
if (fmt == Format.PROPERTIES) {
|
|
273 |
loadProperties(al, url);
|
|
274 |
break;
|
|
275 |
} else {
|
|
276 |
if (resourceStack.getStack().get(url) != null) {
|
|
277 |
log("Warning: Recursive loading of " + url
|
|
278 |
+ " ignored"
|
|
279 |
+ " at " + getLocation()
|
|
280 |
+ " originally loaded at "
|
|
281 |
+ resourceStack.getStack().get(url),
|
|
282 |
Project.MSG_WARN);
|
|
283 |
} else {
|
|
284 |
try {
|
|
285 |
resourceStack.getStack().put(url, getLocation());
|
|
286 |
loadAntlib(al, url);
|
|
287 |
} finally {
|
|
288 |
resourceStack.getStack().remove(url);
|
|
289 |
}
|
|
290 |
}
|
|
291 |
}
|
|
292 |
}
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* This is where the logic to map from a URI to an antlib resource
|
|
298 |
* is kept.
|
|
299 |
* @param uri the xml namespace uri that to convert.
|
|
300 |
* @return the name of a resource. It may not exist
|
|
301 |
*/
|
|
302 |
|
|
303 |
public static String makeResourceFromURI(String uri) {
|
|
304 |
String path = uri.substring(MagicNames.ANTLIB_PREFIX.length());
|
|
305 |
String resource;
|
|
306 |
if (path.startsWith("//")) {
|
|
307 |
//handle new style full paths to an antlib, in which
|
|
308 |
//all but the forward slashes are allowed.
|
|
309 |
resource = path.substring("//".length());
|
|
310 |
if (!resource.endsWith(".xml")) {
|
|
311 |
//if we haven't already named an XML file, it gets antlib.xml
|
|
312 |
resource = resource + ANTLIB_XML;
|
|
313 |
}
|
|
314 |
} else {
|
|
315 |
//convert from a package to a path
|
|
316 |
resource = path.replace('.', '/') + ANTLIB_XML;
|
|
317 |
}
|
|
318 |
return resource;
|
|
319 |
}
|
|
320 |
|
|
321 |
/**
|
|
322 |
* Convert a file to a file: URL.
|
|
323 |
*
|
|
324 |
* @return the URL, or null if it isn't valid and the active error policy
|
|
325 |
* is not to raise a fault
|
|
326 |
* @throws BuildException if the file is missing/not a file and the
|
|
327 |
* policy requires failure at this point.
|
|
328 |
*/
|
|
329 |
private URL fileToURL() {
|
|
330 |
String message = null;
|
|
331 |
if (!(file.exists())) {
|
|
332 |
message = "File " + file + " does not exist";
|
|
333 |
}
|
|
334 |
if (message == null && !(file.isFile())) {
|
|
335 |
message = "File " + file + " is not a file";
|
|
336 |
}
|
|
337 |
try {
|
|
338 |
if (message == null) {
|
|
339 |
return file.toURI().toURL();
|
|
340 |
}
|
|
341 |
} catch (MalformedURLException ex) {
|
|
342 |
message =
|
|
343 |
"File " + file + " cannot use as URL: "
|
|
344 |
+ ex.toString();
|
|
345 |
}
|
|
346 |
// Here if there is an error
|
|
347 |
switch (onError) {
|
|
348 |
case OnError.FAIL_ALL:
|
|
349 |
throw new BuildException(message);
|
|
350 |
case OnError.FAIL:
|
|
351 |
// Fall Through
|
|
352 |
case OnError.REPORT:
|
|
353 |
log(message, Project.MSG_WARN);
|
|
354 |
break;
|
|
355 |
case OnError.IGNORE:
|
|
356 |
// log at a lower level
|
|
357 |
log(message, Project.MSG_VERBOSE);
|
|
358 |
break;
|
|
359 |
default:
|
|
360 |
// Ignore the problem
|
|
361 |
break;
|
|
362 |
}
|
|
363 |
return null;
|
|
364 |
}
|
|
365 |
|
|
366 |
private Enumeration<URL> resourceToURLs(ClassLoader classLoader) {
|
|
367 |
Enumeration<URL> ret;
|
|
368 |
try {
|
|
369 |
ret = classLoader.getResources(resource);
|
|
370 |
} catch (IOException e) {
|
|
371 |
throw new BuildException(
|
|
372 |
"Could not fetch resources named " + resource,
|
|
373 |
e, getLocation());
|
|
374 |
}
|
|
375 |
if (!ret.hasMoreElements()) {
|
|
376 |
String message = "Could not load definitions from resource "
|
|
377 |
+ resource + ". It could not be found.";
|
|
378 |
switch (onError) {
|
|
379 |
case OnError.FAIL_ALL:
|
|
380 |
throw new BuildException(message);
|
|
381 |
case OnError.FAIL:
|
|
382 |
case OnError.REPORT:
|
|
383 |
log(message, Project.MSG_WARN);
|
|
384 |
break;
|
|
385 |
case OnError.IGNORE:
|
|
386 |
log(message, Project.MSG_VERBOSE);
|
|
387 |
break;
|
|
388 |
default:
|
|
389 |
// Ignore the problem
|
|
390 |
break;
|
|
391 |
}
|
|
392 |
}
|
|
393 |
return ret;
|
|
394 |
}
|
|
395 |
|
|
396 |
/**
|
|
397 |
* Load type definitions as properties from a URL.
|
|
398 |
*
|
|
399 |
* @param al the classloader to use
|
|
400 |
* @param url the url to get the definitions from
|
|
401 |
*/
|
|
402 |
protected void loadProperties(ClassLoader al, URL url) {
|
|
403 |
InputStream is = null;
|
|
404 |
try {
|
|
405 |
is = url.openStream();
|
|
406 |
if (is == null) {
|
|
407 |
log("Could not load definitions from " + url,
|
|
408 |
Project.MSG_WARN);
|
|
409 |
return;
|
|
410 |
}
|
|
411 |
Properties props = new Properties();
|
|
412 |
props.load(is);
|
|
413 |
Enumeration keys = props.keys();
|
|
414 |
while (keys.hasMoreElements()) {
|
|
415 |
name = (String) keys.nextElement();
|
|
416 |
classname = props.getProperty(name);
|
|
417 |
addDefinition(al, name, classname);
|
|
418 |
}
|
|
419 |
} catch (IOException ex) {
|
|
420 |
throw new BuildException(ex, getLocation());
|
|
421 |
} finally {
|
|
422 |
FileUtils.close(is);
|
|
423 |
}
|
|
424 |
}
|
|
425 |
|
|
426 |
/**
|
|
427 |
* Load an antlib from a URL.
|
|
428 |
*
|
|
429 |
* @param classLoader the classloader to use.
|
|
430 |
* @param url the url to load the definitions from.
|
|
431 |
*/
|
|
432 |
private void loadAntlib(ClassLoader classLoader, URL url) {
|
|
433 |
try {
|
|
434 |
Antlib antlib = Antlib.createAntlib(getProject(), url, getURI());
|
|
435 |
antlib.setClassLoader(classLoader);
|
|
436 |
antlib.setURI(getURI());
|
|
437 |
antlib.execute();
|
|
438 |
} catch (BuildException ex) {
|
|
439 |
throw ProjectHelper.addLocationToBuildException(
|
|
440 |
ex, getLocation());
|
|
441 |
}
|
|
442 |
}
|
|
443 |
|
|
444 |
/**
|
|
445 |
* Name of the property file to load
|
|
446 |
* ant name/classname pairs from.
|
|
447 |
* @param file the file
|
|
448 |
*/
|
|
449 |
public void setFile(File file) {
|
|
450 |
if (definerSet) {
|
|
451 |
tooManyDefinitions();
|
|
452 |
}
|
|
453 |
definerSet = true;
|
|
454 |
this.file = file;
|
|
455 |
}
|
|
456 |
|
|
457 |
/**
|
|
458 |
* Name of the property resource to load
|
|
459 |
* ant name/classname pairs from.
|
|
460 |
* @param res the resource to use
|
|
461 |
*/
|
|
462 |
public void setResource(String res) {
|
|
463 |
if (definerSet) {
|
|
464 |
tooManyDefinitions();
|
|
465 |
}
|
|
466 |
definerSet = true;
|
|
467 |
this.resource = res;
|
|
468 |
}
|
|
469 |
|
|
470 |
/**
|
|
471 |
* Antlib attribute, sets resource and uri.
|
|
472 |
* uri is set the antlib value and, resource is set
|
|
473 |
* to the antlib.xml resource in the classpath.
|
|
474 |
* For example antlib="antlib:org.acme.bland.cola"
|
|
475 |
* corresponds to uri="antlib:org.acme.bland.cola"
|
|
476 |
* resource="org/acme/bland/cola/antlib.xml".
|
|
477 |
* @param antlib the value to set.
|
|
478 |
*/
|
|
479 |
public void setAntlib(String antlib) {
|
|
480 |
if (definerSet) {
|
|
481 |
tooManyDefinitions();
|
|
482 |
}
|
|
483 |
if (!antlib.startsWith("antlib:")) {
|
|
484 |
throw new BuildException(
|
|
485 |
"Invalid antlib attribute - it must start with antlib:");
|
|
486 |
}
|
|
487 |
setURI(antlib);
|
|
488 |
this.resource = antlib.substring("antlib:".length()).replace('.', '/')
|
|
489 |
+ "/antlib.xml";
|
|
490 |
definerSet = true;
|
|
491 |
}
|
|
492 |
|
|
493 |
/**
|
|
494 |
* Name of the definition
|
|
495 |
* @param name the name of the definition
|
|
496 |
*/
|
|
497 |
public void setName(String name) {
|
|
498 |
if (definerSet) {
|
|
499 |
tooManyDefinitions();
|
|
500 |
}
|
|
501 |
definerSet = true;
|
|
502 |
this.name = name;
|
|
503 |
}
|
|
504 |
|
|
505 |
/**
|
|
506 |
* Returns the classname of the object we are defining.
|
|
507 |
* May be <code>null</code>.
|
|
508 |
* @return the class name
|
|
509 |
*/
|
|
510 |
public String getClassname() {
|
|
511 |
return classname;
|
|
512 |
}
|
|
513 |
|
|
514 |
/**
|
|
515 |
* The full class name of the object being defined.
|
|
516 |
* Required, unless file or resource have
|
|
517 |
* been specified.
|
|
518 |
* @param classname the name of the class
|
|
519 |
*/
|
|
520 |
public void setClassname(String classname) {
|
|
521 |
this.classname = classname;
|
|
522 |
}
|
|
523 |
|
|
524 |
/**
|
|
525 |
* Set the class name of the adapter class.
|
|
526 |
* An adapter class is used to proxy the
|
|
527 |
* definition class. It is used if the
|
|
528 |
* definition class is not assignable to
|
|
529 |
* the adaptto class, or if the adaptto
|
|
530 |
* class is not present.
|
|
531 |
*
|
|
532 |
* @param adapter the name of the adapter class
|
|
533 |
*/
|
|
534 |
|
|
535 |
public void setAdapter(String adapter) {
|
|
536 |
this.adapter = adapter;
|
|
537 |
}
|
|
538 |
|
|
539 |
/**
|
|
540 |
* Set the adapter class.
|
|
541 |
*
|
|
542 |
* @param adapterClass the class to use to adapt the definition class
|
|
543 |
*/
|
|
544 |
protected void setAdapterClass(Class adapterClass) {
|
|
545 |
this.adapterClass = adapterClass;
|
|
546 |
}
|
|
547 |
|
|
548 |
/**
|
|
549 |
* Set the classname of the class that the definition
|
|
550 |
* must be compatible with, either directly or
|
|
551 |
* by use of the adapter class.
|
|
552 |
*
|
|
553 |
* @param adaptTo the name of the adaptto class
|
|
554 |
*/
|
|
555 |
public void setAdaptTo(String adaptTo) {
|
|
556 |
this.adaptTo = adaptTo;
|
|
557 |
}
|
|
558 |
|
|
559 |
/**
|
|
560 |
* Set the class for adaptToClass, to be
|
|
561 |
* used by derived classes, used instead of
|
|
562 |
* the adaptTo attribute.
|
|
563 |
*
|
|
564 |
* @param adaptToClass the class for adapto.
|
|
565 |
*/
|
|
566 |
protected void setAdaptToClass(Class adaptToClass) {
|
|
567 |
this.adaptToClass = adaptToClass;
|
|
568 |
}
|
|
569 |
|
|
570 |
|
|
571 |
/**
|
|
572 |
* Add a definition using the attributes of Definer
|
|
573 |
*
|
|
574 |
* @param al the ClassLoader to use
|
|
575 |
* @param name the name of the definition
|
|
576 |
* @param classname the classname of the definition
|
|
577 |
* @exception BuildException if an error occurs
|
|
578 |
*/
|
|
579 |
protected void addDefinition(ClassLoader al, String name, String classname)
|
|
580 |
{
|
|
581 |
Class<?> cl = null;
|
|
582 |
try {
|
|
583 |
try {
|
|
584 |
name = ProjectHelper.genComponentName(getURI(), name);
|
|
585 |
|
|
586 |
if (onError != OnError.IGNORE) {
|
|
587 |
cl = Class.forName(classname, true, al);
|
|
588 |
}
|
|
589 |
|
|
590 |
if (adapter != null) {
|
|
591 |
adapterClass = Class.forName(adapter, true, al);
|
|
592 |
}
|
|
593 |
|
|
594 |
if (adaptTo != null) {
|
|
595 |
adaptToClass = Class.forName(adaptTo, true, al);
|
|
596 |
}
|
|
597 |
|
|
598 |
AntTypeDefinition def = new AntTypeDefinition();
|
|
599 |
def.setName(name);
|
|
600 |
def.setClassName(classname);
|
|
601 |
def.setClass(cl);
|
|
602 |
def.setAdapterClass(adapterClass);
|
|
603 |
def.setAdaptToClass(adaptToClass);
|
|
604 |
def.setClassLoader(al);
|
|
605 |
if (cl != null) {
|
|
606 |
def.checkClass(getProject());
|
|
607 |
}
|
|
608 |
ComponentHelper.getComponentHelper(getProject())
|
|
609 |
.addDataTypeDefinition(def);
|
|
610 |
} catch (ClassNotFoundException cnfe) {
|
|
611 |
String msg = getTaskName() + " class " + classname
|
|
612 |
+ " cannot be found";
|
|
613 |
throw new BuildException(msg, cnfe, getLocation());
|
|
614 |
} catch (NoClassDefFoundError ncdfe) {
|
|
615 |
String msg = getTaskName() + " A class needed by class "
|
|
616 |
+ classname + " cannot be found: " + ncdfe.getMessage();
|
|
617 |
throw new BuildException(msg, ncdfe, getLocation());
|
|
618 |
}
|
|
619 |
} catch (BuildException ex) {
|
|
620 |
switch (onError) {
|
|
621 |
case OnError.FAIL_ALL:
|
|
622 |
case OnError.FAIL:
|
|
623 |
throw ex;
|
|
624 |
case OnError.REPORT:
|
|
625 |
log(ex.getLocation() + "Warning: " + ex.getMessage(),
|
|
626 |
Project.MSG_WARN);
|
|
627 |
break;
|
|
628 |
default:
|
|
629 |
log(ex.getLocation() + ex.getMessage(),
|
|
630 |
Project.MSG_DEBUG);
|
|
631 |
}
|
|
632 |
}
|
|
633 |
}
|
|
634 |
|
|
635 |
/**
|
|
636 |
* handle too many definitions by raising an exception.
|
|
637 |
* @throws BuildException always.
|
|
638 |
*/
|
|
639 |
private void tooManyDefinitions() {
|
|
640 |
throw new BuildException(
|
|
641 |
"Only one of the attributes name, file and resource"
|
|
642 |
+ " can be set", getLocation());
|
|
643 |
}
|
|
644 |
}
|