From 08cddc03458b32af8f6ac1d2d81befaf01f1eaa9 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Wed, 7 Feb 2001 05:37:15 +0000 Subject: [PATCH] Module for basic functionality useful in almost all domains git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268601 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/ant/modules/basic/AntCall.java | 87 +++++++++ .../org/apache/ant/modules/basic/Echo.java | 33 ++++ .../org/apache/ant/modules/basic/Pattern.java | 96 +++++++++ .../apache/ant/modules/basic/Property.java | 183 ++++++++++++++++++ .../modules/basic/StringToByteConverter.java | 32 +++ .../modules/basic/StringToClassConverter.java | 32 +++ .../basic/StringToDoubleConverter.java | 32 +++ .../modules/basic/StringToFileConverter.java | 35 ++++ .../modules/basic/StringToFloatConverter.java | 32 +++ .../basic/StringToIntegerConverter.java | 32 +++ .../modules/basic/StringToLongConverter.java | 32 +++ .../modules/basic/StringToShortConverter.java | 32 +++ .../modules/basic/StringToURLConverter.java | 33 ++++ 13 files changed, 691 insertions(+) create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/AntCall.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/Echo.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/Pattern.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/Property.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToByteConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToClassConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToDoubleConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFileConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFloatConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToIntegerConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToLongConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToShortConverter.java create mode 100644 proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToURLConverter.java diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/AntCall.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/AntCall.java new file mode 100644 index 000000000..f333737ab --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/AntCall.java @@ -0,0 +1,87 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import java.util.ArrayList; +import org.apache.ant.AntException; +import org.apache.ant.project.Project; +import org.apache.ant.project.ProjectEngine; +import org.apache.ant.tasklet.AbstractTasklet; +import org.apache.ant.tasklet.DefaultTaskletContext; +import org.apache.ant.tasklet.TaskletContext; +import org.apache.avalon.ComponentManager; +import org.apache.avalon.ComponentManagerException; +import org.apache.avalon.Composer; +import org.apache.avalon.Context; + +/** + * This is abstract base class for tasklets. + * + * @author Peter Donald + */ +public class AntCall + extends AbstractTasklet + implements Composer +{ + protected ProjectEngine m_projectEngine; + protected Project m_project; + protected String m_target; + protected ArrayList m_properties = new ArrayList(); + protected TaskletContext m_childContext; + protected ComponentManager m_componentManager; + + public void contextualize( final Context context ) + { + super.contextualize( context ); + m_childContext = new DefaultTaskletContext( getContext() ); + } + + public void compose( final ComponentManager componentManager ) + throws ComponentManagerException + { + m_componentManager = componentManager; + m_projectEngine = (ProjectEngine)componentManager. + lookup( "org.apache.ant.project.ProjectEngine" ); + m_project = (Project)componentManager.lookup( "org.apache.ant.project.Project" ); + } + + public void setTarget( final String target ) + { + m_target = target; + } + + public Property createParam() + throws Exception + { + final Property property = new Property(); + property.setLogger( getLogger() ); + property.contextualize( m_childContext ); + property.compose( m_componentManager ); + m_properties.add( property ); + return property; + } + + public void run() + throws AntException + { + if( null == m_target ) + { + throw new AntException( "Target attribute must be specified" ); + } + + final int size = m_properties.size(); + for( int i = 0; i < size; i++ ) + { + final Property property = (Property)m_properties.get( i ); + property.run(); + } + + getLogger().info( "Calling target " + m_target ); + m_projectEngine.execute( m_project, m_target, m_childContext ); + } +} diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Echo.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Echo.java new file mode 100644 index 000000000..f3c06c547 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Echo.java @@ -0,0 +1,33 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.AntException; +import org.apache.ant.tasklet.AbstractTasklet; + +/** + * This is the echo task to display a message. + * + * @author Peter Donald + */ +public class Echo + extends AbstractTasklet +{ + protected String m_message; + + public void setMessage( final String message ) + { + m_message = message; + } + + public void run() + throws AntException + { + getLogger().warn( m_message ); + } +} diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Pattern.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Pattern.java new file mode 100644 index 000000000..4368ea275 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Pattern.java @@ -0,0 +1,96 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.AntException; +import org.apache.ant.tasklet.DataType; +import org.apache.ant.util.Condition; + +/** + * Basic data type for holding patterns. + * + * @author Peter Donald + */ +public class Pattern + implements DataType +{ + protected String m_name; + protected Condition m_condition; + + /** + * Retrieve name (aka value) of pattern. + * + * @return the name/value of pattern + */ + public String getName() + { + return m_name; + } + + /** + * Get condition associated with pattern if any. + * + * @return the Condition + */ + public Condition getCondition() + { + return m_condition; + } + + /** + * Setter method for name/value of pattern. + * Conforms to ant setter patterns + * + * @param name the value + */ + public void setName( final String name ) + { + m_name = name; + } + + /** + * Set if clause on pattern. + * + * @param condition the condition + * @exception AntException if an error occurs + */ + public void setIf( final String condition ) + throws AntException + { + verifyConditionNull(); + m_condition = new Condition( true, condition ); + } + + /** + * Set unless clause of pattern. + * + * @param condition the unless clause + * @exception AntException if an error occurs + */ + public void setUnless( final String condition ) + throws AntException + { + verifyConditionNull(); + m_condition = new Condition( false, condition ); + } + + /** + * Utility method to make sure condition unset. + * Made so that it is not possible for both if and unless to be set. + * + * @exception AntException if an error occurs + */ + protected void verifyConditionNull() + throws AntException + { + if( null != m_condition ) + { + throw new AntException( "Can only set one of if/else for pattern data type" ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Property.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Property.java new file mode 100644 index 000000000..2be4e8b68 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/Property.java @@ -0,0 +1,183 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import java.util.Iterator; +import org.apache.ant.AntException; +import org.apache.ant.configuration.Configurable; +import org.apache.ant.configuration.Configuration; +import org.apache.ant.configuration.Configurer; +import org.apache.ant.convert.Converter; +import org.apache.ant.tasklet.DataType; +import org.apache.ant.tasklet.engine.DataTypeEngine; +import org.apache.ant.tasklet.AbstractTasklet; +import org.apache.ant.tasklet.TaskletContext; +import org.apache.ant.tasklet.engine.TaskletEngine; +import org.apache.avalon.ComponentManager; +import org.apache.avalon.ComponentManagerException; +import org.apache.avalon.Composer; +import org.apache.avalon.ConfigurationException; +import org.apache.avalon.Resolvable; + +/** + * This is the property "task" to declare a binding of a datatype to a name. + * + * @author Peter Donald + */ +public class Property + extends AbstractTasklet + implements Configurable, Composer +{ + protected String m_name; + protected Object m_value; + protected boolean m_localScope = true; + protected DataTypeEngine m_engine; + protected Converter m_converter; + protected Configurer m_configurer; + + public void compose( final ComponentManager componentManager ) + throws ComponentManagerException + { + m_configurer = (Configurer)componentManager. + lookup( "org.apache.ant.configuration.Configurer" ); + + m_engine = (DataTypeEngine)componentManager. + lookup( "org.apache.ant.tasklet.engine.DataTypeEngine" ); + + m_converter = (Converter)componentManager.lookup( "org.apache.ant.convert.Converter" ); + } + + public void configure( final Configuration configuration ) + throws ConfigurationException + { + final Iterator attributes = configuration.getAttributeNames(); + + while( attributes.hasNext() ) + { + final String name = (String)attributes.next(); + final String value = configuration.getAttribute( name ); + + final Object object = getContext().resolveValue( value ); + + if( null == object ) + { + throw new AntException( "Value for attribute " + name + "resolved to null" ); + } + + if( name.equals( "name" ) ) + { + try + { + final String convertedValue = + (String)m_converter.convert( String.class, object, getContext() ); + setName( convertedValue ); + } + catch( final Exception e ) + { + throw new ConfigurationException( "Error converting value", e ); + } + } + else if( name.equals( "value" ) ) + { + setValue( object ); + } + else if( name.equals( "local-scope" ) ) + { + try + { + final Boolean localScope = + (Boolean)m_converter.convert( Boolean.class, object, getContext() ); + setLocalScope( Boolean.TRUE == localScope ); + } + catch( final Exception e ) + { + throw new ConfigurationException( "Error converting value", e ); + } + } + else + { + throw new ConfigurationException( "Unknown attribute " + name ); + } + } + + final Iterator children = configuration.getChildren(); + while( children.hasNext() ) + { + final Configuration child = (Configuration)children.next(); + + try + { + final DataType value = m_engine.createDataType( child.getName() ); + setValue( value ); + m_configurer.configure( value, child, getContext() ); + } + catch( final Exception e ) + { + throw new ConfigurationException( "Unable to set datatype", e ); + } + } + } + + public void setName( final String name ) + { + m_name = name; + } + + public void setValue( final Object value ) + throws AntException + { + if( null != m_value ) + { + throw new AntException( "Value can not be set multiple times" ); + } + + m_value = value; + } + + public void setLocalScope( final boolean localScope ) + { + m_localScope = localScope; + } + + public void run() + throws AntException + { + if( null == m_name ) + { + throw new AntException( "Name must be specified" ); + } + + if( null == m_value ) + { + throw new AntException( "Value must be specified" ); + } + + final TaskletContext context = getContext(); + + Object value = m_value; + + if( value instanceof String ) + { + value = context.resolveValue( (String)value ); + } + + while( null != value && value instanceof Resolvable ) + { + value = ((Resolvable)value).resolve( context ); + } + + if( m_localScope ) + { + context.setProperty( m_name, value ); + } + else + { + context.setProperty( m_name, value, TaskletContext.PARENT ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToByteConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToByteConverter.java new file mode 100644 index 000000000..0db19847d --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToByteConverter.java @@ -0,0 +1,32 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to byte converter + * + * @author Peter Donald + */ +public class StringToByteConverter + extends AbstractConverter +{ + public StringToByteConverter() + { + super( String.class, Byte.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return new Byte( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToClassConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToClassConverter.java new file mode 100644 index 000000000..254f58734 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToClassConverter.java @@ -0,0 +1,32 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to class converter + * + * @author Peter Donald + */ +public class StringToClassConverter + extends AbstractConverter +{ + public StringToClassConverter() + { + super( String.class, Class.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return Class.forName( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToDoubleConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToDoubleConverter.java new file mode 100644 index 000000000..067353578 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToDoubleConverter.java @@ -0,0 +1,32 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to double converter + * + * @author Peter Donald + */ +public class StringToDoubleConverter + extends AbstractConverter +{ + public StringToDoubleConverter() + { + super( String.class, Double.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return new Double( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFileConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFileConverter.java new file mode 100644 index 000000000..24b1d8be0 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFileConverter.java @@ -0,0 +1,35 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import java.io.File; +import org.apache.ant.convert.AbstractConverter; +import org.apache.ant.tasklet.TaskletContext; +import org.apache.avalon.Context; + +/** + * String to file converter + * + * @author Peter Donald + */ +public class StringToFileConverter + extends AbstractConverter +{ + public StringToFileConverter() + { + super( String.class, File.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + final TaskletContext taskletContext = (TaskletContext)context; + return taskletContext.resolveFile( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFloatConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFloatConverter.java new file mode 100644 index 000000000..fab3e8b3a --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToFloatConverter.java @@ -0,0 +1,32 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to float converter + * + * @author Peter Donald + */ +public class StringToFloatConverter + extends AbstractConverter +{ + public StringToFloatConverter() + { + super( String.class, Float.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return new Float( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToIntegerConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToIntegerConverter.java new file mode 100644 index 000000000..d807666fd --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToIntegerConverter.java @@ -0,0 +1,32 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to integer converter. + * + * @author Peter Donald + */ +public class StringToIntegerConverter + extends AbstractConverter +{ + public StringToIntegerConverter() + { + super( String.class, Integer.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return new Integer( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToLongConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToLongConverter.java new file mode 100644 index 000000000..0b61fe330 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToLongConverter.java @@ -0,0 +1,32 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to long converter + * + * @author Peter Donald + */ +public class StringToLongConverter + extends AbstractConverter +{ + public StringToLongConverter() + { + super( String.class, Long.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return new Long( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToShortConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToShortConverter.java new file mode 100644 index 000000000..7919e648e --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToShortConverter.java @@ -0,0 +1,32 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to short converter + * + * @author Peter Donald + */ +public class StringToShortConverter + extends AbstractConverter +{ + public StringToShortConverter() + { + super( String.class, Short.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return new Short( (String)original ); + } +} + diff --git a/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToURLConverter.java b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToURLConverter.java new file mode 100644 index 000000000..9ec3faded --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/ant/modules/basic/StringToURLConverter.java @@ -0,0 +1,33 @@ +/* + * 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 file. + */ +package org.apache.ant.modules.basic; + +import java.net.URL; +import org.apache.ant.convert.AbstractConverter; +import org.apache.avalon.Context; + +/** + * String to url converter + * + * @author Peter Donald + */ +public class StringToURLConverter + extends AbstractConverter +{ + public StringToURLConverter() + { + super( String.class, URL.class ); + } + + public Object convert( final Object original, final Context context ) + throws Exception + { + return new URL( (String)original ); + } +} +