You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/06/14 12:24:15 UTC
cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/ant example_tasks.jelly
jstrachan 2002/06/14 03:24:14
Modified: jelly build.xml
jelly/src/java/org/apache/commons/jelly TagSupport.java
Tag.java
jelly/src/java/org/apache/commons/jelly/impl
DynaTagScript.java
jelly/src/java/org/apache/commons/jelly/tags/ant
TaskSource.java TaskPropertyTag.java TaskTag.java
AntTagLibrary.java
jelly/src/test/org/apache/commons/jelly/ant
example_tasks.jelly
Log:
Patched the Ant tag library so that Task objects are created for each tag invocation.
This will avoid any problems of looping over a Task object since a Task in Ant often uses
methods like addText() without ever clearing the text down; so it gives us a sure-fire way
to know that the Task gets properly initialized each time.
Also added generic support for the addText(String) method on Tasks using reflection such that
a Tasks' body can be the text value; such as for <echo> or <sql>
Revision Changes Path
1.47 +1 -1 jakarta-commons-sandbox/jelly/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/build.xml,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -r1.46 -r1.47
--- build.xml 14 Jun 2002 06:53:02 -0000 1.46
+++ build.xml 14 Jun 2002 10:24:14 -0000 1.47
@@ -189,7 +189,7 @@
<delete file="velocity.log"/>
</target>
- <target name="compile" depends="maven:compile, maven:jar-resources">
+ <target name="compile" depends="maven:compile">
<path id="test.classpath">
<pathelement path="${maven.build.dest}"/>
1.12 +6 -6 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagSupport.java
Index: TagSupport.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagSupport.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TagSupport.java 14 Jun 2002 06:53:02 -0000 1.11
+++ TagSupport.java 14 Jun 2002 10:24:14 -0000 1.12
@@ -178,7 +178,7 @@
}
/** Sets the context in which the tag will be run */
- public void setContext(JellyContext context) {
+ public void setContext(JellyContext context) throws Exception {
this.context = context;
}
1.7 +6 -6 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Tag.java
Index: Tag.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Tag.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Tag.java 17 May 2002 15:18:12 -0000 1.6
+++ Tag.java 14 Jun 2002 10:24:14 -0000 1.7
@@ -87,7 +87,7 @@
public JellyContext getContext();
/** Sets the context in which the tag will be run */
- public void setContext(JellyContext context);
+ public void setContext(JellyContext context) throws Exception;
/** Evaluates this tag after all the tags properties have been initialized.
*/
1.5 +7 -6 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/DynaTagScript.java
Index: DynaTagScript.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/DynaTagScript.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- DynaTagScript.java 17 May 2002 15:18:11 -0000 1.4
+++ DynaTagScript.java 14 Jun 2002 10:24:14 -0000 1.5
@@ -108,6 +108,8 @@
/** Evaluates the body of a tag */
public void run(JellyContext context, XMLOutput output) throws Exception {
+ tag.setContext(context);
+
DynaTag dynaTag = (DynaTag) tag;
// ### probably compiling this to 2 arrays might be quicker and smaller
@@ -118,7 +120,6 @@
Object value = expression.evaluate(context);
dynaTag.setAttribute(name, value);
}
- tag.setContext(context);
tag.doTag(output);
}
}
1.2 +1 -1 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/TaskSource.java
Index: TaskSource.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/TaskSource.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TaskSource.java 2 Jun 2002 17:29:02 -0000 1.1
+++ TaskSource.java 14 Jun 2002 10:24:14 -0000 1.2
@@ -74,7 +74,7 @@
/**
* @return the task object which may be an Ant Task.
*/
- public Object getTaskObject();
+ public Object getTaskObject() throws Exception;
/**
* @return a DynaBean wrapper around the Task object
1.6 +1 -1 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/TaskPropertyTag.java
Index: TaskPropertyTag.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/TaskPropertyTag.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TaskPropertyTag.java 12 Jun 2002 06:07:29 -0000 1.5
+++ TaskPropertyTag.java 14 Jun 2002 10:24:14 -0000 1.6
@@ -104,7 +104,7 @@
// CompilableTag interface
//-------------------------------------------------------------------------
public void compile() throws Exception {
- TaskTag tag = (TaskTag) findAncestorWithClass( Task.class );
+ TaskTag tag = (TaskTag) findAncestorWithClass( TaskTag.class );
if ( tag == null ) {
throw new JellyException( "You should only use Ant DataType tags within an Ant Task" );
}
1.8 +60 -22 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/TaskTag.java
Index: TaskTag.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/TaskTag.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- TaskTag.java 12 Jun 2002 17:09:41 -0000 1.7
+++ TaskTag.java 14 Jun 2002 10:24:14 -0000 1.8
@@ -61,6 +61,8 @@
*/
package org.apache.commons.jelly.tags.ant;
+import java.lang.reflect.Method;
+
import org.apache.commons.beanutils.ConvertingWrapDynaBean;
import org.apache.commons.beanutils.DynaBean;
@@ -81,46 +83,74 @@
*/
public class TaskTag extends DynaBeanTagSupport implements TaskSource {
+ /** argumment types */
+ private static final Class[] addTaskParamTypes = { String.class };
+
+ /** the type of the Ant task */
+ private Class taskType;
+
/** the Ant task */
private Task task;
+ /** the name of the Ant task */
+ private String taskName;
+
+ /** the current Ant project */
+ private Project project;
+
public TaskTag() {
}
- public TaskTag(Task task) {
- this.task = task;
- setDynaBean( new ConvertingWrapDynaBean(task) );
+ public TaskTag(Project project, Class taskType, String taskName) {
+ this.project = project;
+ this.taskType = taskType;
+ this.taskName = taskName;
+ }
+
+ /**
+ * Override this method as it is called before the tag is configured with its attributes.
+ * We will create a new task object and let the DynaBean configure it.
+ */
+ public void setContext(JellyContext context) throws Exception {
+ super.setContext(context);
+ task = null;
+
+ // force task to be recreated
+ getTask();
}
// Tag interface
//-------------------------------------------------------------------------
public void doTag(XMLOutput output) throws Exception {
-
- task.init();
+ Task task = getTask();
- if ( "echo".equals( task.getTaskName() ) )
- {
- Echo echoTask = (Echo) task;
-
- echoTask.addText( getBodyText() );
- echoTask.perform();
- echoTask.setMessage( "" );
- }
- else
- {
- // run the body first to configure the task via nested
- getBody().run(context, output);
- task.perform();
+ String text = getBodyText();
+
+ // if the task has an addText()
+ try {
+ Method method = taskType.getMethod("addText", addTaskParamTypes);
+ if (method != null) {
+ Object[] args = { text };
+ method.invoke(task, args);
+ }
+ }
+ catch (NoSuchMethodException e) {
+ // this is hardly an exceptional case unfortunately
+ // the JDK should just return null!
}
+
+ task.perform();
}
// TaskSource interface
//-------------------------------------------------------------------------
- public Object getTaskObject() {
- return task;
+ public Object getTaskObject() throws Exception {
+ return getTask();
}
-
+/*
+ This should not be required now...
+
public void setAttribute(String name,
Object value)
{
@@ -135,6 +165,7 @@
super.setAttribute( name,
newValue );
}
+*/
// Properties
//-------------------------------------------------------------------------
@@ -142,7 +173,14 @@
/**
* @return the Ant task
*/
- public Task getTask() {
+ public Task getTask() throws Exception {
+ if ( task == null ) {
+ task = (Task) taskType.newInstance();
+ task.setProject(project);
+ task.setTaskName(taskName);
+ task.init();
+ setDynaBean( new ConvertingWrapDynaBean(task) );
+ }
return task;
}
1.10 +1 -4 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/AntTagLibrary.java
Index: AntTagLibrary.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/AntTagLibrary.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- AntTagLibrary.java 13 Jun 2002 22:12:40 -0000 1.9
+++ AntTagLibrary.java 14 Jun 2002 10:24:14 -0000 1.10
@@ -169,10 +169,7 @@
// is it an Ant task?
Class type = (Class) project.getTaskDefinitions().get(name);
if ( type != null ) {
- Task task = (Task) type.newInstance();
- task.setProject(project);
- task.setTaskName(name);
- TaskTag tag = new TaskTag( task );
+ TaskTag tag = new TaskTag( project, type, name );
tag.setTrim( true );
return TagScript.newInstance(tag);
}
1.3 +4 -0 jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/ant/example_tasks.jelly
Index: example_tasks.jelly
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/ant/example_tasks.jelly,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- example_tasks.jelly 3 Jun 2002 04:31:55 -0000 1.2
+++ example_tasks.jelly 14 Jun 2002 10:24:14 -0000 1.3
@@ -5,6 +5,10 @@
<!-- this example attempts to invoke some Ant tasks -->
<echo message="Invoking the echo task from inside Jelly; the Maven repository is ${lib.repo}"/>
+ <echo>
+ Maven home is ${maven.home}
+ </echo>
+
<!-- lets try invoke a program -->
<java classname="org.apache.commons.jelly.Jelly" fork="yes">
<classpath refid="test.classpath"/>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>