uk.org.ogsadai.activity.files
Class FileWritingActivity

java.lang.Object
  |
  +--uk.org.ogsadai.engine.Activity
        |
        +--uk.org.ogsadai.activity.files.AbstractFileActivity
              |
              +--uk.org.ogsadai.activity.files.FileWritingActivity

public class FileWritingActivity
extends AbstractFileActivity

The Activity which allows writing to a file. The file may be a text file or a binary file. The available actions include deleting and replacing portions of the file, and appending and inserting text into the file.

Author:
The OGSA-DAI Project Team

Field Summary
static int APPEND
          Indicate that we wish to perform a append operation.
static int DELETE
          Indicate that we wish to perform a delete operation.
static int INSERT
          Indicate that we wish to perform a insert operation.
static int NONE
          Indicate that no action has been set.
static int OFFSET_AND_LENGTH
          Indicate that we are identifying the text to modify using offset and/or length.
static boolean PER_FILE
          Indicate that we wish to perform the action on a per-file basis.
static boolean PER_LINE
          Indicate that we wish to perform the action on a per-line basis.
static int REGEXP
          Indicate that we are identifying the text to modify using a regular expression.
static int REPLACE
          Indicate that we wish to perform a replace operation.
static java.lang.String SEQ_SEPARATOR
          The character used to separate items in a sequence in the match expression.
static int STEP_SIZE_UPPER_LIMIT
          The upper limit on the step size which will be employed when reading and writing portions of files.
 
Fields inherited from class uk.org.ogsadai.activity.files.AbstractFileActivity
mConnectionManager, mTopDir
 
Fields inherited from class uk.org.ogsadai.engine.Activity
mContext, mExternalInputs, mExternalOutputs, mInternalInputs, mInternalOutputs
 
Constructor Summary
FileWritingActivity(org.w3c.dom.Element element)
          Parses the perform document which is passed as the parameter.
 
Method Summary
static int charAt(java.io.RandomAccessFile raFile, long pos)
          Returns the byte at a particular offset within a file.
static java.lang.String getFileContents(java.io.RandomAccessFile raFile)
          Obtains the entire content of a file and returns it as a single string.
protected  void initialise()
          Set up the input stream if we are expecting to receive data from a previous activity.
static void overwriteString(java.io.RandomAccessFile raFile, long offset, java.lang.String replacementString)
          Overwrite a sequence of bytes in a file with a replacement string.
 void processBlock()
          Perform the desired action, causing the file to be permanently rewritten.
 java.lang.String readLine(java.io.RandomAccessFile raFile)
           
static void replaceWithLongerString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString, int step)
          Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly longer than the sequence of bytes.
static void replaceWithShorterString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString, int step)
          Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly shorter than the sequence of bytes.
 
Methods inherited from class uk.org.ogsadai.engine.Activity
cleanUp, connectsTo, getActivityConfiguration, getName, getObservableStatus, getProperties, getStatus, hasActivityConfiguration, hasProperties, processFirst, setCompleted, setError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEQ_SEPARATOR

public static final java.lang.String SEQ_SEPARATOR
The character used to separate items in a sequence in the match expression.

See Also:
Constant Field Values

STEP_SIZE_UPPER_LIMIT

public static final int STEP_SIZE_UPPER_LIMIT
The upper limit on the step size which will be employed when reading and writing portions of files.

See Also:
Constant Field Values

NONE

public static final int NONE
Indicate that no action has been set.

See Also:
Constant Field Values

DELETE

public static final int DELETE
Indicate that we wish to perform a delete operation.

See Also:
Constant Field Values

REPLACE

public static final int REPLACE
Indicate that we wish to perform a replace operation.

See Also:
Constant Field Values

APPEND

public static final int APPEND
Indicate that we wish to perform a append operation.

See Also:
Constant Field Values

INSERT

public static final int INSERT
Indicate that we wish to perform a insert operation.

See Also:
Constant Field Values

OFFSET_AND_LENGTH

public static final int OFFSET_AND_LENGTH
Indicate that we are identifying the text to modify using offset and/or length.

See Also:
Constant Field Values

REGEXP

public static final int REGEXP
Indicate that we are identifying the text to modify using a regular expression.

See Also:
Constant Field Values

PER_FILE

public static final boolean PER_FILE
Indicate that we wish to perform the action on a per-file basis.

See Also:
Constant Field Values

PER_LINE

public static final boolean PER_LINE
Indicate that we wish to perform the action on a per-line basis.

See Also:
Constant Field Values
Constructor Detail

FileWritingActivity

public FileWritingActivity(org.w3c.dom.Element element)
                    throws ActivitySystemException,
                           ActivityUserException
Parses the perform document which is passed as the parameter.

Parameters:
element - the perform document
Throws:
ActivitySystemException - if there is a problem constructing the activity
ActivityUserException - if there was an error in the perform document
Method Detail

initialise

protected void initialise()
                   throws ActivitySystemException,
                          ActivityUserException
Set up the input stream if we are expecting to receive data from a previous activity.

Overrides:
initialise in class AbstractFileActivity
Throws:
ActivityUserException - If there is a user problem intialising the activity, like the top-level directory specified in the connection configuration does not exist.
ActivitySystemException - If there is a problem intialising the activity, like not using the FileSystemConnectionManager connection manager.
See Also:
Activity.initialise()

processBlock

public void processBlock()
Perform the desired action, causing the file to be permanently rewritten.

Specified by:
processBlock in class Activity

overwriteString

public static void overwriteString(java.io.RandomAccessFile raFile,
                                   long offset,
                                   java.lang.String replacementString)
                            throws java.io.IOException
Overwrite a sequence of bytes in a file with a replacement string. The bytes which are affected start at a given offset, and are equal in number to the length of the replacement string.

Parameters:
raFile - the file to overwrite bytes in
offset - the offset of the start of the bytes to overwrite
replacementString - the string to overwrite the bytes with
Throws:
java.io.IOException - if there's a problem accessing the file

replaceWithLongerString

public static void replaceWithLongerString(java.io.RandomAccessFile raFile,
                                           long offset,
                                           long length,
                                           java.lang.String replacementString,
                                           int step)
                                    throws java.io.IOException
Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly longer than the sequence of bytes.

This is achieved by performing the following three operations:

  1. Extend the length of the file by the excess in length.
  2. Starting from the end of the file and ending at the offset, shift the bytes toward the end of the file by a distance equal to the difference in length.
  3. Overwrite the replacement string at the specified offset.

Parameters:
raFile - the file within which to replace bytes
offset - the offset of the start of the bytes to replace
length - the number of bytes to replace
replacementString - the string to replace the bytes with, which must be longer than the number of bytes to replace
step - the number of bytes we shift at a time
Throws:
java.io.IOException - if there's a problem accessing the file

replaceWithShorterString

public static void replaceWithShorterString(java.io.RandomAccessFile raFile,
                                            long offset,
                                            long length,
                                            java.lang.String replacementString,
                                            int step)
                                     throws java.io.IOException
Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly shorter than the sequence of bytes.

This is achieved by performing the following three operations:

  1. Overwrite the replacement string at the specified offset.
  2. Starting from the offset and ending at the end of the file, shift the bytes toward the start of the file by a distance equal to the difference in length.
  3. Truncate the length of the file by the difference in length.

Parameters:
raFile - the file within which to replace bytes
offset - the offset of the start of the bytes to replace
length - the number of bytes to replace
replacementString - the string to replace the bytes with, which must be shorter than the number of bytes to replace
step - the number of bytes we shift at a time
Throws:
java.io.IOException - if there's a problem accessing the file

getFileContents

public static java.lang.String getFileContents(java.io.RandomAccessFile raFile)
                                        throws java.io.IOException,
                                               CommonSystemException
Obtains the entire content of a file and returns it as a single string.

Parameters:
raFile - the file to get the content of
Returns:
the content of the file
Throws:
java.io.IOException - if there's a problem accessing the file
CommonSystemException

readLine

public java.lang.String readLine(java.io.RandomAccessFile raFile)
                          throws java.io.IOException
java.io.IOException

charAt

public static int charAt(java.io.RandomAccessFile raFile,
                         long pos)
                  throws java.io.IOException
Returns the byte at a particular offset within a file. The file pointer is unaffected by calling this method.

Parameters:
raFile - the file
pos - the offset within the file
Returns:
the byte in the file at the offset
Throws:
java.io.IOException - if there was a problem accessing the file