patch
Class PatchUtils

java.lang.Object
  extended by patch.PatchUtils

public class PatchUtils
extends java.lang.Object

This Library implements a simple patch algorithm which is able to process the output of diff in normal format.

This class implements the algorithm.

The Method you're probably looking for is PatchUtils.patch(diff, target).

Example usage in comparison to GNU patch:
GNU patch: "patch target < diff"
jPatchLib: "PatchUtils.patch(diff, target"
see http://www.gnu.org/software/diffutils/manual/html_mono/diff.html#Normal

          Copyright (c) 2007 Dominik Schulz

          This file is part of jPatchLib.

          jPatchLib is free software; you can redistribute it and/or modify
          it under the terms of the GNU General Public License as published by
          the Free Software Foundation; either version 2 of the License, or
          (at your option) any later version.

          jPatchLib is distributed in the hope that it will be useful,
          but WITHOUT ANY WARRANTY; without even the implied warranty of
          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
          GNU General Public License for more details.

          You should have received a copy of the GNU General Public License
          along with jPatchLib; if not, write to the Free Software
          Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 

Author:
Dominik

Constructor Summary
PatchUtils()
           
 
Method Summary
private static java.lang.String[] add(int line, java.lang.String[] insertion, java.lang.String[] baseText, int offset)
          Apply an add chunk.
static java.lang.String[] applyChunks(java.lang.String[] targetSrc, Chunk[] chunks)
          Apply the chunks from the chunks array to the target.
private static java.lang.String[] change(int fromLine, int toLine, int to1, int to2, java.lang.String[] changeTo, java.lang.String[] baseText, int offset)
          This applys a chunk of the type change.
private static java.lang.String[] delete(int fromLine, int toLine, java.lang.String[] baseText, int offset)
          This applys a chunk of the type delete
static Chunk[] getChunks(java.lang.String[] patchSrc)
          Get the single chunks from the patch.
static java.lang.String patch(java.lang.String patchText, java.lang.String targetText)
          This is like the patch utility from GNU diff/patch.
static boolean startWithNumber(java.lang.String str)
          Checks if the string starts with a number.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PatchUtils

public PatchUtils()
Method Detail

startWithNumber

public static boolean startWithNumber(java.lang.String str)
Checks if the string starts with a number. Use to check if the string (line) is the beginning of a new chunk.

Parameters:
str - the string to be checked
Returns:
true if the string starts with a number.

getChunks

public static Chunk[] getChunks(java.lang.String[] patchSrc)
Get the single chunks from the patch.

Parameters:
patchSrc - the patch as a string array
Returns:
the array of chunks from the patch file.

applyChunks

public static java.lang.String[] applyChunks(java.lang.String[] targetSrc,
                                             Chunk[] chunks)
Apply the chunks from the chunks array to the target.

Parameters:
targetSrc - the target to be patched
chunks - the chunks to apply
Returns:
The patched targetSrc array.

patch

public static java.lang.String patch(java.lang.String patchText,
                                     java.lang.String targetText)
This is like the patch utility from GNU diff/patch.

Parameters:
patchText - The output from diff
targetText - The text on which the diff will be applied
Returns:
the result of the patching

delete

private static java.lang.String[] delete(int fromLine,
                                         int toLine,
                                         java.lang.String[] baseText,
                                         int offset)
This applys a chunk of the type delete

Parameters:
fromLine - the first line to delete
toLine - the last line to delete
baseText - the text to be patched
offset - the offset. see ... uhm, somewhere else
Returns:
the modified baseText

change

private static java.lang.String[] change(int fromLine,
                                         int toLine,
                                         int to1,
                                         int to2,
                                         java.lang.String[] changeTo,
                                         java.lang.String[] baseText,
                                         int offset)
This applys a chunk of the type change.

Parameters:
fromLine -
toLine -
to1 -
to2 -
changeTo -
baseText -
offset -
Returns:
the modified baseText

add

private static java.lang.String[] add(int line,
                                      java.lang.String[] insertion,
                                      java.lang.String[] baseText,
                                      int offset)
Apply an add chunk.

Parameters:
line -
insertion -
baseText -
offset -
Returns:
the modified baseText