Obtained from: Kevin Jackson git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277156 13f79535-47bb-0310-9956-ffa450edef68master
@@ -29,63 +29,87 @@ import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver; | |||||
* | * | ||||
* @version $Revision$ $Date$ | * @version $Revision$ $Date$ | ||||
*/ | */ | ||||
public class AntResolver | |||||
implements ExtensionResolver { | |||||
private File m_antfile; | |||||
private File m_destfile; | |||||
private String m_target; | |||||
public class AntResolver implements ExtensionResolver { | |||||
private File antfile; | |||||
private File destfile; | |||||
private String target; | |||||
public void setAntfile(File antfile) { | |||||
m_antfile = antfile; | |||||
/** | |||||
* Sets the ant file | |||||
* @param antfile the ant file to set | |||||
*/ | |||||
public void setAntfile(final File antfile) { | |||||
this.antfile = antfile; | |||||
} | } | ||||
public void setDestfile(File destfile) { | |||||
m_destfile = destfile; | |||||
/** | |||||
* Sets the destination file | |||||
* @param destfile the destination file | |||||
*/ | |||||
public void setDestfile(final File destfile) { | |||||
this.destfile = destfile; | |||||
} | } | ||||
/** | |||||
* Sets the target | |||||
* @param target the target | |||||
*/ | |||||
public void setTarget(final String target) { | public void setTarget(final String target) { | ||||
m_target = target; | |||||
this.target = target; | |||||
} | } | ||||
/** | |||||
* Returns the resolved file | |||||
* @param extension the extension | |||||
* @param project the project | |||||
* @return the file resolved | |||||
* @throws BuildException if the file cannot be resolved | |||||
*/ | |||||
public File resolve(final Extension extension, | public File resolve(final Extension extension, | ||||
final Project project) | |||||
throws BuildException { | |||||
final Project project) throws BuildException { | |||||
validate(); | validate(); | ||||
final Ant ant = (Ant) project.createTask("ant"); | final Ant ant = (Ant) project.createTask("ant"); | ||||
ant.setInheritAll(false); | ant.setInheritAll(false); | ||||
ant.setAntfile(m_antfile.getName()); | |||||
ant.setAntfile(antfile.getName()); | |||||
try { | try { | ||||
final File dir = | final File dir = | ||||
m_antfile.getParentFile().getCanonicalFile(); | |||||
antfile.getParentFile().getCanonicalFile(); | |||||
ant.setDir(dir); | ant.setDir(dir); | ||||
} catch (final IOException ioe) { | } catch (final IOException ioe) { | ||||
throw new BuildException(ioe.getMessage(), ioe); | throw new BuildException(ioe.getMessage(), ioe); | ||||
} | } | ||||
if (null != m_target) { | |||||
ant.setTarget(m_target); | |||||
if (null != target) { | |||||
ant.setTarget(target); | |||||
} | } | ||||
ant.execute(); | ant.execute(); | ||||
return m_destfile; | |||||
return destfile; | |||||
} | } | ||||
/* | |||||
* Validates URL | |||||
*/ | |||||
private void validate() { | private void validate() { | ||||
if (null == m_antfile) { | |||||
if (null == antfile) { | |||||
final String message = "Must specify Buildfile"; | final String message = "Must specify Buildfile"; | ||||
throw new BuildException(message); | throw new BuildException(message); | ||||
} | } | ||||
if (null == m_destfile) { | |||||
if (null == destfile) { | |||||
final String message = "Must specify destination file"; | final String message = "Must specify destination file"; | ||||
throw new BuildException(message); | throw new BuildException(message); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Returns a string representation | |||||
* @return the string representation | |||||
*/ | |||||
public String toString() { | public String toString() { | ||||
return "Ant[" + m_antfile + "==>" + m_destfile + "]"; | |||||
return "Ant[" + antfile + "==>" + destfile + "]"; | |||||
} | } | ||||
} | } |
@@ -27,26 +27,38 @@ import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver; | |||||
* | * | ||||
* @version $Revision$ $Date$ | * @version $Revision$ $Date$ | ||||
*/ | */ | ||||
public class LocationResolver | |||||
implements ExtensionResolver { | |||||
private String m_location; | |||||
public class LocationResolver implements ExtensionResolver { | |||||
private String location; | |||||
/** | |||||
* Sets the location for this resolver | |||||
* @param location the location | |||||
*/ | |||||
public void setLocation(final String location) { | public void setLocation(final String location) { | ||||
m_location = location; | |||||
this.location = location; | |||||
} | } | ||||
/** | |||||
* Returns the resolved file | |||||
* @param extension the extension | |||||
* @param project the project | |||||
* @return the file resolved | |||||
* @throws BuildException if no location is set | |||||
*/ | |||||
public File resolve(final Extension extension, | public File resolve(final Extension extension, | ||||
final Project project) | |||||
throws BuildException { | |||||
if (null == m_location) { | |||||
final Project project) throws BuildException { | |||||
if (null == location) { | |||||
final String message = "No location specified for resolver"; | final String message = "No location specified for resolver"; | ||||
throw new BuildException(message); | throw new BuildException(message); | ||||
} | } | ||||
return project.resolveFile(m_location); | |||||
return project.resolveFile(location); | |||||
} | } | ||||
/** | |||||
* Returns a string representation of the Location | |||||
* @return the string representation | |||||
*/ | |||||
public String toString() { | public String toString() { | ||||
return "Location[" + m_location + "]"; | |||||
return "Location[" + location + "]"; | |||||
} | } | ||||
} | } |
@@ -29,45 +29,66 @@ import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver; | |||||
* | * | ||||
* @version $Revision$ $Date$ | * @version $Revision$ $Date$ | ||||
*/ | */ | ||||
public class URLResolver | |||||
implements ExtensionResolver { | |||||
private File m_destfile; | |||||
private File m_destdir; | |||||
private URL m_url; | |||||
public class URLResolver implements ExtensionResolver { | |||||
private File destfile; | |||||
private File destdir; | |||||
private URL url; | |||||
/** | |||||
* Sets the URL | |||||
* @param url the url | |||||
*/ | |||||
public void setUrl(final URL url) { | public void setUrl(final URL url) { | ||||
m_url = url; | |||||
this.url = url; | |||||
} | } | ||||
/** | |||||
* Sets the destination file | |||||
* @param destfile the destination file | |||||
*/ | |||||
public void setDestfile(final File destfile) { | public void setDestfile(final File destfile) { | ||||
m_destfile = destfile; | |||||
this.destfile = destfile; | |||||
} | } | ||||
/** | |||||
* Sets the destination directory | |||||
* @param destdir the destination directory | |||||
*/ | |||||
public void setDestdir(final File destdir) { | public void setDestdir(final File destdir) { | ||||
m_destdir = destdir; | |||||
this.destdir = destdir; | |||||
} | } | ||||
/** | |||||
* Returns the file resolved from URL and directory | |||||
* @param extension the extention | |||||
* @param project the project | |||||
* @return file the file resolved | |||||
* @throws BuildException if the URL is invalid | |||||
*/ | |||||
public File resolve(final Extension extension, | public File resolve(final Extension extension, | ||||
final Project project) | |||||
throws BuildException { | |||||
final Project project) throws BuildException { | |||||
validate(); | validate(); | ||||
final File file = getDest(); | final File file = getDest(); | ||||
final Get get = (Get) project.createTask("get"); | final Get get = (Get) project.createTask("get"); | ||||
get.setDest(file); | get.setDest(file); | ||||
get.setSrc(m_url); | |||||
get.setSrc(url); | |||||
get.execute(); | get.execute(); | ||||
return file; | return file; | ||||
} | } | ||||
/* | |||||
* Gets the destination file | |||||
*/ | |||||
private File getDest() { | private File getDest() { | ||||
if (null != m_destfile) { | |||||
return m_destfile; | |||||
File result; | |||||
if (null != destfile) { | |||||
result = destfile; | |||||
} else { | } else { | ||||
final String file = m_url.getFile(); | |||||
String filename = null; | |||||
final String file = url.getFile(); | |||||
String filename; | |||||
if (null == file || file.length() <= 1) { | if (null == file || file.length() <= 1) { | ||||
filename = "default.file"; | filename = "default.file"; | ||||
} else { | } else { | ||||
@@ -77,27 +98,34 @@ public class URLResolver | |||||
} | } | ||||
filename = file.substring(index); | filename = file.substring(index); | ||||
} | } | ||||
return new File(m_destdir, filename); | |||||
result = new File(destdir, filename); | |||||
} | } | ||||
return result; | |||||
} | } | ||||
/* | |||||
* Validates URL | |||||
*/ | |||||
private void validate() { | private void validate() { | ||||
if (null == m_url) { | |||||
if (null == url) { | |||||
final String message = "Must specify URL"; | final String message = "Must specify URL"; | ||||
throw new BuildException(message); | throw new BuildException(message); | ||||
} | } | ||||
if (null == m_destdir && null == m_destfile) { | |||||
if (null == destdir && null == destfile) { | |||||
final String message = "Must specify destination file or directory"; | final String message = "Must specify destination file or directory"; | ||||
throw new BuildException(message); | throw new BuildException(message); | ||||
} else if (null != m_destdir && null != m_destfile) { | |||||
} else if (null != destdir && null != destfile) { | |||||
final String message = "Must not specify both destination file or directory"; | final String message = "Must not specify both destination file or directory"; | ||||
throw new BuildException(message); | throw new BuildException(message); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Returns a string representation of the URL | |||||
* @return the string representation | |||||
*/ | |||||
public String toString() { | public String toString() { | ||||
return "URL[" + m_url + "]"; | |||||
return "URL[" + url + "]"; | |||||
} | } | ||||
} | } |