You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by do...@apache.org on 2002/05/24 05:58:37 UTC
cvs commit: jakarta-ant-myrmidon/framework/src/todo/org/apache/tools/todo/taskdefs Script.java
donaldp 02/05/23 20:58:37
Modified: framework/src/todo/org/apache/tools/todo/taskdefs
Script.java
Log:
Make this actually compile
Revision Changes Path
1.3 +86 -52 jakarta-ant-myrmidon/framework/src/todo/org/apache/tools/todo/taskdefs/Script.java
Index: Script.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/todo/org/apache/tools/todo/taskdefs/Script.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Script.java 23 May 2002 01:50:26 -0000 1.2
+++ Script.java 24 May 2002 03:58:37 -0000 1.3
@@ -14,58 +14,42 @@
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
+import java.util.Map;
import org.apache.myrmidon.api.AbstractTask;
import org.apache.myrmidon.api.TaskException;
+import org.apache.avalon.excalibur.io.IOUtil;
/**
* Execute a script
*
* @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
*/
-public class Script extends AbstractTask
+public class Script
+ extends AbstractTask
{
- private String script = "";
- private Hashtable beans = new Hashtable();
- private String language;
+ private final Hashtable m_beans = new Hashtable();
+ private String m_language;
+ private String m_script;
+ private File m_src;
/**
* Defines the language (required).
*
* @param language The new Language value
*/
- public void setLanguage( String language )
+ public void setLanguage( final String language )
{
- this.language = language;
+ m_language = language;
}
/**
* Load the script from an external file
*
- * @param fileName The new Src value
+ * @param src The new Src value
*/
- public void setSrc( String fileName )
+ public void setSrc( final File src )
{
- File file = new File( fileName );
- if( !file.exists() )
- {
- throw new TaskException( "file " + fileName + " not found." );
- }
-
- int count = (int)file.length();
- byte data[] = new byte[ count ];
-
- try
- {
- FileInputStream inStream = new FileInputStream( file );
- inStream.read( data );
- inStream.close();
- }
- catch( IOException e )
- {
- throw new TaskException( "Error", e );
- }
-
- script += new String( data );
+ m_src = src;
}
/**
@@ -73,45 +57,47 @@
*
* @param text The feature to be added to the Text attribute
*/
- public void addContent( String text )
+ public void addContent( final String text )
{
- this.script += text;
+ m_script = text;
}
- /**
- * Do the work.
- *
- * @throws org.apache.myrmidon.api.TaskException if someting goes wrong with the build
- */
public void execute()
throws TaskException
{
+ validate();
+
+ if( null != m_src )
+ {
+ loadScript();
+ }
+
try
{
addBeans( getContext().getProperties() );
//In Ant2 there is no difference between properties and references
//addBeans( getProject().getReferences() );
- beans.put( "context", getContext() );
+ m_beans.put( "context", getContext() );
+ m_beans.put( "self", this );
- beans.put( "self", this );
+ final BSFManager manager = new BSFManager();
- BSFManager manager = new BSFManager();
-
- for( Iterator e = beans.keys(); e.hasNext(); )
+ final Iterator iterator = m_beans.keySet().iterator();
+ while( iterator.hasNext() )
{
- String key = (String)e.next();
- Object value = beans.get( key );
+ final String key = (String)iterator.next();
+ final Object value = m_beans.get( key );
manager.declareBean( key, value, value.getClass() );
}
// execute the script
- manager.exec( language, "<ANT>", 0, 0, script );
+ manager.exec( m_language, "<ANT>", 0, 0, m_script );
}
catch( BSFException be )
{
Throwable t = be;
- Throwable te = be.getTargetException();
+ final Throwable te = be.getTargetException();
if( te != null )
{
if( te instanceof TaskException )
@@ -123,7 +109,53 @@
t = te;
}
}
- throw new TaskException( "Error", t );
+ throw new TaskException( t.getMessage(), t );
+ }
+ }
+
+ /**
+ * Load script from file.
+ *
+ * @throws TaskException
+ */
+ private void loadScript()
+ throws TaskException
+ {
+ FileInputStream input = null;
+ try
+ {
+ input = new FileInputStream( m_src );
+ m_script = IOUtil.toString( input );
+ }
+ catch( final IOException ioe )
+ {
+ throw new TaskException( ioe.getMessage(), ioe );
+ }
+ finally
+ {
+ IOUtil.shutdownStream( input );
+ }
+ }
+
+ /**
+ * Validate task parameters.
+ *
+ * @throws TaskException on error
+ */
+ private void validate()
+ throws TaskException
+ {
+ if( null != m_script && null != m_src )
+ {
+ final String message =
+ "Can not specify file and content for script.";
+ throw new TaskException( message );
+ }
+
+ if( !m_src.exists() )
+ {
+ final String message = "file " + m_src + " not found.";
+ throw new TaskException( message );
}
}
@@ -132,23 +164,25 @@
*
* @param dictionary The feature to be added to the Beans attribute
*/
- private void addBeans( Hashtable dictionary )
+ private void addBeans( final Map dictionary )
{
- for( Iterator e = dictionary.keys(); e.hasNext(); )
+ final Iterator iterator = dictionary.keySet().iterator();
+ while( iterator.hasNext() )
{
- String key = (String)e.next();
+ final String key = (String)iterator.next();
- boolean isValid = key.length() > 0 &&
+ final int size = key.length();
+ boolean isValid = size > 0 &&
Character.isJavaIdentifierStart( key.charAt( 0 ) );
- for( int i = 1; isValid && i < key.length(); i++ )
+ for( int i = 1; isValid && i < size; i++ )
{
isValid = Character.isJavaIdentifierPart( key.charAt( i ) );
}
if( isValid )
{
- beans.put( key, dictionary.get( key ) );
+ m_beans.put( key, dictionary.get( key ) );
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>