org.schmant.support.text
Class TextReplacer

java.lang.Object
  extended by org.schmant.support.text.TextReplacer

public class TextReplacer
extends Object

The text replacer uses one or several PatternAndStrategy objects to replace text in a text file.

Since:
0.8
Author:
Karl Gustafsson

Field Summary
static int DEFAULT_BUFFER_SIZE
           
 
Constructor Summary
TextReplacer(Collection<? extends PatternAndStrategy> replaces)
          Create a new text replacer.
TextReplacer(Collection<? extends PatternAndStrategy> replaces, Charset inputEncoding, Charset outputEncoding, int bufSize)
          Create a new text replacer.
TextReplacer(Collection<? extends PatternAndStrategy> replaces, Charset inputEncoding, Charset outputEncoding, int bufSize, boolean errorIfReplaceNotFound)
          Create a new text replacer.
 
Method Summary
 void replace(ReadableFile source, WritableFile target)
          Run the replaces on the source file and put the result in the target file.
 void replace(ReadableFile source, WritableFile target, DirectoryView tempFilesDir, int maxNoOfReplacesPerPattern)
          Run the replaces on the source file and put the result in the target file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

TextReplacer

public TextReplacer(Collection<? extends PatternAndStrategy> replaces)
Create a new text replacer. The platform's default character encoding will be used for both source and target files.

Parameters:
replaces - A collection of PatternAndStrategy objects that are used to replace text. The replaces are run in the order that they are returned when iterating over the collection.

TextReplacer

public TextReplacer(Collection<? extends PatternAndStrategy> replaces,
                    Charset inputEncoding,
                    Charset outputEncoding,
                    int bufSize)
             throws IllegalArgumentException
Create a new text replacer.

Parameters:
replaces - A collection of PatternAndStrategy objects that are used to replace text. The replaces are run in the order that they are returned when iterating over the collection.
inputEncoding - The character encoding for source files. Set this to null to use the platform's default encoding.
outputEncoding - The character encoding for target files (the files where replace results are written to). Set to null to use the platform's default encoding.
bufSize - The size of internal in-memory buffers used by this object.
Throws:
IllegalArgumentException - If the buffer size is less than 1.

TextReplacer

public TextReplacer(Collection<? extends PatternAndStrategy> replaces,
                    Charset inputEncoding,
                    Charset outputEncoding,
                    int bufSize,
                    boolean errorIfReplaceNotFound)
             throws IllegalArgumentException
Create a new text replacer.

Parameters:
replaces - A collection of PatternAndStrategy objects that are used to replace text. The replaces are run in the order that they are returned when iterating over the collection.
inputEncoding - The character encoding for source files. Set this to null to use the platform's default encoding.
outputEncoding - The character encoding for target files (the files where replace results are written to). Set to null to use the platform's default encoding.
bufSize - The size of internal in-memory buffers used by this object.
errorIfReplaceNotFound - If a replace is not found, is that an error?
Throws:
IllegalArgumentException - If the buffer size is less than 1.
Method Detail

replace

public void replace(ReadableFile source,
                    WritableFile target)
             throws WrappedIOException,
                    ReplaceNotFoundException
Run the replaces on the source file and put the result in the target file.

Parameters:
source - The source file.
target - The target file.
Throws:
WrappedIOException - On I/O errors.
ReplaceNotFoundException - If one text to replace was not found and this object is configured to treat that as an error.

replace

public void replace(ReadableFile source,
                    WritableFile target,
                    DirectoryView tempFilesDir,
                    int maxNoOfReplacesPerPattern)
             throws WrappedIOException,
                    ReplaceNotFoundException
Run the replaces on the source file and put the result in the target file.

Parameters:
source - The source file.
target - The target file.
tempFilesDir - A directory for temporary files. If there is more than one replace, the intermediate results are stored in temporary files in that directory. Set this to null to use the default temporary files directory.
maxNoOfReplacesPerPattern - Don't replace text more than this number of times for each pattern, even if more strings match. Set this to -1 for an unlimited number of replaces.
Throws:
WrappedIOException - On I/O errors.
ReplaceNotFoundException - If one text to replace was not found and this object is configured to treat that as an error.