org.jutil.java.regex
Class RegexReplacer

java.lang.Object
  |
  +--org.jutil.java.regex.RegexReplacer

public class RegexReplacer
extends java.lang.Object

A class of object the replace regular expressions for others.

For example, it can replace a regular expression in a file and write the output to another file.

At this moment, we use JRegex instead of jdk 1.4 regular expressions because:

This means that this class will change in the future, and the Pattern class will probably disappear since it's only adapts the JRegex Pattern class to the interface of java.util.regex.Pattern in order to simplify porting afterwards.


Constructor Summary
RegexReplacer(java.lang.String pattern, java.lang.String replacement)
          Initialize a new RegexReplacer for the given pattern and replacement.
RegexReplacer(java.lang.String pattern, java.lang.String replacement, int flags)
          Initialize a new RegexReplacer for the given pattern, replacement and flags.
 
Method Summary
 Pattern getPattern()
          Return the pattern of this RegexReplacer.
 java.lang.String getReplacement()
          Return the replacement string of this RegexReplacer.
static void main(java.lang.String[] args)
           
 void replace(java.io.File input, java.io.File output)
          Replace all occurrences of the pattern of this RegexReplacer in the input file by the replacement of this RegexReplacer, and write the result to the output file. Both File objects may refer to the same real file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RegexReplacer

public RegexReplacer(java.lang.String pattern,
                     java.lang.String replacement)
Initialize a new RegexReplacer for the given pattern and replacement.

Parameters:
pattern - a String containing the pattern to be replaced.
replacement - a String containing the pattern to replace the matched pattern.

Specifications:
public behavior
requires pattern != null;
requires replacement != null;
requires (* pattern must be a valid regular expression *);
ensures getPattern().pattern().equals(pattern);
ensures getPattern().flags() == Pattern.MULTILINE;
ensures getReplacement() == replacement;

RegexReplacer

public RegexReplacer(java.lang.String pattern,
                     java.lang.String replacement,
                     int flags)
Initialize a new RegexReplacer for the given pattern, replacement and flags.

Parameters:
pattern - A String containing the pattern to be replaced.
replacement - A String containing the pattern to replace the matched pattern.
flags - The flags for the pattern.

Specifications:
public behavior
requires pattern != null;
requires replacement != null;
requires (* pattern must be a valid regular expression *);
requires (* flags must be a valid flag for a pattern. *);
requires (* The flags should be a bitwise or of the provide static flags, <b>THIS WILL CHANGE IN THE FUTURE</b> *);
ensures getPattern().pattern().equals(pattern);
ensures getPattern().flags() == flags;
ensures getReplacement() == replacement;
Method Detail

replace

public void replace(java.io.File input,
                    java.io.File output)
             throws java.io.FileNotFoundException,
                    java.io.IOException

Replace all occurrences of the pattern of this RegexReplacer in the input file by the replacement of this RegexReplacer, and write the result to the output file.

Both File objects may refer to the same real file.

Parameters:
input - The file in which the pattern must be replaced.
output - The file to which the result must be written.

Specifications:
public behavior
requires input != null;
requires output != null;
ensures (* all occurrences of getPattern() will be replaced by getReplacement() in the input file, and the result will be written the output file. *);
signals (FileNotFoundException) (* one of the files is not found *);
signals (IOException) (* something went wrong during IO *);

getPattern

public Pattern getPattern()
Return the pattern of this RegexReplacer.

Specifications:
public behavior
ensures \result != null;

getReplacement

public java.lang.String getReplacement()
Return the replacement string of this RegexReplacer.

Specifications:
public behavior
ensures \result != null;

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception