|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--uk.org.ogsadai.activity.Activity | +--uk.org.ogsadai.activity.files.AbstractFileActivity | +--uk.org.ogsadai.activity.files.FileWritingActivity
This activity 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.
For information on this activity see the OGSA-DAI user doc:
OGSA-DAI/doc/interaction/activities/files/fileWritingActivity.html
Field Summary | |
static int |
APPEND
Indicate that we wish to perform a append operation. |
private static java.lang.String |
COPYRIGHT_NOTICE
Copyright statement |
static int |
DELETE
Indicate that we wish to perform a delete operation. |
static int |
INSERT
Indicate that we wish to perform a insert operation. |
private static DAILogger |
LOG
Logger object for logging in this class |
private int |
mAction
The action which will be performed on the file. |
private boolean |
mAlreadyReturnedBlank
Indicates whether we've already returned a blank. |
private java.io.File |
mFile
The file to write to. |
private long |
mFileLength
The length of the file to write to, before any writing takes place. |
private java.lang.String |
mFilename
The filename of the file to write to. |
private BlockReader |
mInput
The output from another activity, from which to read text to write into the file. |
private java.lang.String |
mLength
The number of bytes length over which the writing will occur. |
private InfiniteSet |
mMatch
An expression of the instances of matches of the regular expression to deal with. |
private boolean |
mMultiline
An indication of whether we are performing the action on a per-file or per-line basis. |
private java.lang.String |
mOffset
The number of bytes offset from which the writing will occur. |
private boolean |
mOptimiseForTime
An indication of whether we wish to aim for efficient use of time or efficent use of memory when performing edits on large files. |
private BlockWriter |
mOutput
The input to another activity, to which to write this activity's output. |
private java.io.RandomAccessFile |
mRaFile
The file to write to. |
private java.lang.String |
mRegexp
The regular expression to use to locate the part of the file to write to. |
private java.lang.String |
mString
The string containing the text to write into the file. |
private int |
mType
The style we are using to indicate where the writing will occur. |
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 |
mCredentials, mFileAccessProvider, mTopDir |
Fields inherited from class uk.org.ogsadai.activity.Activity |
mContext, mExternalInputs, mExternalOutputs, mInternalInputs, mInternalOutputs |
Constructor Summary | |
FileWritingActivity(org.w3c.dom.Element element)
|
Method Summary | |
private void |
append(java.io.RandomAccessFile raFile,
java.lang.String string)
Append a string to the end of a file, thereby extending its length. |
static int |
charAt(java.io.RandomAccessFile raFile,
long pos)
Returns the byte at a particular offset within a file. |
private void |
delete(java.io.RandomAccessFile raFile,
long offset,
long length)
Delete a number of contiguous bytes from a file, starting at a particular offset. |
private void |
extractFromOrTextAttribute(org.w3c.dom.Element node)
Reads the from or text attributes of
the given XML element, and sets instance variables accordingly. |
private void |
extractOptimiseAttribute(org.w3c.dom.Element node)
Sets the mOptimiseForTime instance variable
depending on the contents of the optimiseSpeed
attribute of the provided XML element. |
static java.lang.String |
getFileContents(java.io.RandomAccessFile raFile)
Obtains the entire content of a file and returns it as a single string. |
private static java.lang.String |
getNextTenBytes(java.io.RandomAccessFile raFile)
Return the 35 bytes from the provided file located after the current file pointer. |
private int |
getStepSize(long length)
Returns the step size, for reading a chunk of data of total size length . |
private java.lang.String |
getStringInput()
Returns the string used as the activity's input. |
void |
initialise()
Gets the data resource accessor for the files data resource according to whether the users credentials permit this access. |
private void |
insert(java.io.RandomAccessFile raFile,
long offset,
java.lang.String string)
Insert a string into a particular place in a file, causing the contents of the file after that place to be shifted away from the start of the file by an amount equal to the length of the string. |
private java.lang.String |
obtainInput()
Obtains all the data available from the activity providing its output to this activity, and returns it as a single string. |
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. |
private int |
parseInt(java.lang.String intString)
Helper method to parse strings into integers. |
private long |
parseLength(java.lang.String string,
long fileLength,
long offset)
Parses a string containing a representation of the length of a sequence of bytes within a file. |
private void |
parseLocate(org.w3c.dom.Element node)
Parse a locate element, extracting its contents and
setting the instance variables of this object appropriately. |
private InfiniteSet |
parseMatch(java.lang.String list)
Parses an expression of a collection of integers. |
private long |
parseOffset(java.lang.String string,
long fileLength)
Parses a string containing a representation of an offset within a file or a line. |
private void |
performAction(java.io.RandomAccessFile raFile,
int type,
long offset,
long length,
boolean perLine,
java.lang.String string)
Perform an action on a file. |
private void |
performPerFileOffsetAndLength(java.io.RandomAccessFile raFile,
long fileLength)
Perform a replacement of text within a file, on a per-file basis, locating the text based on a range of bytes uniquely defined by an offset and a length. |
private void |
performPerFileRegexp(java.io.RandomAccessFile raFile,
long fileLength)
Perform a replacement of text within a file, on a per-file basis, locating the text based on matches of a regular expression. |
private void |
performPerLineOffsetAndLength(java.io.RandomAccessFile raFile,
long fileLength)
Perform a replacement of text within a file, on a per-line basis, locating the text based on a range of bytes uniquely defined by an offset and a length. |
private void |
performPerLineRegexp(java.io.RandomAccessFile raFile,
long fileLength)
Perform a replacement of text within a file, on a per-line basis, locating the text based on matches of a regular expression. |
private java.lang.String |
performSubstitutions(java.lang.String string,
AbstractMatcher matcher)
Return a string equal to the provided string, but with group variables substituted with their actual value, in the context of a particular regular expression match. |
void |
processBlock()
Performs an iteration of the processing of an activity. |
java.lang.String |
readLine(java.io.RandomAccessFile raFile)
Read a line from the file |
private void |
replace(java.io.RandomAccessFile raFile,
long offset,
long length,
java.lang.String string)
Replace a sequence of contiguous bytes in a file with a string. |
private void |
replaceString(java.io.RandomAccessFile raFile,
long offset,
long length,
java.lang.String replacementString)
Replace a sequence of contiguous bytes in a file with a string. |
private java.lang.String |
replaceStringInString(java.lang.String hostString,
int offset,
int length,
java.lang.String replacement)
Replaces a substring within a string with another string. |
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.activity.Activity |
cleanUp, connectsTo, createOutputPipe, getActivityConfiguration, getActivityName, getCause, getName, getObservableStatus, getProperties, getSession, getStatus, hasActivityConfiguration, hasProperties, process, processFirst, setActivityName, setCompleted, setError |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final java.lang.String COPYRIGHT_NOTICE
private static final DAILogger LOG
public static final java.lang.String SEQ_SEPARATOR
public static final int STEP_SIZE_UPPER_LIMIT
public static final int NONE
public static final int DELETE
public static final int REPLACE
public static final int APPEND
public static final int INSERT
public static final int OFFSET_AND_LENGTH
public static final int REGEXP
public static final boolean PER_FILE
public static final boolean PER_LINE
private int mType
private java.lang.String mFilename
private java.io.File mFile
private java.io.RandomAccessFile mRaFile
private long mFileLength
private java.lang.String mOffset
private java.lang.String mLength
private java.lang.String mRegexp
private InfiniteSet mMatch
private boolean mMultiline
private boolean mOptimiseForTime
private int mAction
private BlockReader mInput
private BlockWriter mOutput
private java.lang.String mString
private boolean mAlreadyReturnedBlank
Constructor Detail |
public FileWritingActivity(org.w3c.dom.Element element) throws ActivitySpecificationException, ActivityCreationException
Method Detail |
private InfiniteSet parseMatch(java.lang.String list) throws ActivitySpecificationException
The expression must be a comma-separated list of values. The values may be any of:
Value | String | Represents |
---|---|---|
Single, positive integers | x | The integer x |
Finite ranges of positive integers | x- y |
The interval [x, y], where x < y |
Infinite ranges of positive integers | x- |
The interval [x, infinity) |
Strictly increasing infinite sequences of positive integers | x1: x2: x3:... |
The infinite sequence (x1, x2, x3, ...), where x1 < x2 and x2 < x3 |
list
- The comma-separated list of values
ActivitySpecificationException
- If there was a syntax error in the list of valuesprivate int parseInt(java.lang.String intString) throws DAINumberFormatException
intString
- String to parse
DAINumberFormatException
- If the string cannot be parsed into an integer.private void parseLocate(org.w3c.dom.Element node) throws ActivitySpecificationException
locate
element, extracting its contents and
setting the instance variables of this object appropriately.
node
- The element to parse
ActivitySpecificationException
- If there is a syntax error in the match
expressionprivate void extractOptimiseAttribute(org.w3c.dom.Element node)
mOptimiseForTime
instance variable
depending on the contents of the optimiseSpeed
attribute of the provided XML element. Iff the element does not
contain this attribute, or contains the attribute with a
boolean true
value then the field is set to true.
node
- The XML elementprivate void extractFromOrTextAttribute(org.w3c.dom.Element node) throws ActivitySpecificationException
from
or text
attributes of
the given XML element, and sets instance variables accordingly.
node
- the XML element
ActivitySpecificationException
- If neither from
nor text
attributes were specifiedprivate long parseOffset(java.lang.String string, long fileLength) throws DAINumberFormatException, DAIOutOfBoundsException
start
or end
, representing the
beginning and end of the file or the line, respectively.
string
- The string to parsefileLength
- The length of the file or the line
DAINumberFormatException
- If the string did not contain an integer.
DAIOutOfBoundsException
- If the string contained an out of bounds integer.private long parseLength(java.lang.String string, long fileLength, long offset) throws DAINumberFormatException, DAIOutOfBoundsException
toEnd
, which represents the length
from the given offset to the end of the file.
string
- The string to parsefileLength
- The total length of the fileoffset
- The offset from which this length is considered to start
DAINumberFormatException
- If the string did not contain an integer.
DAIOutOfBoundsException
- If the string contained an out of bounds integer.public void initialise() throws ActivitySpecificationException, ActivityExecutionException
AbstractFileActivity
initialise
in class AbstractFileActivity
ActivitySpecificationException
- If the user is not permitted to access the resource.
ActivityExecutionException
- If some internal problem occurs.Activity.initialise()
public void processBlock()
Activity
setCompleted
method to indicate that
processing is complete, or the setError
method if
an error occurs that will prevent the processing from
completing.
processBlock
in class Activity
private void performPerLineRegexp(java.io.RandomAccessFile raFile, long fileLength) throws NonIncreasingSequenceException, MalformedRegexpPatternException, MalformedReplacementStringException, UnsupportedRegexpPatternFormatException, java.io.IOException
raFile
- The file to replace text withinfileLength
- The length of the file
NonIncreasingSequenceException
- If the match expression contained a sequence of integers
which was not strictly increasing
MalformedRegexpPatternException
- If the regular expression contains a syntax error.
MalformedReplacementStringException
- If there was a syntax error in the string
UnsupportedRegexpPatternFormatException
- If the regular expression is unsupported.
java.io.IOException
- If there's a problem accessing the fileprivate void performPerFileRegexp(java.io.RandomAccessFile raFile, long fileLength) throws NonIncreasingSequenceException, MalformedRegexpPatternException, MalformedReplacementStringException, UnsupportedRegexpPatternFormatException, java.io.IOException
raFile
- The file to replace text withinfileLength
- The length of the file
NonIncreasingSequenceException
- If the match expression contained a sequence of integers
which was not strictly increasing
MalformedRegexpPatternException
- If the regular expression contains a syntax error.
MalformedReplacementStringException
- If there was a syntax error in the string
UnsupportedRegexpPatternFormatException
- If the regular expression is unsupported.
java.io.IOException
- If there's a problem accessing the fileprivate void performPerLineOffsetAndLength(java.io.RandomAccessFile raFile, long fileLength) throws ActivitySpecificationException, java.io.IOException
raFile
- The file to replace text withinfileLength
- The length of the file
ActivitySpecificationException
- If the offset or length are invalid for the requested
operation.
java.io.IOException
- If there's a problem accessing the fileprivate void performPerFileOffsetAndLength(java.io.RandomAccessFile raFile, long fileLength) throws ActivitySpecificationException, java.io.IOException
raFile
- The file to replace text withinfileLength
- The length of the file
ActivitySpecificationException
- If the offset or length are invalid for the requested
operation.
java.io.IOException
- If there's a problem accessing the fileprivate java.lang.String performSubstitutions(java.lang.String string, AbstractMatcher matcher) throws MalformedReplacementStringException
Groups are defined by bracketing in a regular expression, and
are numbered from left-to-right. Group 0 denotes the entire
regular expression.
Groups may be referenced by group variables in a string, of the
form $0
, $1
, etc. These may also be
written ${0}
, ${1}
, etc., in order to
disambiguate the end of the group variable and subsequent
digits in the string.
Consider the following example:
Happy birthday!
^H(.?)p+y
(bi([rst])([a-z])hday)!
Happy birthday!
a
birthday
r
t
$3$1$4$4y $2!
ratty birthday!
string
- Text which may contain references to groups by referring to
$
nmatcher
- The matcher object which has already found a match
MalformedReplacementStringException
- If there was a syntax error in the stringprivate java.lang.String replaceStringInString(java.lang.String hostString, int offset, int length, java.lang.String replacement)
hello
with the string yes
will result in the string hyeslo
.
hostString
- The string within which to replace the substringoffset
- The offset of the substring to replace, within
hostString
length
- The length of the substring to replacereplacement
- The string with which to replace the substring
private int getStepSize(long length)
length
. This will depend on whether the user
has chosen to optimise for minimal speed or for minimal memory
usage.
length
- The total length of the data chunk
private void performAction(java.io.RandomAccessFile raFile, int type, long offset, long length, boolean perLine, java.lang.String string) throws java.io.IOException
raFile
- The file within which to perform the actiontype
- The action to perform. One of DELETE
,
REPLACE
, INSERT
, APPEND
offset
- The offset at which delete, replace and insert will operatelength
- The length of the selection of bytes used by delete and
replaceperLine
- true
indicates that this action is being
executed on a per-line basis, throughout the file,
false
indicates that this action is being
executed once for the entire file.string
- The string used by replace, insert and append
java.io.IOException
- If there's a problem accessing the fileprivate void append(java.io.RandomAccessFile raFile, java.lang.String string) throws java.io.IOException
Semantically equivalent to calling method replace
with arguments raFile
,
raFile.length()
, 0, string
.
raFile
- The file to append tostring
- The string to append
java.io.IOException
- If there's a problem accessing the fileprivate void insert(java.io.RandomAccessFile raFile, long offset, java.lang.String string) throws java.io.IOException
Semantically equivalent to calling method replace
with arguments raFile
, offset
, 0,
string
.
raFile
- The file to insert the string intooffset
- The offset at which to insert the stringstring
- TThe string to insert
java.io.IOException
- If there's a problem accessing the fileprivate void delete(java.io.RandomAccessFile raFile, long offset, long length) throws java.io.IOException
offset
+ length
) will be shifted towards
the start of the file by an amount equal to length
.
Semantically equivalent to calling method replace
with
arguments raFile
, offset
, length
,
""
.
raFile
- The file to delete the bytes fromoffset
- The offset at which to start deleting byteslength
- The number of bytes to delete
java.io.IOException
- If there's a problem accessing the fileprivate void replace(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String string) throws java.io.IOException
Semantically equivalent to calling method replaceString
with arguments raFile
, offset
,
length
, string
.
raFile
- The file to replace bytes withinoffset
- The offset of the start of the sequence of bytes to replacelength
- The number of bytes to replacestring
- The string to replace the bytes with
java.io.IOException
- If there's a problem accessing the fileprivate java.lang.String getStringInput()
private java.lang.String obtainInput()
OutOfMemoryError
since it reads all of the output
from the previous activity into memory. Instead, this should be
implemented in a streaming fashion.
private void replaceString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString) throws java.io.IOException
raFile
- The file to replace bytes withinoffset
- The offset of the start of the sequence of bytes to replacelength
- The number of bytes to replacereplacementString
- The string to replace the bytes with
java.io.IOException
- If there's a problem accessing the filepublic static void overwriteString(java.io.RandomAccessFile raFile, long offset, java.lang.String replacementString) throws java.io.IOException
raFile
- The file to overwrite bytes inoffset
- The offset of the start of the bytes to overwritereplacementString
- The string to overwrite the bytes with
java.io.IOException
- If there's a problem accessing the filepublic static void replaceWithLongerString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString, int step) throws java.io.IOException
This is achieved by performing the following three operations:
raFile
- The file within which to replace bytesoffset
- The offset of the start of the bytes to replacelength
- The number of bytes to replacereplacementString
- The string to replace the bytes with, which must be longer
than the number of bytes to replacestep
- The number of bytes we shift at a time
java.io.IOException
- If there's a problem accessing the filepublic static void replaceWithShorterString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString, int step) throws java.io.IOException
This is achieved by performing the following three operations:
raFile
- The file within which to replace bytesoffset
- The offset of the start of the bytes to replacelength
- The number of bytes to replacereplacementString
- The string to replace the bytes with, which must be shorter
than the number of bytes to replacestep
- The number of bytes we shift at a time
java.io.IOException
- If there's a problem accessing the filepublic static java.lang.String getFileContents(java.io.RandomAccessFile raFile) throws java.io.IOException
raFile
- The file to get the content of
java.io.IOException
- If there was a problem accessing the filepublic java.lang.String readLine(java.io.RandomAccessFile raFile) throws java.io.IOException
raFile
- The file
java.io.IOException
- If there was a problem accessing the filepublic static int charAt(java.io.RandomAccessFile raFile, long pos) throws java.io.IOException
raFile
- The filepos
- The offset within the file
java.io.IOException
- If there was a problem accessing the fileprivate static java.lang.String getNextTenBytes(java.io.RandomAccessFile raFile) throws java.io.IOException
raFile
- The file to read the bytes from
java.io.IOException
- If there were problems accessing the file
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |