KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > LineDiff


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * LineDiff.java
22  *
23  * Created on March 28, 2002, 9:49 AM
24  */

25
26 package org.netbeans.test.editor;
27
28 import org.netbeans.junit.diff.Diff;
29 import java.io.File JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.LineNumberReader JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.PrintStream JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  *
40  * @author jlahoda, ehucka
41  */

42 public class LineDiff implements Diff {
43
44     private boolean ignoreCase;
45
46     /** Creates a new instance of LineDiff */
47     public LineDiff(boolean ignoreCase) {
48         this.ignoreCase = ignoreCase;
49     }
50     
51     public boolean getIgnoreCase() {
52         return ignoreCase;
53     }
54     
55     /**
56      * @param l1 first line to compare
57      * @param l2 second line to compare
58      * @return true if lines equal
59      */

60     private boolean compareLines(String JavaDoc l1,String JavaDoc l2) {
61         if (getIgnoreCase()) {
62             if (l1.equalsIgnoreCase(l2))
63                 return true;
64         } else {
65             if (l1.equals(l2))
66                 return true;
67         }
68         return false;
69     }
70     
71     /**
72      * @param first first file to compare
73      * @param second second file to compare
74      * @param diff difference file, caller can pass null value, when results are not needed.
75      * @return true iff files differ
76      */

77     public boolean diff(String JavaDoc first, String JavaDoc second, String JavaDoc diff) throws java.io.IOException JavaDoc {
78         File JavaDoc fFirst = new File JavaDoc(first);
79         File JavaDoc fSecond = new File JavaDoc(second);
80         File JavaDoc fDiff = null != diff ? new File JavaDoc(diff) : null;
81         return diff(fFirst, fSecond, fDiff);
82     }
83     
84     /**
85      * @param first first file to compare
86      * @param second second file to compare
87      * @param diff difference file, caller can pass null value, when results are not needed.
88      * @return true iff files differ
89      */

90     public boolean diff(java.io.File JavaDoc firstFile, java.io.File JavaDoc secondFile, java.io.File JavaDoc diffFile) throws java.io.IOException JavaDoc {
91         LineNumberReader JavaDoc first = new LineNumberReader JavaDoc(new FileReader JavaDoc(firstFile));
92         LineNumberReader JavaDoc second = new LineNumberReader JavaDoc(new FileReader JavaDoc(secondFile));
93         String JavaDoc firstLine;
94         String JavaDoc secondLine;
95         
96         if (diffFile == null) {
97             while ((firstLine = first.readLine()) != null) {
98                 secondLine = second.readLine();
99                 if (secondLine == null) {
100                     first.close();
101                     second.close();
102                     return true;
103                 }
104                 if (!compareLines(firstLine,secondLine)) {
105                     first.close();
106                     second.close();
107                     return true;
108                 }
109             }
110         } else {
111             ArrayList JavaDoc a1,a2,newLines,missingLines;
112             
113             a1=new ArrayList JavaDoc();
114             while ((firstLine = first.readLine()) != null) {
115                 a1.add(firstLine);
116             }
117             a2=new ArrayList JavaDoc();
118             while ((secondLine = second.readLine()) != null) {
119                 a2.add(secondLine);
120             }
121             first.close();
122             second.close();
123             newLines=new ArrayList JavaDoc();
124             missingLines=new ArrayList JavaDoc();
125             
126             int j=0,bj;
127             boolean found;
128             
129             for (int i=0;i < a1.size();i++) {
130                 if (j >= a2.size()) {
131                     for (int k=i;k < a1.size();k++) {
132                         missingLines.add(k+"> "+a1.get(k));
133                     }
134                     break;
135                 }
136                 firstLine=(String JavaDoc)(a1.get(i));
137                 secondLine=(String JavaDoc)(a2.get(j));
138                 if (!compareLines(firstLine,secondLine)) {
139                     found=false;
140                     for (int k=j;k < a2.size();k++) {
141                         secondLine = (String JavaDoc)(a2.get(k));
142                         if (compareLines(firstLine,secondLine)) {
143                             for (int l=j;l < k;l++) {
144                                 newLines.add(l+"> "+a2.get(l));
145                             }
146                             j=k;
147                             found=true;
148                             break;
149                         }
150                     }
151                     if (!found) {
152                         missingLines.add(i+"> "+firstLine);
153                         j--;
154                     }
155                 }
156                 j++;
157             }
158             if (j < a2.size()) {
159                 for (int i=j;i < a2.size();i++) {
160                     newLines.add(i+"> "+a2.get(i));
161                 }
162             }
163             
164             if (missingLines.size() > 0 || newLines.size() > 0) {
165                 PrintStream JavaDoc pw=null;
166                 pw=new PrintStream JavaDoc(new FileOutputStream JavaDoc(diffFile));
167                 //pw=System.out;
168
if (missingLines.size() > 0) {
169                     pw.println("-----------------------------Missing Lines:-----");
170                     for (int i=0;i < missingLines.size();i++) {
171                         pw.println(missingLines.get(i));
172                     }
173                 }
174                 if (newLines.size() > 0) {
175                     pw.println("-----------------------------New Lines:---------");
176                     for (int i=0;i < newLines.size();i++) {
177                         pw.println(newLines.get(i));
178                     }
179                 }
180                 pw.close();
181                 return true;
182             }
183         }
184         return false;
185     }
186     
187     public static void main(String JavaDoc[] argv) {
188         try {
189             LineDiff diff=new LineDiff(true);
190             diff.diff("/tmp/diff/test.pass","/tmp/diff/test.ref","/tmp/diff/test.diff");
191         } catch (Exception JavaDoc ex) {
192             ex.printStackTrace();
193         }
194     }
195 }
196
Popular Tags