FileDocCategorySizeDatePackage
FilterSet.javaAPI DocApache Ant 1.7019054Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.types

FilterSet

public class FilterSet extends DataType implements Cloneable
A set of filters to be applied to something. A filter set may have begintoken and endtokens defined.

Fields Summary
public static final String
DEFAULT_TOKEN_START
The default token start string
public static final String
DEFAULT_TOKEN_END
The default token end string
private String
startOfToken
private String
endOfToken
private Vector
passedTokens
Contains a list of parsed tokens
private boolean
duplicateToken
if a duplicate token is found, this is set to true
private boolean
recurse
private Hashtable
filterHash
private Vector
filtersFiles
private OnMissing
onMissingFiltersFile
private boolean
readingFiles
private int
recurseDepth
private Vector
filters
List of ordered filters and filter files.
Constructors Summary
public FilterSet()
Default constructor.


           
      
    
protected FilterSet(FilterSet filterset)
Create a Filterset from another filterset.

param
filterset the filterset upon which this filterset will be based.

        super();
        this.filters = (Vector) filterset.getFilters().clone();
    
Methods Summary
public synchronized voidaddConfiguredFilterSet(org.apache.tools.ant.types.FilterSet filterSet)
Add a Filterset to this filter set.

param
filterSet the filterset to be added to this filterset

        if (isReference()) {
            throw noChildrenAllowed();
        }
        for (Enumeration e = filterSet.getFilters().elements(); e.hasMoreElements();) {
            addFilter((Filter) e.nextElement());
        }
    
public synchronized voidaddFilter(org.apache.tools.ant.types.FilterSet$Filter filter)
Add a new filter.

param
filter the filter to be added.

        if (isReference()) {
            throw noChildrenAllowed();
        }
        filters.addElement(filter);
        filterHash = null;
    
public synchronized voidaddFilter(java.lang.String token, java.lang.String value)
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.

        if (isReference()) {
            throw noChildrenAllowed();
        }
        addFilter(new Filter(token, value));
    
public synchronized java.lang.Objectclone()
Clone the filterset.

return
a deep clone of this filterset.
throws
BuildException if the clone cannot be performed.

        if (isReference()) {
            return ((FilterSet) getRef()).clone();
        }
        try {
            FilterSet fs = (FilterSet) super.clone();
            fs.filters = (Vector) getFilters().clone();
            fs.setProject(getProject());
            return fs;
        } catch (CloneNotSupportedException e) {
            throw new BuildException(e);
        }
    
public org.apache.tools.ant.types.FilterSet$FiltersFilecreateFiltersfile()
Create a new FiltersFile.

return
The filtersfile that was created.

        if (isReference()) {
            throw noChildrenAllowed();
        }
        return new FiltersFile();
    
public java.lang.StringgetBeginToken()
Get the begin token for this filterset.

return
the filter set's begin token for filtering.

        if (isReference()) {
            return getRef().getBeginToken();
        }
        return startOfToken;
    
public java.lang.StringgetEndToken()
Get the end token for this filterset.

return
the filter set's end token for replacement delimiting.

        if (isReference()) {
            return getRef().getEndToken();
        }
        return endOfToken;
    
public synchronized java.util.HashtablegetFilterHash()
Gets the filter hash of the FilterSet.

return
The hash of the tokens and values for quick lookup.

        if (filterHash == null) {
            filterHash = new Hashtable(getFilters().size());
            for (Enumeration e = getFilters().elements(); e.hasMoreElements();) {
               Filter filter = (Filter) e.nextElement();
               filterHash.put(filter.getToken(), filter.getValue());
            }
        }
        return filterHash;
    
protected synchronized java.util.VectorgetFilters()
Get the filters in the filter set.

return
a Vector of Filter instances.

        if (isReference()) {
            return getRef().getFilters();
        }
        //silly hack to avoid stack overflow...
        if (!readingFiles) {
            readingFiles = true;
            for (int i = 0, sz = filtersFiles.size(); i < sz; i++) {
                readFiltersFromFile((File) filtersFiles.get(i));
            }
            filtersFiles.clear();
            readingFiles = false;
        }
        return filters;
    
public org.apache.tools.ant.types.FilterSet$OnMissinggetOnMissingFiltersFile()
Get the onMissingFiltersFile setting.

return
the OnMissing instance.

        return onMissingFiltersFile;
    
protected org.apache.tools.ant.types.FilterSetgetRef()
Get the referenced filter set.

return
the filterset from the reference.

        return (FilterSet) getCheckedRef(FilterSet.class, "filterset");
    
private voidhandleMissingFile(java.lang.String message)

        switch (onMissingFiltersFile.getIndex()) {
        case OnMissing.IGNORE_INDEX:
            return;
        case OnMissing.FAIL_INDEX:
            throw new BuildException(message);
        case OnMissing.WARN_INDEX:
            log(message, Project.MSG_WARN);
            return;
        default:
            throw new BuildException("Invalid value for onMissingFiltersFile");
        }
    
public synchronized booleanhasFilters()
Test to see if this filter set has filters.

return
Return true if there are filters in this set.

        return getFilters().size() > 0;
    
private synchronized java.lang.StringiReplaceTokens(java.lang.String line)
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.

        String beginToken = getBeginToken();
        String endToken = getEndToken();
        int index = line.indexOf(beginToken);

        if (index > -1) {
            Hashtable tokens = getFilterHash();
            try {
                StringBuffer b = new StringBuffer();
                int i = 0;
                String token = null;
                String value = null;

                while (index > -1) {
                    //can't have zero-length token
                    int endIndex = line.indexOf(endToken,
                        index + beginToken.length() + 1);
                    if (endIndex == -1) {
                        break;
                    }
                    token
                        = line.substring(index + beginToken.length(), endIndex);
                    b.append(line.substring(i, index));
                    if (tokens.containsKey(token)) {
                        value = (String) tokens.get(token);
                        if (recurse && !value.equals(token)) {
                            // we have another token, let's parse it.
                            value = replaceTokens(value, token);
                        }
                        log("Replacing: " + beginToken + token + endToken
                            + " -> " + value, Project.MSG_VERBOSE);
                        b.append(value);
                        i = index + beginToken.length() + token.length()
                            + endToken.length();
                    } else {
                        // just append beginToken and search further
                        b.append(beginToken);
                        i = index + beginToken.length();
                    }
                    index = line.indexOf(beginToken, i);
                }

                b.append(line.substring(i));
                return b.toString();
            } catch (StringIndexOutOfBoundsException e) {
                return line;
            }
        } else {
           return line;
        }
    
public booleanisRecurse()
Get whether recursive token expansion is enabled.

return
boolean whether enabled.

        return recurse;
    
public synchronized voidreadFiltersFromFile(java.io.File filtersFile)
Read the filters from the given file.

param
filtersFile the file from which filters are read.
exception
BuildException when the file cannot be read.

        if (isReference()) {
            throw tooManyAttributes();
        }
        if (!filtersFile.exists()) {
           handleMissingFile("Could not read filters from file "
                                     + filtersFile + " as it doesn't exist.");
        }
        if (filtersFile.isFile()) {
           log("Reading filters from " + filtersFile, Project.MSG_VERBOSE);
           FileInputStream in = null;
           try {
              Properties props = new Properties();
              in = new FileInputStream(filtersFile);
              props.load(in);

              Enumeration e = props.propertyNames();
              Vector filts = getFilters();
              while (e.hasMoreElements()) {
                 String strPropName = (String) e.nextElement();
                 String strValue = props.getProperty(strPropName);
                 filts.addElement(new Filter(strPropName, strValue));
              }
           } catch (Exception ex) {
              throw new BuildException("Could not read filters from file: "
                  + filtersFile);
           } finally {
              FileUtils.close(in);
           }
        } else {
           handleMissingFile(
               "Must specify a file rather than a directory in "
               + "the filtersfile attribute:" + filtersFile);
        }
        filterHash = null;
    
public synchronized java.lang.StringreplaceTokens(java.lang.String line)
Does replacement on the given string with token matching. This uses the defined begintoken and endtoken values which default to @ for both. This resets the passedTokens and calls iReplaceTokens to do the actual replacements.

param
line The line in which to process embedded tokens.
return
The input string after token replacement.

        return iReplaceTokens(line);
    
private synchronized java.lang.StringreplaceTokens(java.lang.String line, java.lang.String parent)
This parses tokens which point to tokens. It also maintains a list of currently used tokens, so we cannot get into an infinite loop.

param
line the value / token to parse.
param
parent the parent token (= the token it was parsed from).

        String beginToken = getBeginToken();
        String endToken = getEndToken();
        if (recurseDepth == 0) {
            passedTokens = new Vector();
        }
        recurseDepth++;
        if (passedTokens.contains(parent) && !duplicateToken) {
            duplicateToken = true;
            System.out.println(
                "Infinite loop in tokens. Currently known tokens : "
                + passedTokens.toString() + "\nProblem token : " + beginToken
                + parent + endToken + " called from " + beginToken
                + passedTokens.lastElement().toString() + endToken);
            recurseDepth--;
            return parent;
        }
        passedTokens.addElement(parent);
        String value = iReplaceTokens(line);
        if (value.indexOf(beginToken) == -1 && !duplicateToken
                && recurseDepth == 1) {
            passedTokens = null;
        } else if (duplicateToken) {
            // should always be the case...
            if (passedTokens.size() > 0) {
                value = (String) passedTokens.remove(passedTokens.size() - 1);
                if (passedTokens.size() == 0) {
                    value = beginToken + value + endToken;
                    duplicateToken = false;
                }
            }
        }
        recurseDepth--;
        return value;
    
public voidsetBeginToken(java.lang.String startOfToken)
Set the string used to id the beginning of a token.

param
startOfToken The new Begintoken value.

        if (isReference()) {
            throw tooManyAttributes();
        }
        if (startOfToken == null || "".equals(startOfToken)) {
            throw new BuildException("beginToken must not be empty");
        }
        this.startOfToken = startOfToken;
    
public voidsetEndToken(java.lang.String endOfToken)
Set the string used to id the end of a token.

param
endOfToken The new Endtoken value.

        if (isReference()) {
            throw tooManyAttributes();
        }
        if (endOfToken == null || "".equals(endOfToken)) {
            throw new BuildException("endToken must not be empty");
        }
        this.endOfToken = endOfToken;
    
public voidsetFiltersfile(java.io.File filtersFile)
Set the file containing the filters for this filterset.

param
filtersFile sets the filter file from which to read filters for this filter set.
throws
BuildException if there is an error.

        if (isReference()) {
            throw tooManyAttributes();
        }
        filtersFiles.add(filtersFile);
    
public voidsetOnMissingFiltersFile(org.apache.tools.ant.types.FilterSet$OnMissing onMissingFiltersFile)
Set the behavior WRT missing filtersfiles.

param
onMissingFiltersFile the OnMissing describing the behavior.

        this.onMissingFiltersFile = onMissingFiltersFile;
    
public voidsetRecurse(boolean recurse)
Set whether recursive token expansion is enabled.

param
recurse boolean whether to recurse.

        this.recurse = recurse;