git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268601 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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" ); | |||
| } | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||
| @@ -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 <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||
| */ | |||
| 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 ); | |||
| } | |||
| } | |||