KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > versioning > cookbook > DiffFile


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.versioning.cookbook;
27
28 import org.snipsnap.versioning.ChangeInfo;
29
30 import java.io.*;
31 import java.util.List JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * Returns differences between two files
36  *
37  * @author Stephan J. Schmidt
38  * @version $Id: DiffFile.java 1257 2003-12-11 13:36:55Z leo $
39  */

40
41 public class DiffFile {
42   public static void main(String JavaDoc 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 JavaDoc oldFile = argstrings[0];
49     String JavaDoc newFile = argstrings[1];
50
51     System.out.println(">>>> Difference of file \"" + oldFile +
52                        "\" and file \"" + newFile + "\".\n");
53
54     List JavaDoc result = d.diff(readFromFile(oldFile), readFromFile(newFile));
55     Iterator JavaDoc 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 JavaDoc[] lines = info.getLines();
68       for (int i = 0; i < lines.length; i++) {
69         String JavaDoc 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 JavaDoc readFromFile(String JavaDoc name) {
83     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84     BufferedReader file = null;
85     try {
86       file = new BufferedReader(
87         new InputStreamReader(
88           new FileInputStream(name)));
89
90       String JavaDoc 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