1 25 26 package org.snipsnap.versioning.cookbook; 27 28 import org.snipsnap.versioning.ChangeInfo; 29 30 import java.io.*; 31 import java.util.List ; 32 import java.util.Iterator ; 33 34 40 41 public class DiffFile { 42 public static void main(String argstrings[]) { 43 if (argstrings.length != 2) { 44 System.err.println("Usage: diff oldfile newfile"); 45 System.exit(1); 46 } 47 CookbookDiff d = new CookbookDiff(); 48 String oldFile = argstrings[0]; 49 String newFile = argstrings[1]; 50 51 System.out.println(">>>> Difference of file \"" + oldFile + 52 "\" and file \"" + newFile + "\".\n"); 53 54 List result = d.diff(readFromFile(oldFile), readFromFile(newFile)); 55 Iterator iterator = result.iterator(); 56 while (iterator.hasNext()) { 57 ChangeInfo info = (ChangeInfo) iterator.next(); 58 if (ChangeInfo.DELETE.equals(info.getType())) { 59 System.out.println("DELETE AT " + info.getFrom()); 60 } else if (ChangeInfo.INSERT.equals(info.getType())) { 61 System.out.println("INSERT BEFORE " + info.getFrom()); 62 } else if (ChangeInfo.CHANGE.equals(info.getType())) { 63 System.out.println("CHANGE AT " + info.getFrom()); 64 } else if (ChangeInfo.MOVE.equals(info.getType())) { 65 System.out.println("MOVE FROM " + info.getFrom() + " THROUGH "+(info.getFrom()+info.getSize()-1)+" TO "+ info.getTo()); 66 } 67 String [] lines = info.getLines(); 68 for (int i = 0; i < lines.length; i++) { 69 String line = lines[i]; 70 System.out.println(" "+line); 71 } 72 } 73 74 if (result.size() != 0) { 75 System.out.println(">>>> End of differences."); 76 } else { 77 System.out.println(">>>> Files are identical."); 78 } 79 return; 80 } 81 82 public static String readFromFile(String name) { 83 StringBuffer buffer = new StringBuffer (); 84 BufferedReader file = null; 85 try { 86 file = new BufferedReader( 87 new InputStreamReader( 88 new FileInputStream(name))); 89 90 String lineBuffer; 91 while ((lineBuffer = file.readLine()) != null) { 92 buffer.append(lineBuffer); 93 buffer.append("\n"); 94 } 95 } catch (IOException e) { 96 System.err.println("Diff can't read file " + 97 name); 98 System.err.println("Error Exception was:" + e); 99 System.exit(1); 100 } 101 return buffer.toString(); 102 } 103 } 104 | Popular Tags |