tribble.text
Class DifferenceReader

java.lang.Object
  extended bytribble.text.DifferenceReader

public class DifferenceReader
extends java.lang.Object

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:

keep (CMD_KEEP)
A text line from the old input stream is to be kept and copied as is to the output stream.
delete (CMD_DELETE)
A text line from the old input stream is to be removed and not written to the output stream.
insert (CMD_INSERT)
A text line from the new input stream is to be inserted into the output stream.
end (CMD_END)
Both the old and new input streams have been read completely, and there are no more delta commands to be generated.

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.


Since:
2003-09-06
Version:
$Revision: 1.2 $ $Date: 2004/10/03 18:35:26 $
Author:
David R. Tribble (david@tribble.com).
Copyright ©2003,2004 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.

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

DFL_WINDOW_LEN

public static final int DFL_WINDOW_LEN
Default text line window size (200).

See Also:
Constant Field Values

CMD_END

public static final short CMD_END
Delta command: End of both input streams.

See Also:
Constant Field Values

CMD_KEEP

public static final short CMD_KEEP
Delta command: Keep a line from the old input stream.

See Also:
Constant Field Values

CMD_DELETE

public static final short CMD_DELETE
Delta command: Delete a line from the old input stream.

See Also:
Constant Field Values

CMD_INSERT

public static final short CMD_INSERT
Delta command: Insert a line from the new input stream.

See Also:
Constant Field Values
Constructor Detail

DifferenceReader

public DifferenceReader(java.io.Reader prev,
                        java.io.Reader curr)
Constructor.

Parameters:
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.
Throws:
java.lang.NullPointerException - (unchecked) Thrown if prev or curr is null.
Since:
1.1, 2003-09-06
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Determine the difference between two text files.

Usage

java tribble.text.DifferenceReader oldfile newfile

Parameters:
args - Command line arguments.
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs on either of the input text files.
Since:
1.2, 2003-09-07

close

public void close()
Close this difference generator.

Note that this does not close the two input streams, but only disassociates them from this object.

Since:
1.1, 2003-09-07

setWindowSize

public void setWindowSize(int len)
Establish the size of the text line difference window.

Parameters:
len - Number of text lines to look ahead within the old and new input text streams.
Since:
1.1, 2003-09-06

getDelta

public int getDelta()
             throws java.io.IOException
Generate the next difference delta command.

Returns:
A delta command, which is one of the CMD_XXX constants.
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs on either the old or the new input streams.
Since:
1.1, 2003-09-07
See Also:
getDeltaText(), getDeltaLineNumber()

getDeltaText

public java.lang.String getDeltaText()
Retrieve the text line for the last difference delta command generated.

Returns:
Text line associated with the last delta command.
Since:
1.1, 2003-09-07
See Also:
getDelta(), getDeltaLineNumber()

getDeltaLineNumber

public int getDeltaLineNumber()
Retrieve the line number of the text line for the last difference delta command generated.

Returns:
Line number of the text line associated with the last delta command (where the first line is number 1).
Since:
1.2, 2003-09-07
See Also:
getDelta(), getDeltaText()

finalize

protected void finalize()
                 throws java.lang.Throwable
Finalization.

Throws:
java.lang.Throwable
Since:
1.1, 2003-09-07

readLine

protected tribble.text.DiffLine readLine(java.io.LineNumberReader in)
                                  throws java.io.IOException
Throws:
java.io.IOException