FileDocCategorySizeDatePackage
RegexpPatternMapper.javaAPI DocApache Ant 1.705010Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.util

RegexpPatternMapper

public class RegexpPatternMapper extends Object implements FileNameMapper
Implementation of FileNameMapper that does regular expression replacements.

Fields Summary
protected org.apache.tools.ant.util.regexp.RegexpMatcher
reg
protected char[]
to
protected StringBuffer
result
private boolean
handleDirSep
private int
regexpOptions
Constructors Summary
public RegexpPatternMapper()
Constructor for RegexpPatternMapper.

throws
BuildException on error.

    // CheckStyle:VisibilityModifier ON

                
        
        reg = (new RegexpMatcherFactory()).newRegexpMatcher();
    
Methods Summary
public java.lang.String[]mapFileName(java.lang.String sourceFileName)
Returns null if the source file name doesn't match the "from" pattern, an one-element array containing the translated file otherwise.

param
sourceFileName the source file name
return
a one-element array containing the translated file or null if the to pattern did not match

        if (handleDirSep) {
            if (sourceFileName.indexOf("\\") != -1) {
                sourceFileName = sourceFileName.replace('\\", '/");
            }
        }
        if (reg == null  || to == null
            || !reg.matches(sourceFileName, regexpOptions)) {
            return null;
        }
        return new String[] {replaceReferences(sourceFileName)};
    
protected java.lang.StringreplaceReferences(java.lang.String source)
Replace all backreferences in the to pattern with the matched groups of the source.

param
source the source file name.
return
the translated file name.

        Vector v = reg.getGroups(source, regexpOptions);

        result.setLength(0);
        for (int i = 0; i < to.length; i++) {
            if (to[i] == '\\") {
                if (++i < to.length) {
                    int value = Character.digit(to[i], 10);
                    if (value > -1) {
                        result.append((String) v.elementAt(value));
                    } else {
                        result.append(to[i]);
                    }
                } else {
                    // XXX - should throw an exception instead?
                    result.append('\\");
                }
            } else {
                result.append(to[i]);
            }
        }
        return result.substring(0);
    
public voidsetCaseSensitive(boolean caseSensitive)
Attribute specifing whether to ignore the case difference in the names.

param
caseSensitive a boolean, default is false.
since
Ant 1.6.3

        if (!caseSensitive) {
            regexpOptions = RegexpMatcher.MATCH_CASE_INSENSITIVE;
        } else {
            regexpOptions = 0;
        }
    
public voidsetFrom(java.lang.String from)
Sets the "from" pattern. Required.

param
from the from pattern.
throws
BuildException on error.

        try {
            reg.setPattern(from);
        } catch (NoClassDefFoundError e) {
            // depending on the implementation the actual RE won't
            // get instantiated in the constructor.
            throw new BuildException("Cannot load regular expression matcher",
                                     e);
        }
    
public voidsetHandleDirSep(boolean handleDirSep)
Attribute specifing whether to ignore the difference between / and \ (the two common directory characters).

param
handleDirSep a boolean, default is false.
since
Ant 1.6.3


                                   
        
        this.handleDirSep = handleDirSep;
    
public voidsetTo(java.lang.String to)
Sets the "to" pattern. Required.

param
to the to pattern.
throws
BuildException on error.

        this.to = to.toCharArray();