com.acsinet_solutions.util.regexp
Class Perl5Regexp

java.lang.Object
  extended by com.acsinet_solutions.util.regexp.Perl5Regexp

public final class Perl5Regexp
extends java.lang.Object

Implementation of a simple regular expression class. It is a wrapper over an external perl 5 regular expression facility created to provide a more friendly and change independent interface to the user.

This implementation is based on J2SE regexp facilities. Previous versions of the framework used Jakarta ORO.

This class IS thread safe.

(c) 2004 ACSINET S.A. de C.V. Derechos Reservados

Author:
Santiago Arriaga

Constructor Summary
Perl5Regexp(java.lang.String re)
          Public constructor for a regular expression with no default sunstitution template string and case sensitive
Perl5Regexp(java.lang.String re, boolean caseSensitive)
          Public constructor for a regular expression with no default sunstitution template string
Perl5Regexp(java.lang.String re, boolean caseSensitive, java.lang.String defaultSubstitutionTemplate)
          Public constructor for a regular expression
 
Method Summary
 java.util.List<java.lang.String> apply(java.lang.String str)
          Match the string against the given string and return an array of values containing the match.
 java.lang.String getExpressionString()
          Get the inner regular expression string
static Perl5Regexp getValidRegexp(java.lang.String regexpString)
          Get a valid Perl5Regexp object from the given regular expression String instance.
 java.util.List<java.lang.String> split(java.lang.String str)
          Split a string using the regular expression as delimiter.
 java.util.List<java.lang.String> split(java.lang.String str, boolean returnTokens)
          Split a string using the regular expression as delimiter.
 java.lang.String substitute(java.lang.String str)
          Substitute the results from the regular expression applied into the string given as a parameter into a default tempalate string.
 java.lang.String substitute(java.lang.String str, java.lang.String template)
          Substitute the results from the regular expression applied into the string given as a parameter into the given template string.
 java.lang.String substitute(java.lang.String str, java.lang.String template, java.util.EnumSet<Substitution> flags)
          Substitute the results from the regular expression applied into the string given as a parameter into the given template string
 boolean test(java.lang.String str)
          Test if the given string matches the given string.
 java.util.List<java.lang.String> testAndApply(java.lang.String str)
          Convenience implementation that combines test() and apply() method and is more efficient when both methods are being used together
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Perl5Regexp

public Perl5Regexp(java.lang.String re,
                   boolean caseSensitive,
                   java.lang.String defaultSubstitutionTemplate)
            throws SyntaxException
Public constructor for a regular expression

Parameters:
re - Regular expression definition string
caseSensitive - true if the match will be case sensitiva; false otherwise
defaultSubstitutionTemplate - Default string used for substitution or null if no default substitution string will be used
Throws:
com.acsinet_solutions.cetia4.regexp.RegexpSyntaxException - Thrown if a syntax exception is found while creating the regular exception
SyntaxException

Perl5Regexp

public Perl5Regexp(java.lang.String re,
                   boolean caseSensitive)
            throws SyntaxException
Public constructor for a regular expression with no default sunstitution template string

Parameters:
re - Regular expression definition string
caseSensitive - true if the match will be case sensitiva; false otherwise
Throws:
com.acsinet_solutions.cetia4.regexp.RegexpSyntaxException - Thrown if a syntax exception is found while creating the regular exception
SyntaxException

Perl5Regexp

public Perl5Regexp(java.lang.String re)
            throws SyntaxException
Public constructor for a regular expression with no default sunstitution template string and case sensitive

Parameters:
re - Regular expression definition string
Throws:
com.acsinet_solutions.cetia4.regexp.RegexpSyntaxException - Thrown if a syntax exception is found while creating the regular exception
SyntaxException
Method Detail

getValidRegexp

public static Perl5Regexp getValidRegexp(java.lang.String regexpString)
Get a valid Perl5Regexp object from the given regular expression String instance. This method is useful for creating regexp expressions and turning errors into runtime errors

Parameters:
regexpString - A not-null valid regexp String instance
Throws:
java.lang.RuntimeException - Thrown if the input regular expression String is not valid

test

public final boolean test(java.lang.String str)
Test if the given string matches the given string.

Parameters:
str - not-null String against whom the regular expression will be tested.
Returns:
true if the regular expression matches; false otherwise

apply

public final java.util.List<java.lang.String> apply(java.lang.String str)
                                             throws RegexpMatchException
Match the string against the given string and return an array of values containing the match.

Parameters:
str - String against whom the regular expression will be matched.
Returns:
a not-null List containing the matching parts if there are any to be included. All the members of this List are non-null strings.
Throws:
com.acsinet_solutions.cetia4.regexp.RegexpMatchException - Thrown if the given string not make match with the regular expression. For an alternative upon this use method test() can be used first to check the string matches correctly or use testAndApply()
RegexpMatchException

testAndApply

public final java.util.List<java.lang.String> testAndApply(java.lang.String str)
Convenience implementation that combines test() and apply() method and is more efficient when both methods are being used together

Parameters:
str - String against whom the regular expression will be matched.
Returns:
A null List if the test() method did not pass; The results of the apply() method otherwise

substitute

public final java.lang.String substitute(java.lang.String str,
                                         java.lang.String template,
                                         java.util.EnumSet<Substitution> flags)
                                  throws RegexpMatchException
Substitute the results from the regular expression applied into the string given as a parameter into the given template string

Parameters:
str - String against whom the regular expression will be applied.
template - String that contains the format info. If template is null, the internal default template will be used (if any).
flags - The following flags can be used:
  • PARTIAL_SUBSTITUTION: Use if no global substitution is desired
  • NOEXCEPTION_SUBSTITUTION: Use if we donīt want an exception to be sent when an error occurs
Returns:
the resulting formatted string
Throws:
com.acsinet_solutions.cetia4.regexp.RegexpMatchException - thrown if the string does not makes match with the regular expression. For an alternative upon this use method test can be used first to check the string matches correctly
RegexpMatchException

substitute

public final java.lang.String substitute(java.lang.String str,
                                         java.lang.String template)
                                  throws RegexpMatchException
Substitute the results from the regular expression applied into the string given as a parameter into the given template string. The substitution will be global

Parameters:
str - String against whom the regular expression will be applied.
format - String that contains the format info
Returns:
the resulting formatted string
Throws:
com.acsinet_solutions.cetia4.regexp.RegexpMatchException - thrown if the string does not makes match with the regular expression. For an alternative upon this use method test can be used first to check the string matches correctly
RegexpMatchException

substitute

public final java.lang.String substitute(java.lang.String str)
                                  throws RegexpMatchException
Substitute the results from the regular expression applied into the string given as a parameter into a default tempalate string. The string must be defined for the implementing class or established using one of its constructors or setup methods. The substitution will be global

Parameters:
str - String against whom the regular expression will be applied.
Returns:
the resulting formatted string
Throws:
com.acsinet_solutions.cetia4.regexp.RegexpMatchException - thrown if the string does not makes match with the regular expression. For an alternative upon this use method test can be used first to check the string matches correctly
RegexpMatchException

split

public final java.util.List<java.lang.String> split(java.lang.String str,
                                                    boolean returnTokens)
Split a string using the regular expression as delimiter. This method can be used instead of StringTokenizer when the token is not constant

Parameters:
str - String against whom the regular expression will be applied to obtain make the split.
returnTokens - true if we want to return also the tokens, and false otherwise. The default is false.
Returns:
the splitted string as a not-null List

split

public final java.util.List<java.lang.String> split(java.lang.String str)
Split a string using the regular expression as delimiter. This method can be used instead of StringTokenizer when the token is not constant. This method does not return tokens

Parameters:
str - String against whom the regular expression will be applied to obtain make the split.
Returns:
the splitted string as a not-null List

getExpressionString

public final java.lang.String getExpressionString()
Get the inner regular expression string