Browse Source

Added a helper class for enumerated attributes and demonstrated its

usage in FixCRLF.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267766 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
084609e5ef
3 changed files with 42 additions and 12 deletions
  1. +4
    -0
      docs/index.html
  2. +17
    -0
      src/main/org/apache/tools/ant/IntrospectionHelper.java
  3. +21
    -12
      src/main/org/apache/tools/ant/taskdefs/FixCRLF.java

+ 4
- 0
docs/index.html View File

@@ -3171,6 +3171,10 @@ output.
value of the attribute is interpreted relative to the project's basedir)
or any other type that has a constructor with a single <code>String</code>
argument</li>
<li>If your task has enumerated attributes, you should consider using
a subclass of org.apache.tools.ant.EnumeratedAttribute as argument
to your setter method. See org.apache.tools.ant.taskdefs.FixCRLF for
an example.</li>
<li>If the task should support character data, write a <code>public void
addText(String)</code> method.</li>
<li>For each nested element, write a create or add method. A create method


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

@@ -438,6 +438,23 @@ public class IntrospectionHelper {

};

// EnumeratedAttributes have their own helper class
} else if (org.apache.tools.ant.EnumeratedAttribute.class.isAssignableFrom(arg)) {
return new AttributeSetter() {
public void set(Project p, Object parent, String value)
throws InvocationTargetException, IllegalAccessException, BuildException {
try {
EnumeratedAttribute ea =
(EnumeratedAttribute)arg.newInstance();
ea.setValue(value);
m.invoke(parent, new EnumeratedAttribute[] {ea});
} catch (InstantiationException ie) {
throw new BuildException(ie);
}
}
};

// worst case. look for a public String constructor and use it
} else {



+ 21
- 12
src/main/org/apache/tools/ant/taskdefs/FixCRLF.java View File

@@ -139,15 +139,15 @@ public class FixCRLF extends MatchingTask {
* <li>remove: remove all CR characters
* </ul>
*/
public void setCr(String option) {
public void setCr(AddAsisRemove attr) {
String option = attr.getValue();
if (option.equals("remove")) {
addcr = -1;
} else if (option.equals("asis")) {
addcr = 0;
} else if (option.equals("add")) {
addcr = +1;
} else {
throw new BuildException("Invalid option: " + option );
// must be "add"
addcr = +1;
}
}

@@ -161,15 +161,15 @@ public class FixCRLF extends MatchingTask {
* <li>remove: convert tabs to spaces
* </ul>
*/
public void setTab(String option) {
public void setTab(AddAsisRemove attr) {
String option = attr.getValue();
if (option.equals("remove")) {
addtab = -1;
} else if (option.equals("asis")) {
addtab = 0;
} else if (option.equals("add")) {
addtab = +1;
} else {
throw new BuildException("Invalid option: " + option );
// must be "add"
addtab = +1;
}
}

@@ -196,15 +196,15 @@ public class FixCRLF extends MatchingTask {
* <li>remove: remove any eof character found at the end
* </ul>
*/
public void setEof(String option) {
public void setEof(AddAsisRemove attr) {
String option = attr.getValue();
if (option.equals("remove")) {
ctrlz = -1;
} else if (option.equals("asis")) {
ctrlz = 0;
} else if (option.equals("add")) {
ctrlz = +1;
} else {
throw new BuildException("Invalid option: " + option );
// must be "add"
ctrlz = +1;
}
}

@@ -370,4 +370,13 @@ public class FixCRLF extends MatchingTask {

} /* end for */
}

/**
* Enumerated attribute with the values "asis", "add" and "remove".
*/
public static class AddAsisRemove extends EnumeratedAttribute {
public String[] getValues() {
return new String[] {"add", "asis", "remove"};
}
}
}

Loading…
Cancel
Save