Browse Source

support creating url resources relative to other URLs

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@880590 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
e1cd13c79b
7 changed files with 82 additions and 13 deletions
  1. +2
    -2
      docs/manual/CoreTasks/import.html
  2. +2
    -2
      docs/manual/CoreTasks/include.html
  3. +10
    -0
      docs/manual/CoreTypes/resources.html
  4. +10
    -6
      src/main/org/apache/tools/ant/helper/AntXMLContext.java
  5. +1
    -0
      src/main/org/apache/tools/ant/helper/ProjectHelper2.java
  6. +44
    -1
      src/main/org/apache/tools/ant/types/resources/URLResource.java
  7. +13
    -2
      src/tests/antunit/taskdefs/import-url-test.xml

+ 2
- 2
docs/manual/CoreTasks/import.html View File

@@ -150,9 +150,9 @@ project).</p>
To create a relative resource you'd use something like:</p>

<pre>
&lt;url id="imported.basedir" url="${ant.file.imported}/."/&gt;
&lt;loadproperties&gt;
&lt;url url="${toString:imported.basedir}/imported.properties"/&gt;
&lt;url baseUrl="${ant.file.imported}"
relativePath="imported.properties"/&gt;
&lt;/loadproperties&gt;
</pre>



+ 2
- 2
docs/manual/CoreTasks/include.html View File

@@ -153,9 +153,9 @@ project).</p>
To create a relative resource you'd use something like:</p>

<pre>
&lt;url id="included.basedir" url="${ant.file.included}/."/&gt;
&lt;loadproperties&gt;
&lt;url url="${toString:included.basedir}/included.properties"/&gt;
&lt;url baseUrl="${ant.file.included}"
relativePath="included.properties"/&gt;
&lt;/loadproperties&gt;
</pre>



+ 10
- 0
docs/manual/CoreTypes/resources.html View File

@@ -254,6 +254,16 @@ element.</p>
<td valign="top">file</td>
<td valign="top">The file to expose as a file: url</td>
</tr>
<tr>
<td valign="top">baseUrl</td>
<td valign="top">The base URL which must be combined with relativePath</td>
</tr>
<tr>
<td valign="top">relativePath</td>
<td valign="top">Relative path that defines the url combined with
baseUrl</td>
<td align="center" valign="top">If using baseUrl</td>
</tr>
</table>

<h4><a name="string">string</a></h4>


+ 10
- 6
src/main/org/apache/tools/ant/helper/AntXMLContext.java View File

@@ -124,12 +124,16 @@ public class AntXMLContext {
*/
public void setBuildFile(File buildFile) {
this.buildFile = buildFile;
this.buildFileParent = new File(buildFile.getParent());
implicitTarget.setLocation(new Location(buildFile.getAbsolutePath()));
try {
setBuildFile(FileUtils.getFileUtils().getFileURL(buildFile));
} catch (MalformedURLException ex) {
throw new BuildException(ex);
if (buildFile != null) {
this.buildFileParent = new File(buildFile.getParent());
implicitTarget.setLocation(new Location(buildFile.getAbsolutePath()));
try {
setBuildFile(FileUtils.getFileUtils().getFileURL(buildFile));
} catch (MalformedURLException ex) {
throw new BuildException(ex);
}
} else {
this.buildFileParent = null;
}
}



+ 1
- 0
src/main/org/apache/tools/ant/helper/ProjectHelper2.java View File

@@ -219,6 +219,7 @@ public class ProjectHelper2 extends ProjectHelper {
buildFileName = buildFile.toString();
} else if (url != null) {
try {
context.setBuildFile((File) null);
context.setBuildFile(url);
} catch (java.net.MalformedURLException ex) {
throw new BuildException(ex);


+ 44
- 1
src/main/org/apache/tools/ant/types/resources/URLResource.java View File

@@ -46,6 +46,8 @@ public class URLResource extends Resource implements URLProvider {

private URL url;
private URLConnection conn;
private URL baseURL;
private String relPath;

/**
* Default constructor.
@@ -107,6 +109,34 @@ public class URLResource extends Resource implements URLProvider {
}
}

/**
* Base URL which combined with the relativePath attribute defines
* the URL.
* @since Ant 1.8.0
*/
public synchronized void setBaseURL(URL base) {
checkAttributesAllowed();
if (url != null) {
throw new BuildException("can't define URL and baseURL attribute");
}
baseURL = base;
}

/**
* Relative path which combined with the baseURL attribute defines
* the URL.
* @since Ant 1.8.0
*/
public synchronized void setRelativePath(String r) {
checkAttributesAllowed();
if (url != null) {
throw new BuildException("can't define URL and relativePath"
+ " attribute");
}
relPath = r;
}


/**
* Get the URL used by this URLResource.
* @return a URL object.
@@ -115,6 +145,19 @@ public class URLResource extends Resource implements URLProvider {
if (isReference()) {
return ((URLResource) getCheckedRef()).getURL();
}
if (url == null) {
if (baseURL != null) {
if (relPath == null) {
throw new BuildException("must provide relativePath"
+ " attribute when using baseURL.");
}
try {
url = new URL(baseURL, relPath);
} catch (MalformedURLException e) {
throw new BuildException(e);
}
}
}
return url;
}

@@ -124,7 +167,7 @@ public class URLResource extends Resource implements URLProvider {
*/
public synchronized void setRefid(Reference r) {
//not using the accessor in this case to avoid side effects
if (url != null) {
if (url != null || baseURL != null || relPath != null) {
throw tooManyAttributes();
}
super.setRefid(r);


+ 13
- 2
src/tests/antunit/taskdefs/import-url-test.xml View File

@@ -21,16 +21,25 @@
<mkdir dir="${input}/a/b"/>
<mkdir dir="${input}/a/c"/>
<echo file="${input}/a/b/outer.xml"><![CDATA[
<project>
<project name="outer">
<import file="../c/inner.xml"/>
</project>
]]></echo>
<echo file="${input}/a/c/inner.xml"><![CDATA[
<project>
<project name="inner">
<target name="foo">
<echo>In inner</echo>
<echo>ant.file.inner is ${ant.file.inner}</echo>
<echo>type is ${ant.file.type.inner}</echo>
<loadproperties>
<url baseUrl="${ant.file.inner}" relativePath="test.properties"/>
</loadproperties>
<echo>foo is ${foo}</echo>
</target>
</project>]]></echo>
<echo file="${input}/a/c/test.properties"><![CDATA[
foo=bar
]]></echo>
<mkdir dir="${output}"/>
<jar destfile="${output}/test.jar">
<fileset dir="${input}"/>
@@ -45,5 +54,7 @@

<target name="testImportOfNestedFile" depends="foo">
<au:assertLogContains text="In inner"/>
<au:assertLogContains text="type is url"/>
<au:assertLogContains text="foo is bar"/>
</target>
</project>

Loading…
Cancel
Save