* Added LineFilter role, which represents a filter that handles lines of text. * Added TokenSet role, which represents a set of token -> value mappings. * Renamed FilterSetCollection -> LineFilterSet. This is now a compound LineFilter that takes a set of nested LineFilters. This is pretty much just a placeholder until the new filter streams are ported across from ant 1. * Renamed Filter -> SingletonTokenSet. This is a TokenSet that contains a single mapping. Moved to antlib.core. * Added PropertyTokenSet, a TokenSet that uses the project properties as its mappings. Added to antlib.core. * Split FilterSet into TokenLineFilter, which is a LineFilter that replaces tokens with their value, and FileTokenSet, which is a TokenSet loaded from a properties file. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271869 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -0,0 +1,81 @@ | |||
| /* | |||
| * 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 java.io.FileInputStream; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import java.util.Properties; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.avalon.excalibur.io.IOUtil; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.filters.TokenSet; | |||
| /** | |||
| * A set of tokens that are read from a file. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:type type="token-set" name="tokens-file" | |||
| */ | |||
| public class FileTokenSet | |||
| implements TokenSet | |||
| { | |||
| private final static Resources REZ | |||
| = ResourceManager.getPackageResources( FileTokenSet.class ); | |||
| private Map m_tokens = new HashMap(); | |||
| /** | |||
| * set the file containing the tokens for this tokenset. | |||
| */ | |||
| public void setFile( final File file ) | |||
| throws TaskException | |||
| { | |||
| // TODO - defer loading the tokens | |||
| if( !file.isFile() ) | |||
| { | |||
| final String message = REZ.getString( "filetokenset.not-a-file.error", file ); | |||
| throw new TaskException( message ); | |||
| } | |||
| try | |||
| { | |||
| FileInputStream instr = new FileInputStream( file ); | |||
| try | |||
| { | |||
| Properties props = new Properties(); | |||
| props.load( instr ); | |||
| m_tokens.putAll( props ); | |||
| } | |||
| finally | |||
| { | |||
| IOUtil.shutdownStream( instr ); | |||
| } | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| final String message = REZ.getString( "filetokenset.read-tokens.error", file ); | |||
| throw new TaskException( message, e ); | |||
| } | |||
| } | |||
| /** | |||
| * Evaluates the value for a token. | |||
| */ | |||
| public String getValue( String token, TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| return (String)m_tokens.get( token ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,38 @@ | |||
| /* | |||
| * 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 org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.filters.TokenSet; | |||
| /** | |||
| * A token set that uses the project's properties as tokens. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:type type="token-set" name="properties" | |||
| */ | |||
| public class PropertyTokenSet | |||
| implements TokenSet | |||
| { | |||
| /** | |||
| * Evaluates the value for a token. | |||
| */ | |||
| public String getValue( String token, TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final Object propValue = context.getProperty( token ); | |||
| if( propValue == null ) | |||
| { | |||
| return null; | |||
| } | |||
| return propValue.toString(); | |||
| } | |||
| } | |||
| @@ -33,4 +33,7 @@ trycatch.multiple-catches.error=Multiple <catch/> elements can not be placed ins | |||
| trycatch.missing-try-before-finally.error=There needs to be a <try/> element before <finally/> element. | |||
| trycatch.multiple-finallys.error=Multiple <finally/> elements can not be placed inside <try-catch/> task. | |||
| trycatch.no-try.error=Missing <try/> element from <try-catch/> task. | |||
| trycatch.missing-second.error=Missing <catch/> or <finally/> elements from <try-catch/> task. | |||
| trycatch.missing-second.error=Missing <catch/> or <finally/> elements from <try-catch/> task. | |||
| filetokenset.not-a-file.error=File {0} does not exist, or is not a file. | |||
| filetokenset.read-tokens.error=Could not read tokens from {0}. | |||
| @@ -5,15 +5,22 @@ | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.myrmidon.framework; | |||
| package org.apache.antlib.core; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.filters.TokenSet; | |||
| /** | |||
| * Individual filter component of filterset | |||
| * A single token and its value. | |||
| * | |||
| * @author Michael McCallum | |||
| * @created 14 March 2001 | |||
| * | |||
| * @ant:type type="token-set" name="token" | |||
| */ | |||
| public class Filter | |||
| public class SingletonTokenSet | |||
| implements TokenSet | |||
| { | |||
| /** | |||
| * Token which will be replaced in the filter operation | |||
| @@ -31,7 +38,7 @@ public class Filter | |||
| * @param token The token which will be replaced when filtering | |||
| * @param value The value which will replace the token when filtering | |||
| */ | |||
| public Filter( final String token, final String value ) | |||
| public SingletonTokenSet( final String token, final String value ) | |||
| { | |||
| m_token = token; | |||
| m_value = value; | |||
| @@ -40,7 +47,7 @@ public class Filter | |||
| /** | |||
| * No argument conmstructor | |||
| */ | |||
| public Filter() | |||
| public SingletonTokenSet() | |||
| { | |||
| } | |||
| @@ -61,18 +68,15 @@ public class Filter | |||
| } | |||
| /** | |||
| * Gets the Token attribute of the Filter object | |||
| */ | |||
| public String getToken() | |||
| { | |||
| return m_token; | |||
| } | |||
| /** | |||
| * Gets the Value attribute of the Filter object | |||
| * Evaluates the value for a token. | |||
| */ | |||
| public String getValue() | |||
| public String getValue( final String token, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| return m_value; | |||
| if( token.equals( m_token ) ) | |||
| { | |||
| return m_value; | |||
| } | |||
| return null; | |||
| } | |||
| } | |||
| @@ -20,11 +20,9 @@ import java.io.OutputStreamWriter; | |||
| import java.io.Reader; | |||
| import java.io.UnsupportedEncodingException; | |||
| import java.io.Writer; | |||
| import java.util.ArrayList; | |||
| import org.apache.avalon.excalibur.io.IOUtil; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.FilterSetCollection; | |||
| import org.apache.tools.ant.types.FilterSet; | |||
| import org.apache.myrmidon.framework.filters.LineFilterSet; | |||
| /** | |||
| * A task used to copy files and simultaneously apply a | |||
| @@ -37,13 +35,12 @@ import org.apache.tools.ant.types.FilterSet; | |||
| public class FilteredCopyTask | |||
| extends CopyTask | |||
| { | |||
| private ArrayList m_filterSets = new ArrayList(); | |||
| private FilterSetCollection m_filterSetCollection; | |||
| private LineFilterSet m_filterSetCollection = new LineFilterSet(); | |||
| private String m_encoding = "US-ASCII"; | |||
| public void addFilterSet( final FilterSet filterSet ) | |||
| public void addFilterset( final LineFilterSet filter ) | |||
| { | |||
| m_filterSets.add( filterSet ); | |||
| m_filterSetCollection.add( filter ); | |||
| } | |||
| public void setEncoding( final String encoding ) | |||
| @@ -121,31 +118,15 @@ public class FilteredCopyTask | |||
| private String replaceTokens( final String line ) | |||
| throws IOException | |||
| { | |||
| final FilterSetCollection filters = buildFilterSetCollection(); | |||
| try | |||
| { | |||
| return filters.replaceTokens( line ); | |||
| final StringBuffer buffer = new StringBuffer( line ); | |||
| m_filterSetCollection.filterLine( buffer, getContext() ); | |||
| return buffer.toString(); | |||
| } | |||
| catch( final TaskException te ) | |||
| { | |||
| throw new IOException( te.getMessage() ); | |||
| } | |||
| } | |||
| private FilterSetCollection buildFilterSetCollection() | |||
| { | |||
| if( null == m_filterSetCollection ) | |||
| { | |||
| m_filterSetCollection = new FilterSetCollection(); | |||
| final int size = m_filterSets.size(); | |||
| for( int i = 0; i < size; i++ ) | |||
| { | |||
| final FilterSet filterSet = (FilterSet)m_filterSets.get( i ); | |||
| m_filterSetCollection.addFilterSet( filterSet ); | |||
| } | |||
| } | |||
| return m_filterSetCollection; | |||
| } | |||
| } | |||
| @@ -1,52 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.util.ArrayList; | |||
| import java.util.Iterator; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.tools.ant.types.FilterSet; | |||
| /** | |||
| * A FilterSetCollection is a collection of filtersets each of which may have a | |||
| * different start/end token settings. | |||
| * | |||
| * @author <A href="mailto:conor@apache.org">Conor MacNeill</A> | |||
| */ | |||
| public class FilterSetCollection | |||
| { | |||
| private ArrayList m_filterSets = new ArrayList(); | |||
| public void addFilterSet( final FilterSet filterSet ) | |||
| { | |||
| m_filterSets.add( filterSet ); | |||
| } | |||
| /** | |||
| * Does replacement on the given string with token matching. This uses the | |||
| * defined begintoken and endtoken values which default to @ for both. | |||
| * | |||
| * @param line The line to process the tokens in. | |||
| * @return The string with the tokens replaced. | |||
| */ | |||
| public String replaceTokens( String line ) | |||
| throws TaskException | |||
| { | |||
| String replacedLine = line; | |||
| final Iterator filterSets = m_filterSets.iterator(); | |||
| while( filterSets.hasNext() ) | |||
| { | |||
| final FilterSet filterSet = (FilterSet)filterSets.next(); | |||
| replacedLine = filterSet.replaceTokens( replacedLine ); | |||
| } | |||
| return replacedLine; | |||
| } | |||
| } | |||
| @@ -0,0 +1,31 @@ | |||
| /* | |||
| * 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.myrmidon.framework.filters; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * Filters lines of text. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:role shorthand="line-filter" | |||
| */ | |||
| public interface LineFilter | |||
| { | |||
| /** | |||
| * Filters a line of text. | |||
| * | |||
| * @param line the text to filter. | |||
| * @param context the context to use when filtering. | |||
| */ | |||
| void filterLine( StringBuffer line, TaskContext context ) | |||
| throws TaskException; | |||
| } | |||
| @@ -0,0 +1,50 @@ | |||
| /* | |||
| * 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.myrmidon.framework.filters; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A collection of line filters. | |||
| * | |||
| * @author <A href="mailto:conor@apache.org">Conor MacNeill</A> | |||
| * | |||
| * @ant:data-type name="filterset" | |||
| * @ant:type type="line-filter" name="filterset" | |||
| */ | |||
| public class LineFilterSet | |||
| implements LineFilter | |||
| { | |||
| private ArrayList m_filterSets = new ArrayList(); | |||
| public void add( final LineFilter filter ) | |||
| { | |||
| m_filterSets.add( filter ); | |||
| } | |||
| /** | |||
| * Filters a line of text. | |||
| * | |||
| * @param line the text to filter. | |||
| * @param context the context to use when filtering. | |||
| */ | |||
| public void filterLine( final StringBuffer line, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final int count = m_filterSets.size(); | |||
| for( int i = 0; i < count; i++ ) | |||
| { | |||
| final LineFilter filter = (LineFilter)m_filterSets.get( i ); | |||
| filter.filterLine( line, context ); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,138 @@ | |||
| /* | |||
| * 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.myrmidon.framework.filters; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A line filter that replaces tokens. May have begintoken and endtokens defined. | |||
| * | |||
| * @author <A href="mailto:gholam@xtra.co.nz"> Michael McCallum </A> | |||
| * | |||
| * @ant:type type="line-filter" name="token-filter" | |||
| */ | |||
| public class TokenLineFilter | |||
| implements LineFilter | |||
| { | |||
| /** | |||
| * The default token start string | |||
| */ | |||
| private final static char[] DEFAULT_TOKEN_START = {'@'}; | |||
| /** | |||
| * The default token end string | |||
| */ | |||
| private final static char[] DEFAULT_TOKEN_END = {'@'}; | |||
| /** | |||
| * List of ordered filters and filter files. | |||
| */ | |||
| private ArrayList m_tokenSets = new ArrayList(); | |||
| /** | |||
| * Adds a TokenSet to this filter. | |||
| */ | |||
| public void add( final TokenSet tokens ) | |||
| { | |||
| m_tokenSets.add( tokens ); | |||
| } | |||
| /** | |||
| * Filters a line of text. | |||
| * | |||
| * @param line the text to filter. | |||
| * @param context the context to use when filtering. | |||
| */ | |||
| public void filterLine( final StringBuffer line, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| int index = 0; | |||
| while( index < line.length() ) | |||
| { | |||
| // Find the start of the next token | |||
| final int startToken = indexOf( line, DEFAULT_TOKEN_START, index ); | |||
| if( startToken == -1 ) | |||
| { | |||
| break; | |||
| } | |||
| final int startTokenName = startToken + DEFAULT_TOKEN_START.length; | |||
| // Find the end of the next token | |||
| int endTokenName = indexOf( line, DEFAULT_TOKEN_END, startTokenName ); | |||
| if( endTokenName == -1 ) | |||
| { | |||
| break; | |||
| } | |||
| int endToken = endTokenName + DEFAULT_TOKEN_END.length; | |||
| if( endTokenName == startTokenName ) | |||
| { | |||
| // Empty token name - skip | |||
| index = endToken; | |||
| continue; | |||
| } | |||
| // Extract token name figure out the value | |||
| final String token = line.substring( startTokenName, endTokenName ); | |||
| final String value = getTokenValue( token, context ); | |||
| if( value == null ) | |||
| { | |||
| // Unknown token - skip | |||
| index = endToken; | |||
| continue; | |||
| } | |||
| // Replace token with its value | |||
| line.delete( startToken, endToken ); | |||
| line.insert( startToken, value ); | |||
| index = startToken + value.length(); | |||
| } | |||
| } | |||
| /** | |||
| * Returns the value of a token. | |||
| */ | |||
| private String getTokenValue( final String token, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| String value = null; | |||
| final int count = m_tokenSets.size(); | |||
| for( int i = 0; value == null && i < count; i++ ) | |||
| { | |||
| final TokenSet tokenSet = (TokenSet)m_tokenSets.get( i ); | |||
| value = tokenSet.getValue( token, context ); | |||
| } | |||
| return value; | |||
| } | |||
| /** | |||
| * Returns the location of a string in a stringbuffer. | |||
| */ | |||
| private int indexOf( final StringBuffer buffer, | |||
| final char[] str, | |||
| final int index ) | |||
| { | |||
| final int maxIndex = buffer.length() - str.length + 1; | |||
| outer: for( int i = index; i < maxIndex; i++ ) | |||
| { | |||
| for( int j = 0; j < str.length; j++ ) | |||
| { | |||
| if( buffer.charAt( i + j ) != str[ j ] ) | |||
| { | |||
| continue outer; | |||
| } | |||
| } | |||
| return i; | |||
| } | |||
| return -1; | |||
| } | |||
| } | |||
| @@ -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.txt file. | |||
| */ | |||
| package org.apache.myrmidon.framework.filters; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A set of tokens. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:role shorthand="token-set" | |||
| */ | |||
| public interface TokenSet | |||
| { | |||
| /** | |||
| * Evaluates the value for a token. | |||
| * | |||
| * @param token the token to evaluate. | |||
| * @param context the context to use to evaluate the token. | |||
| * @return the value for the token, or null if the token is unknown. | |||
| */ | |||
| String getValue( String token, TaskContext context ) | |||
| throws TaskException; | |||
| } | |||
| @@ -1,83 +0,0 @@ | |||
| /* | |||
| * 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.tools.ant.taskdefs; | |||
| import java.io.File; | |||
| import org.apache.myrmidon.api.AbstractTask; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.tools.ant.types.FilterSet; | |||
| /** | |||
| * This task sets a token filter that is used by the file copy methods of the | |||
| * project to do token substitution, or sets mutiple tokens by reading these | |||
| * from a file. | |||
| * | |||
| * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org"> | |||
| * stefano@apache.org</a> | |||
| * @author Gero Vermaas <a href="mailto:gero@xs4all.nl">gero@xs4all.nl</a> | |||
| * @author <A href="gholam@xtra.co.nz">Michael McCallum</A> | |||
| */ | |||
| public class Filter | |||
| extends AbstractTask | |||
| { | |||
| private File filtersFile; | |||
| private String token; | |||
| private String value; | |||
| public void setFiltersfile( File filtersFile ) | |||
| { | |||
| this.filtersFile = filtersFile; | |||
| } | |||
| public void setToken( String token ) | |||
| { | |||
| this.token = token; | |||
| } | |||
| public void setValue( String value ) | |||
| { | |||
| this.value = value; | |||
| } | |||
| public void execute() | |||
| throws TaskException | |||
| { | |||
| boolean isFiltersFromFile = filtersFile != null && token == null && value == null; | |||
| boolean isSingleFilter = filtersFile == null && token != null && value != null; | |||
| if( !isFiltersFromFile && !isSingleFilter ) | |||
| { | |||
| throw new TaskException( "both token and value parameters, or only a filtersFile parameter is required" ); | |||
| } | |||
| if( isSingleFilter ) | |||
| { | |||
| getGlobalFilterSet().addFilter( token, value ); | |||
| } | |||
| if( isFiltersFromFile ) | |||
| { | |||
| readFilters(); | |||
| } | |||
| } | |||
| protected void readFilters() | |||
| throws TaskException | |||
| { | |||
| getContext().debug( "Reading filters from " + filtersFile ); | |||
| getGlobalFilterSet().readFiltersFromFile( filtersFile ); | |||
| } | |||
| private FilterSet getGlobalFilterSet() | |||
| { | |||
| //Get filterset from a well known propety and replace it there | |||
| return null; | |||
| } | |||
| } | |||
| @@ -1,231 +0,0 @@ | |||
| /* | |||
| * 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.tools.ant.types; | |||
| import java.io.File; | |||
| import java.io.FileInputStream; | |||
| import java.io.IOException; | |||
| import java.util.ArrayList; | |||
| import java.util.Enumeration; | |||
| import java.util.Hashtable; | |||
| import java.util.Iterator; | |||
| import java.util.Properties; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.Filter; | |||
| /** | |||
| * A set of filters to be applied to something. A filter set may have begintoken | |||
| * and endtokens defined. | |||
| * | |||
| * @author <A href="mailto:gholam@xtra.co.nz"> Michael McCallum </A> | |||
| */ | |||
| public class FilterSet | |||
| //extends AbstractLogEnabled | |||
| implements Cloneable | |||
| { | |||
| /** | |||
| * The default token start string | |||
| */ | |||
| private final static String DEFAULT_TOKEN_START = "@"; | |||
| /** | |||
| * The default token end string | |||
| */ | |||
| private final static String DEFAULT_TOKEN_END = "@"; | |||
| /** | |||
| * List of ordered filters and filter files. | |||
| */ | |||
| private ArrayList m_filters = new ArrayList(); | |||
| /** | |||
| * set the file containing the filters for this filterset. | |||
| * | |||
| * @param filtersFile sets the filter fil to read filters for this filter | |||
| * set from. | |||
| * @exception TaskException if there is a problem reading the filters | |||
| */ | |||
| public void setFiltersfile( File filtersFile ) | |||
| throws TaskException | |||
| { | |||
| readFiltersFromFile( filtersFile ); | |||
| } | |||
| /** | |||
| * Gets the filter hash of the FilterSet. | |||
| * | |||
| * @return The hash of the tokens and values for quick lookup. | |||
| */ | |||
| public Hashtable getFilterHash() | |||
| throws TaskException | |||
| { | |||
| final int filterSize = m_filters.size(); | |||
| final Hashtable filterHash = new Hashtable( filterSize ); | |||
| final Iterator e = m_filters.iterator(); | |||
| while( e.hasNext() ) | |||
| { | |||
| final Filter filter = (Filter)e.next(); | |||
| filterHash.put( filter.getToken(), filter.getValue() ); | |||
| } | |||
| return filterHash; | |||
| } | |||
| /** | |||
| * Create a new filter | |||
| * | |||
| * @param filter The feature to be added to the Filter attribute | |||
| */ | |||
| public void addFilter( final Filter filter ) | |||
| { | |||
| m_filters.add( filter ); | |||
| } | |||
| /** | |||
| * Add a new filter made from the given token and value. | |||
| * | |||
| * @param token The token for the new filter. | |||
| * @param value The value for the new filter. | |||
| */ | |||
| public void addFilter( final String token, final String value ) | |||
| { | |||
| m_filters.add( new Filter( token, value ) ); | |||
| } | |||
| /** | |||
| * Add a Filterset to this filter set | |||
| * | |||
| * @param filterSet the filterset to be added to this filterset | |||
| */ | |||
| public void addFilterSet( final FilterSet filterSet ) | |||
| { | |||
| final Iterator e = filterSet.m_filters.iterator(); | |||
| while( e.hasNext() ) | |||
| { | |||
| m_filters.add( (Filter)e.next() ); | |||
| } | |||
| } | |||
| /** | |||
| * Create a new FiltersFile | |||
| * | |||
| * @return The filter that was created. | |||
| */ | |||
| /*public FiltersFile createFiltersfile() | |||
| { | |||
| return new FiltersFile(); | |||
| }*/ | |||
| /** | |||
| * Read the filters from the given file. | |||
| * | |||
| * @param filtersFile the file from which filters are read | |||
| * @exception TaskException Throw a build exception when unable to read the | |||
| * file. | |||
| */ | |||
| public void readFiltersFromFile( File filtersFile ) | |||
| throws TaskException | |||
| { | |||
| if( filtersFile.isFile() ) | |||
| { | |||
| FileInputStream in = null; | |||
| try | |||
| { | |||
| Properties props = new Properties(); | |||
| in = new FileInputStream( filtersFile ); | |||
| props.load( in ); | |||
| Enumeration enum = props.propertyNames(); | |||
| while( enum.hasMoreElements() ) | |||
| { | |||
| String strPropName = (String)enum.nextElement(); | |||
| String strValue = props.getProperty( strPropName ); | |||
| m_filters.add( new Filter( strPropName, strValue ) ); | |||
| } | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| throw new TaskException( "Could not read filters from file: " + filtersFile ); | |||
| } | |||
| finally | |||
| { | |||
| if( in != null ) | |||
| { | |||
| try | |||
| { | |||
| in.close(); | |||
| } | |||
| catch( IOException ioex ) | |||
| { | |||
| } | |||
| } | |||
| } | |||
| } | |||
| else | |||
| { | |||
| throw new TaskException( "Must specify a file not a directory in the filtersfile attribute:" + filtersFile ); | |||
| } | |||
| } | |||
| /** | |||
| * Does replacement on the given string with token matching. This uses the | |||
| * defined begintoken and endtoken values which default to @ for both. | |||
| * | |||
| * @param line The line to process the tokens in. | |||
| * @return The string with the tokens replaced. | |||
| */ | |||
| public String replaceTokens( final String line ) | |||
| throws TaskException | |||
| { | |||
| int index = line.indexOf( DEFAULT_TOKEN_START ); | |||
| if( -1 == index ) | |||
| { | |||
| return line; | |||
| } | |||
| Hashtable tokens = getFilterHash(); | |||
| try | |||
| { | |||
| StringBuffer b = new StringBuffer(); | |||
| int i = 0; | |||
| String token = null; | |||
| String value = null; | |||
| do | |||
| { | |||
| int endIndex = line.indexOf( DEFAULT_TOKEN_END, index + DEFAULT_TOKEN_START.length() + 1 ); | |||
| if( endIndex == -1 ) | |||
| { | |||
| break; | |||
| } | |||
| token = line.substring( index + DEFAULT_TOKEN_START.length(), endIndex ); | |||
| b.append( line.substring( i, index ) ); | |||
| if( tokens.containsKey( token ) ) | |||
| { | |||
| value = (String)tokens.get( token ); | |||
| b.append( value ); | |||
| i = index + DEFAULT_TOKEN_START.length() + token.length() + DEFAULT_TOKEN_END.length(); | |||
| } | |||
| else | |||
| { | |||
| // just append beginToken and search further | |||
| b.append( DEFAULT_TOKEN_START ); | |||
| i = index + DEFAULT_TOKEN_START.length(); | |||
| } | |||
| } while( ( index = line.indexOf( DEFAULT_TOKEN_START, i ) ) > -1 ); | |||
| b.append( line.substring( i ) ); | |||
| return b.toString(); | |||
| } | |||
| catch( StringIndexOutOfBoundsException e ) | |||
| { | |||
| return line; | |||
| } | |||
| } | |||
| } | |||
| @@ -1,83 +0,0 @@ | |||
| /* | |||
| * 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.tools.ant.taskdefs; | |||
| import java.io.File; | |||
| import org.apache.myrmidon.api.AbstractTask; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.tools.ant.types.FilterSet; | |||
| /** | |||
| * This task sets a token filter that is used by the file copy methods of the | |||
| * project to do token substitution, or sets mutiple tokens by reading these | |||
| * from a file. | |||
| * | |||
| * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org"> | |||
| * stefano@apache.org</a> | |||
| * @author Gero Vermaas <a href="mailto:gero@xs4all.nl">gero@xs4all.nl</a> | |||
| * @author <A href="gholam@xtra.co.nz">Michael McCallum</A> | |||
| */ | |||
| public class Filter | |||
| extends AbstractTask | |||
| { | |||
| private File filtersFile; | |||
| private String token; | |||
| private String value; | |||
| public void setFiltersfile( File filtersFile ) | |||
| { | |||
| this.filtersFile = filtersFile; | |||
| } | |||
| public void setToken( String token ) | |||
| { | |||
| this.token = token; | |||
| } | |||
| public void setValue( String value ) | |||
| { | |||
| this.value = value; | |||
| } | |||
| public void execute() | |||
| throws TaskException | |||
| { | |||
| boolean isFiltersFromFile = filtersFile != null && token == null && value == null; | |||
| boolean isSingleFilter = filtersFile == null && token != null && value != null; | |||
| if( !isFiltersFromFile && !isSingleFilter ) | |||
| { | |||
| throw new TaskException( "both token and value parameters, or only a filtersFile parameter is required" ); | |||
| } | |||
| if( isSingleFilter ) | |||
| { | |||
| getGlobalFilterSet().addFilter( token, value ); | |||
| } | |||
| if( isFiltersFromFile ) | |||
| { | |||
| readFilters(); | |||
| } | |||
| } | |||
| protected void readFilters() | |||
| throws TaskException | |||
| { | |||
| getContext().debug( "Reading filters from " + filtersFile ); | |||
| getGlobalFilterSet().readFiltersFromFile( filtersFile ); | |||
| } | |||
| private FilterSet getGlobalFilterSet() | |||
| { | |||
| //Get filterset from a well known propety and replace it there | |||
| return null; | |||
| } | |||
| } | |||
| @@ -1,231 +0,0 @@ | |||
| /* | |||
| * 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.tools.ant.types; | |||
| import java.io.File; | |||
| import java.io.FileInputStream; | |||
| import java.io.IOException; | |||
| import java.util.ArrayList; | |||
| import java.util.Enumeration; | |||
| import java.util.Hashtable; | |||
| import java.util.Iterator; | |||
| import java.util.Properties; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.Filter; | |||
| /** | |||
| * A set of filters to be applied to something. A filter set may have begintoken | |||
| * and endtokens defined. | |||
| * | |||
| * @author <A href="mailto:gholam@xtra.co.nz"> Michael McCallum </A> | |||
| */ | |||
| public class FilterSet | |||
| //extends AbstractLogEnabled | |||
| implements Cloneable | |||
| { | |||
| /** | |||
| * The default token start string | |||
| */ | |||
| private final static String DEFAULT_TOKEN_START = "@"; | |||
| /** | |||
| * The default token end string | |||
| */ | |||
| private final static String DEFAULT_TOKEN_END = "@"; | |||
| /** | |||
| * List of ordered filters and filter files. | |||
| */ | |||
| private ArrayList m_filters = new ArrayList(); | |||
| /** | |||
| * set the file containing the filters for this filterset. | |||
| * | |||
| * @param filtersFile sets the filter fil to read filters for this filter | |||
| * set from. | |||
| * @exception TaskException if there is a problem reading the filters | |||
| */ | |||
| public void setFiltersfile( File filtersFile ) | |||
| throws TaskException | |||
| { | |||
| readFiltersFromFile( filtersFile ); | |||
| } | |||
| /** | |||
| * Gets the filter hash of the FilterSet. | |||
| * | |||
| * @return The hash of the tokens and values for quick lookup. | |||
| */ | |||
| public Hashtable getFilterHash() | |||
| throws TaskException | |||
| { | |||
| final int filterSize = m_filters.size(); | |||
| final Hashtable filterHash = new Hashtable( filterSize ); | |||
| final Iterator e = m_filters.iterator(); | |||
| while( e.hasNext() ) | |||
| { | |||
| final Filter filter = (Filter)e.next(); | |||
| filterHash.put( filter.getToken(), filter.getValue() ); | |||
| } | |||
| return filterHash; | |||
| } | |||
| /** | |||
| * Create a new filter | |||
| * | |||
| * @param filter The feature to be added to the Filter attribute | |||
| */ | |||
| public void addFilter( final Filter filter ) | |||
| { | |||
| m_filters.add( filter ); | |||
| } | |||
| /** | |||
| * Add a new filter made from the given token and value. | |||
| * | |||
| * @param token The token for the new filter. | |||
| * @param value The value for the new filter. | |||
| */ | |||
| public void addFilter( final String token, final String value ) | |||
| { | |||
| m_filters.add( new Filter( token, value ) ); | |||
| } | |||
| /** | |||
| * Add a Filterset to this filter set | |||
| * | |||
| * @param filterSet the filterset to be added to this filterset | |||
| */ | |||
| public void addFilterSet( final FilterSet filterSet ) | |||
| { | |||
| final Iterator e = filterSet.m_filters.iterator(); | |||
| while( e.hasNext() ) | |||
| { | |||
| m_filters.add( (Filter)e.next() ); | |||
| } | |||
| } | |||
| /** | |||
| * Create a new FiltersFile | |||
| * | |||
| * @return The filter that was created. | |||
| */ | |||
| /*public FiltersFile createFiltersfile() | |||
| { | |||
| return new FiltersFile(); | |||
| }*/ | |||
| /** | |||
| * Read the filters from the given file. | |||
| * | |||
| * @param filtersFile the file from which filters are read | |||
| * @exception TaskException Throw a build exception when unable to read the | |||
| * file. | |||
| */ | |||
| public void readFiltersFromFile( File filtersFile ) | |||
| throws TaskException | |||
| { | |||
| if( filtersFile.isFile() ) | |||
| { | |||
| FileInputStream in = null; | |||
| try | |||
| { | |||
| Properties props = new Properties(); | |||
| in = new FileInputStream( filtersFile ); | |||
| props.load( in ); | |||
| Enumeration enum = props.propertyNames(); | |||
| while( enum.hasMoreElements() ) | |||
| { | |||
| String strPropName = (String)enum.nextElement(); | |||
| String strValue = props.getProperty( strPropName ); | |||
| m_filters.add( new Filter( strPropName, strValue ) ); | |||
| } | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| throw new TaskException( "Could not read filters from file: " + filtersFile ); | |||
| } | |||
| finally | |||
| { | |||
| if( in != null ) | |||
| { | |||
| try | |||
| { | |||
| in.close(); | |||
| } | |||
| catch( IOException ioex ) | |||
| { | |||
| } | |||
| } | |||
| } | |||
| } | |||
| else | |||
| { | |||
| throw new TaskException( "Must specify a file not a directory in the filtersfile attribute:" + filtersFile ); | |||
| } | |||
| } | |||
| /** | |||
| * Does replacement on the given string with token matching. This uses the | |||
| * defined begintoken and endtoken values which default to @ for both. | |||
| * | |||
| * @param line The line to process the tokens in. | |||
| * @return The string with the tokens replaced. | |||
| */ | |||
| public String replaceTokens( final String line ) | |||
| throws TaskException | |||
| { | |||
| int index = line.indexOf( DEFAULT_TOKEN_START ); | |||
| if( -1 == index ) | |||
| { | |||
| return line; | |||
| } | |||
| Hashtable tokens = getFilterHash(); | |||
| try | |||
| { | |||
| StringBuffer b = new StringBuffer(); | |||
| int i = 0; | |||
| String token = null; | |||
| String value = null; | |||
| do | |||
| { | |||
| int endIndex = line.indexOf( DEFAULT_TOKEN_END, index + DEFAULT_TOKEN_START.length() + 1 ); | |||
| if( endIndex == -1 ) | |||
| { | |||
| break; | |||
| } | |||
| token = line.substring( index + DEFAULT_TOKEN_START.length(), endIndex ); | |||
| b.append( line.substring( i, index ) ); | |||
| if( tokens.containsKey( token ) ) | |||
| { | |||
| value = (String)tokens.get( token ); | |||
| b.append( value ); | |||
| i = index + DEFAULT_TOKEN_START.length() + token.length() + DEFAULT_TOKEN_END.length(); | |||
| } | |||
| else | |||
| { | |||
| // just append beginToken and search further | |||
| b.append( DEFAULT_TOKEN_START ); | |||
| i = index + DEFAULT_TOKEN_START.length(); | |||
| } | |||
| } while( ( index = line.indexOf( DEFAULT_TOKEN_START, i ) ) > -1 ); | |||
| b.append( line.substring( i ) ); | |||
| return b.toString(); | |||
| } | |||
| catch( StringIndexOutOfBoundsException e ) | |||
| { | |||
| return line; | |||
| } | |||
| } | |||
| } | |||