org.apache.oro.text.regex
Class Perl5Substitution

java.lang.Object
  |
  +--org.apache.oro.text.regex.StringSubstitution
        |
        +--org.apache.oro.text.regex.Perl5Substitution
All Implemented Interfaces:
Substitution

public class Perl5Substitution
extends StringSubstitution

Untamed: Perl5Substitution implements a Substitution consisting of a literal string, but allowing Perl5 variable interpolation referencing saved groups in a match. This class is intended for use with Util.substitute.

The substitution string may contain variable interpolations referring to the saved parenthesized groups of the search pattern. A variable interpolation is denoted by $1, or $2, or $3, etc. If you don want such expressions to be interpreted literally, you should set the numInterpolations parameter to INTERPOLATE_NONE . It is easiest to explain what an interpolated variable does by giving an example:

A final thing to keep in mind is that if you use an interpolation variable that corresponds to a group not contained in the match, then it is interpreted literally. So given the regular expression from the example, and a substitution expression of a$2-, the result of the last sample input would be:

Tank a$2- 85  Tank a$2- 32  Tank a$2- 22
Also, $0 is always interpreted literally.

Version:
$Id: Perl5Substitution.java,v 1.5 2001/12/02 06:01:41 markm Exp $
Author:
Daniel F. Savarese
See Also:
Substitution, Util, Util.substitute(org.apache.oro.text.regex.PatternMatcher, org.apache.oro.text.regex.Pattern, org.apache.oro.text.regex.Substitution, java.lang.String, int), Substitution, StringSubstitution

Field Summary
(package private)  String _lastInterpolation
           
(package private)  int _numInterpolations
           
(package private)  Vector _substitutions
           
static int INTERPOLATE_ALL
          Enabled: A constant used when creating a Perl5Substitution indicating that interpolation variables should be computed relative to the most recent pattern match.
static int INTERPOLATE_NONE
          Enabled: A constant used when creating a Perl5Substitution indicating that interpolation variables should be interpreted literally, effectively disabling interpolation.
 
Fields inherited from class org.apache.oro.text.regex.StringSubstitution
_subLength, _substitution
 
Constructor Summary
Perl5Substitution()
          Enabled: Default constructor initializing substitution to a zero length String and the number of interpolations to INTERPOLATE_ALL.
Perl5Substitution(String substitution)
          Enabled: Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to INTERPOLATE_ALL.
Perl5Substitution(String substitution, int numInterpolations)
          Enabled: Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to the specified value.
 
Method Summary
(package private)  void _calcSub(StringBuffer buffer, MatchResult result)
           
(package private)  String _finalInterpolatedSub(MatchResult result)
           
(package private) static Vector _parseSubs(String sub)
           
 void appendSubstitution(StringBuffer appendBuffer, MatchResult match, int substitutionCount, String originalInput, PatternMatcher matcher, Pattern pattern)
          Enabled: Appends the substitution to a buffer containing the original input with substitutions applied for the pattern matches found so far.
 void setSubstitution(String substitution)
          Enabled: Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to INTERPOLATE_ALL.
 void setSubstitution(String substitution, int numInterpolations)
          Enabled: Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to the specified value.
 
Methods inherited from class org.apache.oro.text.regex.StringSubstitution
getSubstitution, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

INTERPOLATE_ALL

public static final int INTERPOLATE_ALL
Enabled: A constant used when creating a Perl5Substitution indicating that interpolation variables should be computed relative to the most recent pattern match.


INTERPOLATE_NONE

public static final int INTERPOLATE_NONE
Enabled: A constant used when creating a Perl5Substitution indicating that interpolation variables should be interpreted literally, effectively disabling interpolation.


_numInterpolations

int _numInterpolations

_substitutions

Vector _substitutions

_lastInterpolation

transient String _lastInterpolation
Constructor Detail

Perl5Substitution

public Perl5Substitution()
Enabled: Default constructor initializing substitution to a zero length String and the number of interpolations to INTERPOLATE_ALL.


Perl5Substitution

public Perl5Substitution(String substitution)
Enabled: Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to INTERPOLATE_ALL.

Parameters:
substitution - The string to use as a substitution.

Perl5Substitution

public Perl5Substitution(String substitution,
                         int numInterpolations)
Enabled: Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to the specified value.

Parameters:
substitution - The string to use as a substitution.
numInterpolations - If set to INTERPOLATE_NONE, interpolation variables are interpreted literally and not as references to the saved parenthesized groups of a pattern match. If set to INTERPOLATE_ALL , all variable interpolations are computed relative to the pattern match responsible for the current substitution. If set to a positive integer, the first numInterpolations substitutions have their variable interpolation performed relative to the most recent match, but the remaining substitutions have their variable interpolations performed relative to the numInterpolations 'th match.
Method Detail

_parseSubs

static Vector _parseSubs(String sub)

_finalInterpolatedSub

String _finalInterpolatedSub(MatchResult result)

_calcSub

void _calcSub(StringBuffer buffer,
              MatchResult result)

setSubstitution

public void setSubstitution(String substitution)
Enabled: Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to INTERPOLATE_ALL. You should use this method in order to avoid repeatedly allocating new Perl5Substitutions. It is recommended that you allocate a single Perl5Substitution and reuse it by using this method when appropriate.

Overrides:
setSubstitution in class StringSubstitution
Parameters:
substitution - The string to use as a substitution.

setSubstitution

public void setSubstitution(String substitution,
                            int numInterpolations)
Enabled: Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to the specified value. You should use this method in order to avoid repeatedly allocating new Perl5Substitutions. It is recommended that you allocate a single Perl5Substitution and reuse it by using this method when appropriate.

Parameters:
substitution - The string to use as a substitution.
numInterpolations - If set to INTERPOLATE_NONE, interpolation variables are interpreted literally and not as references to the saved parenthesized groups of a pattern match. If set to INTERPOLATE_ALL , all variable interpolations are computed relative to the pattern match responsible for the current substitution. If set to a positive integer, the first numInterpolations substitutions have their variable interpolation performed relative to the most recent match, but the remaining substitutions have their variable interpolations performed relative to the numInterpolations 'th match.

appendSubstitution

public void appendSubstitution(StringBuffer appendBuffer,
                               MatchResult match,
                               int substitutionCount,
                               String originalInput,
                               PatternMatcher matcher,
                               Pattern pattern)
Enabled: Appends the substitution to a buffer containing the original input with substitutions applied for the pattern matches found so far. See Substitution.appendSubstition() for more details regarding the expected behavior of this method.

Specified by:
appendSubstitution in interface Substitution
Overrides:
appendSubstitution in class StringSubstitution
Parameters:
appendBuffer - The buffer containing the new string resulting from performing substitutions on the original input.
match - The current match causing a substitution to be made.
substitutionCount - The number of substitutions that have been performed so far by Util.substitute.
originalInput - The original input upon which the substitutions are being performed.
matcher - The PatternMatcher used to find the current match.
pattern - The Pattern used to find the current match.


comments?