diff --git a/docs/manual/CoreTasks/macrodef.html b/docs/manual/CoreTasks/macrodef.html index 38820023e..5470b4071 100644 --- a/docs/manual/CoreTasks/macrodef.html +++ b/docs/manual/CoreTasks/macrodef.html @@ -43,16 +43,6 @@
+ This is used to specify the treatment of text contents of the macrodef. + If this element is not present, then any nested text in the macro + will be an error. If the text element is present, then the name + becomes an attribute that gets set to the nested text of the macro. + Since ant 1.6.1. +
++ The case of the text name is ignored. +
+| Attribute | +Description | +Required | +
| name | +The name of the text attribute | +Yes | +
| optional | ++ If true nested text in the macro is optional, default is "false". + | +No | +
| trim | ++ If true, the nested text is trimmed of white space, + default is "false". + | +No | +
| description | ++ This contains a description + informing the user what the nested text of the macro is expected + to be. + | +No | +
@@ -211,11 +249,12 @@
- The following shows the use of the textname attribute.
+ The following shows the use of the text element.
-<macrodef name="echotest" textname="text">
+<macrodef name="echotest">
+ <text name="text"/text>
<sequential>
<echo>@{text}</echo>
</sequential>
diff --git a/src/etc/testcases/taskdefs/macrodef.xml b/src/etc/testcases/taskdefs/macrodef.xml
index 01dcd12b5..f114816de 100644
--- a/src/etc/testcases/taskdefs/macrodef.xml
+++ b/src/etc/testcases/taskdefs/macrodef.xml
@@ -102,8 +102,9 @@
-
-
+
+
+
@{text}
@@ -113,8 +114,31 @@
+
+
+
+
+ [@{text}]
+
+
+
+ Hello world
+
+
+
-
+
+
+
+
+ @{text}
+
+
+
+
+
+
+
@{text}
diff --git a/src/main/org/apache/tools/ant/taskdefs/MacroDef.java b/src/main/org/apache/tools/ant/taskdefs/MacroDef.java
index 80c72a1ed..48069a84d 100644
--- a/src/main/org/apache/tools/ant/taskdefs/MacroDef.java
+++ b/src/main/org/apache/tools/ant/taskdefs/MacroDef.java
@@ -59,6 +59,7 @@ import java.util.List;
import java.util.Map;
import java.util.Locale;
import java.util.HashMap;
+import java.util.Iterator;
import org.apache.tools.ant.AntTypeDefinition;
import org.apache.tools.ant.BuildException;
@@ -82,6 +83,7 @@ public class MacroDef extends AntlibDefinition {
private List attributes = new ArrayList();
private Map elements = new HashMap();
private String textName = null;
+ private Text text = null;
/**
* Name of the definition
@@ -92,22 +94,39 @@ public class MacroDef extends AntlibDefinition {
}
/**
- * Name of the text attribute.
- * @param textName the name of the attribute to use for the
- * text content of the macro.
+ * Add the text element.
+ * @param text the nested text element to add
* @since ant 1.6.1
*/
- public void setTextName(String textName) {
- this.textName = textName;
+ public void addConfiguredText(Text text) {
+ if (this.text != null) {
+ throw new BuildException(
+ "Only one nested text element allowed");
+ }
+ if (text.getName() == null) {
+ throw new BuildException(
+ "the text nested element needed a \"name\" attribute");
+ }
+ // Check if used by attributes
+ for (Iterator i = attributes.iterator(); i.hasNext();) {
+ Attribute attribute = (Attribute) i.next();
+ if (text.getName().equals(attribute.getName())) {
+ throw new BuildException(
+ "the name \"" + text.getName()
+ + "\" is already used as an attribute");
+ }
+ }
+ this.text = text;
+ this.textName = text.getName();
}
/**
- * @return the name of the text content attribute
+ * @return the nested text element
* @since ant 1.6.1
*/
- public String getTextName() {
- return textName;
+ public Text getText() {
+ return text;
}
/**
@@ -245,7 +264,7 @@ public class MacroDef extends AntlibDefinition {
if (attribute.getName().equals(textName)) {
throw new BuildException(
"the attribute name \"" + attribute.getName()
- + "\" has already been used by the textname attribute");
+ + "\" has already been used by the text element");
}
for (int i = 0; i < attributes.size(); ++i) {
if (((Attribute) attributes.get(i)).getName().equals(
@@ -403,6 +422,122 @@ public class MacroDef extends AntlibDefinition {
}
}
+ /**
+ * A nested text element for the MacroDef task.
+ * @since ant 1.6.1
+ */
+ public static class Text {
+ private String name;
+ private boolean optional;
+ private boolean trim;
+ private String description;
+
+ /**
+ * The name of the attribute.
+ *
+ * @param name the name of the attribute
+ */
+ public void setName(String name) {
+ if (!isValidName(name)) {
+ throw new BuildException(
+ "Illegal name [" + name + "] for attribute");
+ }
+ this.name = name.toLowerCase(Locale.US);
+ }
+
+ /**
+ * @return the name of the attribute
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * The optional attribute of the text element.
+ *
+ * @param optional if true this is optional
+ */
+ public void setOptional(boolean optional) {
+ this.optional = optional;
+ }
+
+ /**
+ * @return true if the text is optional
+ */
+ public boolean getOptional() {
+ return optional;
+ }
+
+ /**
+ * The trim attribute of the text element.
+ *
+ * @param trim if true this String.trim() is called on
+ * the contents of the text element.
+ */
+ public void setTrim(boolean trim) {
+ this.trim = trim;
+ }
+
+ /**
+ * @return true if the text is trim
+ */
+ public boolean getTrim() {
+ return trim;
+ }
+
+ /**
+ * @param desc Description of the text.
+ */
+ public void setDescription(String desc) {
+ description = desc;
+ }
+
+ /**
+ * @return the description of the text, or null if
+ * no description is available.
+ */
+ public String getDescription() {
+ return description;
+ }
+
+ /**
+ * equality method
+ *
+ * @param obj an Object value
+ * @return a boolean value
+ */
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (obj.getClass() != getClass()) {
+ return false;
+ }
+ Text other = (Text) obj;
+ if (name == null) {
+ if (other.name != null) {
+ return false;
+ }
+ } else if (!name.equals(other.name)) {
+ return false;
+ }
+ if (optional != other.optional) {
+ return false;
+ }
+ if (trim != other.trim) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @return a hash code value for this object.
+ */
+ public int hashCode() {
+ return objectHashCode(name);
+ }
+ }
+
/**
* A nested element for the MacroDef task.
*
@@ -519,12 +654,12 @@ public class MacroDef extends AntlibDefinition {
if (!name.equals(other.name)) {
return false;
}
- if (textName == null) {
- if (other.textName != null) {
+ if (text == null) {
+ if (other.text != null) {
return false;
}
} else {
- if (!textName.equals(other.textName)) {
+ if (!text.equals(other.text)) {
return false;
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java b/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java
index d02dcff02..a90bf9ad1 100644
--- a/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java
+++ b/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java
@@ -88,7 +88,7 @@ public class MacroInstance extends Task implements DynamicConfigurator {
private Map nsElements = null;
private Map presentElements = new HashMap();
private Hashtable localProperties = new Hashtable();
- private String text = "";
+ private String text = null;
/**
* Called from MacroDef.MyAntTypeDefinition#create()
@@ -251,6 +251,7 @@ public class MacroInstance extends Task implements DynamicConfigurator {
* Set the text contents for the macro.
* @param text the text to be added to the macro.
*/
+
public void addText(String text) {
this.text = text;
}
@@ -340,10 +341,25 @@ public class MacroInstance extends Task implements DynamicConfigurator {
if (copyKeys.contains("id")) {
copyKeys.remove("id");
}
- if (macroDef.getTextName() != null) {
- localProperties.put(macroDef.getTextName(), text);
+ if (macroDef.getText() != null) {
+ if (text == null) {
+ if (!macroDef.getText().getOptional()) {
+ throw new BuildException(
+ "required text missing");
+ }
+ text = "";
+ }
+ if (macroDef.getText().getTrim()) {
+ text = text.trim();
+ }
+ localProperties.put(macroDef.getText().getName(), text);
+ } else {
+ if (text != null && !text.trim().equals("")) {
+ throw new BuildException(
+ "The \"" + getTaskName() + "\" macro does not support"
+ + " nested text data.");
+ }
}
-
if (copyKeys.size() != 0) {
throw new BuildException(
"Unknown attribute" + (copyKeys.size() > 1 ? "s " : " ")
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/MacroDefTest.java b/src/testcases/org/apache/tools/ant/taskdefs/MacroDefTest.java
index a7290704e..1366a539d 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/MacroDefTest.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/MacroDefTest.java
@@ -116,15 +116,25 @@ public class MacroDefTest extends BuildFileTest {
"nested elementnested element");
}
- public void testTextName() {
+ public void testTextElement() {
expectLogContaining(
- "textname", "Hello world");
+ "textelement", "Hello world");
+ }
+
+ public void testTextTrim() {
+ expectLogContaining(
+ "text.trim", "[Hello world]");
}
public void testDuplicateTextName() {
expectBuildException(
"duplicatetextname",
- "the attribute text has already been specified");
+ "the name \"text\" is already used as an attribute");
+ }
+ public void testDuplicateTextName2() {
+ expectBuildException(
+ "duplicatetextname2",
+ "the attribute name \"text\" has already been used by the text element");
}
}