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.

Version:
$Revision: 1.3 $
Author:
Marko van Dooren

Constructor Summary
RegexReplacer(java.lang.String pattern, java.lang.String replacement)
          public behavior

pre pattern != null;
pre replacement != null;
pre (* pattern must be a valid regular expression *);

post getPattern().pattern().equals(pattern);
// The pattern is a multi line pattern.
post getPattern().flags() == Pattern.MULTILINE;
post getReplacement() == replacement;
Initialize a new RegexReplacer for the given pattern and replacement.
RegexReplacer(java.lang.String pattern, java.lang.String replacement, int flags)
          public behavior

pre pattern != null;
pre replacement != null;
pre (* pattern must be a valid regular expression *);
pre (* flags must be a valid flag for a pattern.
 
Method Summary
 jregex.Pattern getPattern()
          public behavior

post \result != null;
Return the pattern of this RegexReplacer.
 java.lang.String getReplacement()
          public behavior

post \result != null;
Return the replacement string of this RegexReplacer.
static void main(java.lang.String[] args)
           
 void replace(java.io.File input, java.io.File output)
          public behavior

pre input != null;
pre output != null;

post (* all occurrences of getPattern() will be replaced by getReplacement()
in the input file, and the result will be written the output 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)
public behavior

pre pattern != null;
pre replacement != null;
pre (* pattern must be a valid regular expression *);

post getPattern().pattern().equals(pattern);
// The pattern is a multi line pattern.
post getPattern().flags() == Pattern.MULTILINE;
post getReplacement() == 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.

RegexReplacer

public RegexReplacer(java.lang.String pattern,
                     java.lang.String replacement,
                     int flags)
public behavior

pre pattern != null;
pre replacement != null;
pre (* pattern must be a valid regular expression *);
pre (* flags must be a valid flag for a pattern. *);
pre (* The flags should be a bitwise or of the provide static flags, THIS WILL CHANGE IN THE FUTURE *);

post getPattern().pattern().equals(pattern);
post getPattern().flags() == flags;
post getReplacement() == replacement;
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.
Method Detail

replace

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

pre input != null;
pre output != null;

post (* 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 *);

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.

getPattern

public jregex.Pattern getPattern()
public behavior

post \result != null;
Return the pattern of this RegexReplacer.

getReplacement

public java.lang.String getReplacement()
public behavior

post \result != null;
Return the replacement string of this RegexReplacer.

main

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