adammurdoch 2002/06/25 19:12:36
Modified: antlib/src/java/org/apache/antlib/core Resources.properties
Added: antlib/src/java/org/apache/antlib/core IncludeTask.java
LoadDOMTask.java
antlib/src/test/org/apache/antlib/core/test
IncludeTaskTestCase.java LoadDOMTaskTestCase.java
include-log.ant include-props.ant include.ant
load-dom.ant test.xml
Log:
- Added <include> task, which loads and executes the contents of an XML file.
Execution happens in the same scope as the <include> task.
- Added <load-dom> task, which loads the DOM of an XML file into a property,
where it can be slurped over using XPath.
- Added test cases.
Revision Changes Path
1.7 +6 -2
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/core/Resources.properties
Index: Resources.properties
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/core/Resources.properties,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Resources.properties 6 Jun 2002 10:57:03 -0000 1.6
+++ Resources.properties 26 Jun 2002 02:12:36 -0000 1.7
@@ -33,5 +33,9 @@
load-resource.loading.notice=Loading properties from resource "{0}".
load-resource.missing-resource.notice=Unable to find resource "{0}".
-for-each.no-propertry.error=No property name specified.
-for-each.no-list.error=No values specified.
\ No newline at end of file
+load-dom.no-file.error=No source file specified.
+load-dom.no-property.error=No property name specified.
+load-dom.load-file.error=Could not load file "{0}".
+
+include.no-file.error=No source file specified.
+include.load-file.error=Could not load file "{0}".
\ No newline at end of file
1.1
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/core/IncludeTask.java
Index: IncludeTask.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.core;
import java.io.File;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.api.metadata.ModelElement;
import org.apache.myrmidon.framework.AbstractContainerTask;
import org.apache.myrmidon.interfaces.builder.ModelBuilder;
/**
* A task that loads and executes an XML fragment.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/26 02:12:36 $
*
* @ant.task name="include"
*/
public class IncludeTask
extends AbstractContainerTask
{
private static final Resources REZ =
ResourceManager.getPackageResources( IncludeTask.class );
private File m_file;
/**
* The file to load the XML fragment from.
* @param file
*/
public void setFile( final File file )
{
m_file = file;
}
/**
* Executes the task.
*/
public void execute()
throws TaskException
{
if( m_file == null )
{
final String message = REZ.getString( "include.no-file.error" );
throw new TaskException( message );
}
// Load the model
final ModelElement model;
try
{
final ModelBuilder builder = (ModelBuilder)getService(
ModelBuilder.class );
model = builder.build( m_file.getAbsolutePath() );
}
catch( final Exception e )
{
final String message = REZ.getString( "include.load-file.error",
m_file );
throw new TaskException( message, e );
}
// Execute the children
executeTasks( model.getChildren() );
}
}
1.1
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/core/LoadDOMTask.java
Index: LoadDOMTask.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.core;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.myrmidon.api.AbstractTask;
import org.apache.myrmidon.api.TaskException;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.w3c.dom.Document;
/**
* Loads an XML DOM from a file, and sets it as a property.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/26 02:12:36 $
*
* @ant.task name="load-dom"
*
* @todo - Convert this task into a data-type, instead.
*/
public class LoadDOMTask
extends AbstractTask
{
private static final Resources REZ =
ResourceManager.getPackageResources( LoadDOMTask.class );
private File m_file;
private String m_property;
/**
* The file to load the XML DOM from.
*/
public void setFile( final File file )
{
m_file = file;
}
/**
* The name of the property to set.
*/
public void setProperty( final String property )
{
m_property = property;
}
/**
* Executes this task.
*/
public void execute()
throws TaskException
{
if( m_file == null )
{
final String message = REZ.getString( "load-dom.no-file.error" );
throw new TaskException( message );
}
if( m_property == null )
{
final String message = REZ.getString(
"load-dom.no-property.error" );
throw new TaskException( message );
}
try
{
final Document dom =
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( m_file );
getContext().setProperty( m_property, dom.getDocumentElement() );
}
catch( final Exception e )
{
final String message = REZ.getString( "load-dom.load-file.error",
m_file );
throw new TaskException( message, e );
}
}
}
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/IncludeTaskTestCase.java
Index: IncludeTaskTestCase.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.core.test;
import org.apache.antlib.AbstractProjectTestCase;
import java.io.File;
/**
* Test cases for the <include> task.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/26 02:12:36 $
*/
public class IncludeTaskTestCase
extends AbstractProjectTestCase
{
public IncludeTaskTestCase( final String name )
{
super( name );
}
public void testExecution() throws Exception
{
final File projectFile = getTestResource( "include.ant" );
executeTarget( projectFile, "include-log" );
executeTarget( projectFile, "include-use-props" );
executeTarget( projectFile, "no-file" );
executeTarget( projectFile, "unknown-file" );
}
}
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/LoadDOMTaskTestCase.java
Index: LoadDOMTaskTestCase.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.core.test;
import org.apache.antlib.AbstractProjectTestCase;
import java.io.File;
/**
* Test cases for the <load-dom> task.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/26 02:12:36 $
*/
public class LoadDOMTaskTestCase
extends AbstractProjectTestCase
{
public LoadDOMTaskTestCase( final String name )
{
super( name );
}
public void testExecute() throws Exception
{
final File projectFile = getTestResource( "load-dom.ant" );
executeTarget( projectFile, "load-file" );
executeTarget( projectFile, "no-propname" );
executeTarget( projectFile, "no-file" );
executeTarget( projectFile, "unknown-file" );
}
}
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/include-log.ant
Index: include-log.ant
===================================================================
<tasks>
<log>Included task 1</log>
<log>Included task 2</log>
</tasks>
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/include-props.ant
Index: include-props.ant
===================================================================
<tasks>
<assert><equals arg1="${p}" arg2="original value"/></assert>
<property name="p">new value</property>
</tasks>
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/include.ant
Index: include.ant
===================================================================
<project version="2.0">
<target name="include-log">
<expect-output>
<out>
<include>
<log>Included task 1</log>
<log>Included task 2</log>
</include>
</out>
<tasks>
<include file="include-log.ant"/>
</tasks>
</expect-output>
</target>
<!-- Test that included tasks can get/set properties in current scope -->
<target name="include-use-props">
<property name="p">original value</property>
<include file="include-props.ant"/>
<assert><equals arg1="${p}" arg2="new value"/></assert>
</target>
<!-- Validation tests -->
<target name="no-file">
<expect-error>
<exc>No source file specified.</exc>
<tasks>
<include/>
</tasks>
</expect-error>
</target>
<target name="unknown-file">
<expect-error>
<exc>Could not load file
"${myrmidon.project/baseDirectory}${file.separator}no-such-file".</exc>
<tasks>
<include file="no-such-file"/>
</tasks>
</expect-error>
</target>
</project>
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/load-dom.ant
Index: load-dom.ant
===================================================================
<project version="2.0">
<!-- Load a DOM and try out some XPath expressions on it -->
<target name="load-file">
<load-dom property="dom" file="test.xml"/>
<assert>
<equals arg1="${dom/@attr}" arg2="root"/>
<equals arg1="${dom/elem[1]/@attr}" arg2="elem 1"/>
<equals arg1="${dom/[EMAIL PROTECTED]'elem 2']}" arg2="some
text"/>
</assert>
</target>
<!-- Validation tests -->
<target name="no-propname">
<expect-error>
<exc>No property name specified.</exc>
<tasks>
<load-dom file="test.xml"/>
</tasks>
</expect-error>
</target>
<target name="no-file">
<expect-error>
<exc>No source file specified.</exc>
<tasks>
<load-dom property="prop"/>
</tasks>
</expect-error>
</target>
<target name="unknown-file">
<expect-error>
<exc>Could not load file
"${myrmidon.project/baseDirectory}${file.separator}no-such-file".</exc>
<tasks>
<load-dom property="prop" file="no-such-file"/>
</tasks>
</expect-error>
</target>
</project>
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/test.xml
Index: test.xml
===================================================================
<root attr="root">
<elem attr="elem 1"/>
<elem attr="elem 2">some text</elem>
<elem attr="elem 3"/>
</root>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>