Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.oro.text.regex
Class Perl5Substitution  view Perl5Substitution download Perl5Substitution.java

java.lang.Object
  extended byorg.apache.oro.text.regex.StringSubstitution
      extended byorg.apache.oro.text.regex.Perl5Substitution
All Implemented Interfaces:
Substitution

public class Perl5Substitution
extends StringSubstitution

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 55 .

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 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 as the empty string. 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- 85  Tank a- 32  Tank a- 22
The special substitution $& will interpolate the entire portion of the input matched by the regular expression. $0 will do the same, but it is recommended that it be avoided because the latest versions of Perl use $0 to store the program name rather than duplicate the behavior of $&. Also, the result of substituting $ followed by a non-positive integer is undefined. In order to include a $ in a substitution, it should be escaped with a backslash (e.g., "\\$0").

Perl5 double-quoted string case modification is also supported in the substitution. The following escape sequences are supported:

\\U
make substitution uppercase until end of substitution or \\E
\\u
make next character uppercase
\\L
make substitution uppercase until end of substitution or \\E
\\l
make next character uppercase
\\E
mark the end of the case modification
The double backslashes are shown to remind you that to make a backslash get past Java's string handling and appear as a backslash to the substitution, you must escape the backslash.

Since:
1.1
Version:
@version@

Field Summary
private static int __MAX_GROUPS
          The maximum number of groups supported by interpolation.
private static int __OPCODE_STORAGE_SIZE
          The initial size and unit of growth for the _subOpCodes array.
(package private)  java.lang.String _lastInterpolation
           
(package private)  int _numInterpolations
           
(package private) static int _OPCODE_COPY
          A constant declaring opcode for copy operation.
(package private) static int _OPCODE_ENDCASE_MODE
          A constant declaring opcode for lowercase mode operation.
(package private) static int _OPCODE_LOWERCASE_CHAR
          A constant declaring opcode for lowercase char operation.
(package private) static int _OPCODE_LOWERCASE_MODE
          A constant declaring opcode for lowercase mode operation.
(package private) static int _OPCODE_UPPERCASE_CHAR
          A constant declaring opcode for uppercase char operation.
(package private) static int _OPCODE_UPPERCASE_MODE
          A constant declaring opcode for lowercase mode operation.
(package private)  int[] _subOpcodes
           
(package private)  int _subOpcodesCount
           
(package private)  char[] _substitutionChars
           
static int INTERPOLATE_ALL
          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
          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()
          Default constructor initializing substitution to a zero length String and the number of interpolations to INTERPOLATE_ALL 55 .
Perl5Substitution(java.lang.String substitution)
          Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to INTERPOLATE_ALL 55 .
Perl5Substitution(java.lang.String substitution, int numInterpolations)
          Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to the specified value.
 
Method Summary
private  void __addElement(int value)
           
private static boolean __isInterpolationCharacter(char ch)
           
private  void __parseSubs(java.lang.String sub)
           
(package private)  void _calcSub(java.lang.StringBuffer buffer, MatchResult result)
           
(package private)  java.lang.String _finalInterpolatedSub(MatchResult result)
           
 void appendSubstitution(java.lang.StringBuffer appendBuffer, MatchResult match, int substitutionCount, PatternMatcherInput originalInput, PatternMatcher matcher, Pattern pattern)
          Appends the substitution to a buffer containing the original input with substitutions applied for the pattern matches found so far.
 void setSubstitution(java.lang.String substitution)
          Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to INTERPOLATE_ALL 55 .
 void setSubstitution(java.lang.String substitution, int numInterpolations)
          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
A constant used when creating a Perl5Substitution indicating that interpolation variables should be computed relative to the most recent pattern match.

See Also:
Constant Field Values

INTERPOLATE_NONE

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

See Also:
Constant Field Values

__OPCODE_STORAGE_SIZE

private static final int __OPCODE_STORAGE_SIZE
The initial size and unit of growth for the _subOpCodes array.

See Also:
Constant Field Values

__MAX_GROUPS

private static final int __MAX_GROUPS
The maximum number of groups supported by interpolation.

See Also:
Constant Field Values

_OPCODE_COPY

static final int _OPCODE_COPY
A constant declaring opcode for copy operation.

See Also:
Constant Field Values

_OPCODE_LOWERCASE_CHAR

static final int _OPCODE_LOWERCASE_CHAR
A constant declaring opcode for lowercase char operation.

See Also:
Constant Field Values

_OPCODE_UPPERCASE_CHAR

static final int _OPCODE_UPPERCASE_CHAR
A constant declaring opcode for uppercase char operation.

See Also:
Constant Field Values

_OPCODE_LOWERCASE_MODE

static final int _OPCODE_LOWERCASE_MODE
A constant declaring opcode for lowercase mode operation.

See Also:
Constant Field Values

_OPCODE_UPPERCASE_MODE

static final int _OPCODE_UPPERCASE_MODE
A constant declaring opcode for lowercase mode operation.

See Also:
Constant Field Values

_OPCODE_ENDCASE_MODE

static final int _OPCODE_ENDCASE_MODE
A constant declaring opcode for lowercase mode operation.

See Also:
Constant Field Values

_numInterpolations

int _numInterpolations

_subOpcodes

int[] _subOpcodes

_subOpcodesCount

int _subOpcodesCount

_substitutionChars

char[] _substitutionChars

_lastInterpolation

transient java.lang.String _lastInterpolation
Constructor Detail

Perl5Substitution

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


Perl5Substitution

public Perl5Substitution(java.lang.String substitution)
Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to INTERPOLATE_ALL 55 .


Perl5Substitution

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

Method Detail

__isInterpolationCharacter

private static final boolean __isInterpolationCharacter(char ch)

__addElement

private void __addElement(int value)

__parseSubs

private void __parseSubs(java.lang.String sub)

_finalInterpolatedSub

java.lang.String _finalInterpolatedSub(MatchResult result)

_calcSub

void _calcSub(java.lang.StringBuffer buffer,
              MatchResult result)

setSubstitution

public void setSubstitution(java.lang.String substitution)
Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to INTERPOLATE_ALL 55 . 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

setSubstitution

public void setSubstitution(java.lang.String substitution,
                            int numInterpolations)
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.


appendSubstitution

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

Specified by:
appendSubstitution in interface Substitution
Overrides:
appendSubstitution in class StringSubstitution