|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object tribble.text.DifferenceReader
Text difference generator (INCOMPLETE).
This class takes two input text streams and generates the differences between them. Each input stream is composed of lines of text. The difference generator determines the differences between the first input stream, called the previous (or old) input, and the second input stream, called the current (or new) input.
The output of the generator is a sequence of deltas. Each delta specifies either a keep, delete, or insert command for a specific text line number. Applying the sequence of deltas to text lines in the old input stream produces the text lines of the new input stream.
Delta commands:
CMD_KEEP
)
CMD_DELETE
)
CMD_INSERT
)
CMD_END
)
The difference generator operates by using a look-ahead window of a certain number of text lines. By default, this window contains 200 lines of text, but this size can be changed if desired.
Usage
First, the previous (old) and current (new) input streams are initialized.
LineNumberReader prev; LineNumberReader curr; prev = new LineNumberReader(...); curr = new LineNumberReader(...);
Next, a difference generator is created and initialized.
DifferenceReader difr;
difr = new DifferenceReader
(prev, curr);
The size of the difference window can be changed if so desired.
difr.setWindowSize
(400);
At this point, the sequence of deltas between the old and new input streams can be generated.
int cmd; do { String text; // Get the next delta command cmd = difr.getDelta
(); switch (cmd) { case DifferenceReader.CMD_KEEP
: // Keep (copy) a line from the old stream text = difr.getDeltaText
(); ... break; case DifferenceReader.CMD_DELETE
: // Delete a line from the old stream ... break; case DifferenceReader.CMD_INSERT
: // Insert a line from the new stream text = difr.getDeltaText
(); ... break; } } while (cmd != DifferenceReader.CMD_END
);
A CMD_END
command is generated when both the old and new input
streams have been read completely.
After all of the difference deltas have been generated, the difference generator can be closed.
difr.close
();
Acknowledgments
This code was derived from an original algortihm devised by David R. Tribble.
Field Summary | |
static short |
CMD_DELETE
Delta command: Delete a line from the old input stream. |
static short |
CMD_END
Delta command: End of both input streams. |
static short |
CMD_INSERT
Delta command: Insert a line from the new input stream. |
static short |
CMD_KEEP
Delta command: Keep a line from the old input stream. |
static int |
DFL_WINDOW_LEN
Default text line window size (200). |
Constructor Summary | |
DifferenceReader(java.io.Reader prev,
java.io.Reader curr)
Constructor. |
Method Summary | |
void |
close()
Close this difference generator. |
protected void |
finalize()
Finalization. |
int |
getDelta()
Generate the next difference delta command. |
int |
getDeltaLineNumber()
Retrieve the line number of the text line for the last difference delta command generated. |
java.lang.String |
getDeltaText()
Retrieve the text line for the last difference delta command generated. |
static void |
main(java.lang.String[] args)
Determine the difference between two text files. |
protected tribble.text.DiffLine |
readLine(java.io.LineNumberReader in)
|
void |
setWindowSize(int len)
Establish the size of the text line difference window. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int DFL_WINDOW_LEN
public static final short CMD_END
public static final short CMD_KEEP
public static final short CMD_DELETE
public static final short CMD_INSERT
Constructor Detail |
public DifferenceReader(java.io.Reader prev, java.io.Reader curr)
prev
- Previous (old) input text stream.
This provides lines of text from the old file that is being compared to
the new file.
This stream is wrapped in a LineNumberReader
object,
unless it is already of this type.curr
- Current (new) input text stream.
This provides lines of text from the new file that is being compared to
the old file.
This stream is wrapped in a LineNumberReader
object,
unless it is already of this type.
java.lang.NullPointerException
- (unchecked)
Thrown if prev or curr is null.Method Detail |
public static void main(java.lang.String[] args) throws java.io.IOException
Usage
java tribble.text.DifferenceReader oldfile newfile
args
- Command line arguments.
java.io.IOException
- Thrown if an I/O (read) error occurs on either of the input text files.public void close()
Note that this does not close the two input streams, but only disassociates them from this object.
public void setWindowSize(int len)
len
- Number of text lines to look ahead within the old and new input text
streams.public int getDelta() throws java.io.IOException
CMD_XXX
constants.
java.io.IOException
- Thrown if an I/O (read) error occurs on either the old or the new input
streams.getDeltaText()
,
getDeltaLineNumber()
public java.lang.String getDeltaText()
getDelta()
,
getDeltaLineNumber()
public int getDeltaLineNumber()
getDelta()
,
getDeltaText()
protected void finalize() throws java.lang.Throwable
java.lang.Throwable
protected tribble.text.DiffLine readLine(java.io.LineNumberReader in) throws java.io.IOException
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |