Browse Source

Added 'prefix' option to <property>. Designed to only work when loading a property file or resource.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271519 13f79535-47bb-0310-9956-ffa450edef68
master
Erik Hatcher 23 years ago
parent
commit
2ccd335cf3
3 changed files with 43 additions and 0 deletions
  1. +9
    -0
      src/etc/testcases/taskdefs/property.xml
  2. +17
    -0
      src/main/org/apache/tools/ant/taskdefs/Property.java
  3. +17
    -0
      src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java

+ 9
- 0
src/etc/testcases/taskdefs/property.xml View File

@@ -21,4 +21,13 @@
<property file="property3.properties"/>
<echo message="http.url is ${http.url}"/>
</target>
<target name="prefix.success">
<property file="property3.properties" prefix="server1"/>
</target>

<target name="prefix.fail">
<property name="someprop" value="value" prefix="prefix"/>
</target>
</project>

+ 17
- 0
src/main/org/apache/tools/ant/taskdefs/Property.java View File

@@ -86,6 +86,7 @@ public class Property extends Task {
protected Path classpath;
protected String env;
protected Reference ref;
protected String prefix;

protected boolean userProperty; // set read-only properties

@@ -124,6 +125,13 @@ public class Property extends Task {
public File getFile() {
return file;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
if (!prefix.endsWith(".")) {
this.prefix += ".";
}
}

public void setRefid(Reference ref) {
this.ref = ref;
@@ -192,6 +200,10 @@ public class Property extends Task {
location);
}
}
if (file == null && resource == null && prefix != null) {
throw new BuildException("Prefix is only valid when loading from a file or resource", location);
}

if ((name != null) && (value != null)) {
addProperty(name, value);
@@ -298,6 +310,11 @@ public class Property extends Task {
String value = props.getProperty(name);

String v = project.replaceProperties(value);

if (prefix != null) {
name = prefix + name;
}

addProperty(name, v);
}
}


+ 17
- 0
src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java View File

@@ -95,4 +95,21 @@ public class PropertyTest extends BuildFileTest {
expectLog("test4", "http.url is http://localhost:999");
}
public void testPrefixSuccess() {
executeTarget("prefix.success");
assertEquals("80", project.getProperty("server1.http.port"));
}

public void testPrefixFailure() {
try {
executeTarget("prefix.fail");
}
catch (BuildException e) {
assertEquals("Prefix allowed on non-resource/file load - ", true,
e.getMessage().indexOf("Prefix is only valid") != -1);
return;
}
fail("Did not throw exception on invalid use of prefix");
}
}

Loading…
Cancel
Save