From c2e5f54e8485a57a9d5d06954d5fb0f07e94b82a Mon Sep 17 00:00:00 2001 From: Google Code Exporter Date: Tue, 19 May 2015 12:24:07 -0400 Subject: [PATCH] Migrating wiki contents from Google Code --- ProjectHome.md | 49 ++++++++++++++++++++++++++ SampleUsage.md | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 ProjectHome.md create mode 100644 SampleUsage.md diff --git a/ProjectHome.md b/ProjectHome.md new file mode 100644 index 0000000..1649ac3 --- /dev/null +++ b/ProjectHome.md @@ -0,0 +1,49 @@ +Diff Utils library is an OpenSource library for performing the comparison operations between texts: computing diffs, applying patches, generating unified diffs or parsing them, generating diff output for easy future displaying (like side-by-side view) and so on. + +Main reason to build this library was the lack of easy-to-use libraries with all the usual stuff you need while working with diff files. Originally it was inspired by JRCS library and it's nice design of diff module. + +## Main Features ## + + * computing the difference between two texts. + * capable to hand more than plain ascci. Arrays or List of any type that implements hashCode() and equals() correctly can be subject to differencing using this library + * patch and unpatch the text with the given patch + * parsing the unified diff format + * producing human-readable differences + +### Algoritms ### + +This library implements Myer's diff algorithm. But it can easily replaced by any other which is better for handing your texts. I have plan to add implementation of some in future. + +### Changelog ### + + * Version 1.2 + * JDK 1.5 compatibility + * Ant build script + * Generate output in unified diff format (thanks for Bill James) + +### To Install ### + +Just add the code below to your maven dependencies: +``` + + com.googlecode.java-diff-utils + diffutils + 1.2.1 + +``` + +And for Ivy: +``` + +``` + +## Coming eventually ## + + * support for inline diffs in output + * helpers for showing side-by-side, line-by-line diffs or text with inter-line and intra-line change highlights + * customization of diff algorithm for better experience while computing diffs between strings (ignoring blank lines or spaces, etc) + * generating output in other formats (not only unified). E.g. CVS. + +### Tutorials ### + +http://www.adictosaltrabajo.com/tutoriales/tutoriales.php?pagina=CompararFicherosJavaDiffUtils (in Spanish). Thanks Miguel \ No newline at end of file diff --git a/SampleUsage.md b/SampleUsage.md new file mode 100644 index 0000000..d554b86 --- /dev/null +++ b/SampleUsage.md @@ -0,0 +1,93 @@ +# Introduction # + +The all common operation of this library is available through static methods of DiffUtils object. These methods are: + + * `DiffUtils.diff(List original, List revised): Patch patch` + * `DiffUtils.patch(List original, Patch patch): List revised` + * `DiffUtils.unpatch(List revised, Patch patch): List original` + * `DiffUtils.parseUnifiedDiff(List diff): Patch patch` + * `DiffUtils.getDiffRows(List original, List revised): List diffRows` + +NOTE: All up-to-date examples can be found [here](http://code.google.com/p/java-diff-utils/source/browse/#svn%2Ftrunk%2Ftest%2Fexamples%2Fdiffutils). + +### Basic use in a Java App ### + +## Task 1: Compute the difference between to files and print its deltas ## + +**Solution:** + +``` +import difflib.*; + +public class BasicJavaApp_Task1 { + // Helper method for get the file content + private static List fileToLines(String filename) { + List lines = new LinkedList(); + String line = ""; + try { + BufferedReader in = new BufferedReader(new FileReader(filename)); + while ((line = in.readLine()) != null) { + lines.add(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + return lines; + } + + public static void main(String[] args) { + List original = fileToLines("originalFile.txt"); + List revised = fileToLines("revisedFile.xt"); + + // Compute diff. Get the Patch object. Patch is the container for computed deltas. + Patch patch = DiffUtils.diff(original, revised); + + for (Delta delta: patch.getDeltas()) { + System.out.println(delta); + } + } +} +``` + + +## Task 2: Get the file in unified format and apply it as the patch to given text ## + +**Solution:** + +``` +import difflib.*; + +public class BasicJavaApp_Task2 { + private List originalList = Arrays.asList("aaa", "bbb", "ccc"); + + // Helper method for get the file content + private List fileToLines(String filename) { + List lines = new LinkedList(); + String line = ""; + try { + BufferedReader in = new BufferedReader(new FileReader(filename)); + while ((line = in.readLine()) != null) { + lines.add(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + return lines; + } + + public static void main(String[] args) { + // At first, parse the unified diff file and get the patch + Patch patch = DiffUtils.parseUnifiedDiff(fileToLines("example.diff")); + + // Then apply the computed patch to the given text + List result = DiffUtils.patch(original, patch); + /// Or we can call patch.applyTo(original). There is no difference. + } +} +``` + +## Task 3: Compute the difference between to texts and print it in human-readable side-by-side view ## + +**Solution:** + +_Coming soon..._ \ No newline at end of file