KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > SimpleDiff


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.SimpleDiff
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 //SimpleDiff.java
23
package org.apache.derbyTesting.functionTests.harness;
24
25 import java.io.IOException JavaDoc;
26 import java.io.BufferedInputStream JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.BufferedReader JavaDoc;
31 import java.io.FileReader JavaDoc;
32
33 public class SimpleDiff
34 {
35
36     PrintWriter JavaDoc pw;
37
38     boolean debugOn = Boolean.getBoolean("simplediff.debug");
39     int debugLevel = 1;
40
41     boolean diffsFound = true;
42
43     int lookAhead = 20;
44
45     public void debug(int level, String JavaDoc msg)
46     {
47         if (debugLevel >= level)
48         {
49             debug(msg);
50         }
51     }
52     public void debug(String JavaDoc msg)
53     {
54         if (debugOn)
55         {
56             System.out.println("DEBUG: " + msg);
57         }
58     }
59
60     int lineCount(String JavaDoc file) throws IOException JavaDoc
61     {
62         BufferedReader JavaDoc input = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
63         int count = 0;
64         String JavaDoc aLine = input.readLine();
65         while (aLine != null)
66         {
67             count++;
68             aLine = input.readLine();
69         }
70         input.close();
71         return count;
72     }
73
74
75     public String JavaDoc[] readFile(BufferedReader JavaDoc input) throws IOException JavaDoc
76     {
77
78         Vector JavaDoc vec = new Vector JavaDoc();
79
80         String JavaDoc aLine = "";
81         //int count = 0;
82
aLine = input.readLine();
83         while (aLine != null)
84         {
85             vec.addElement(aLine);
86             //count++;
87
aLine = input.readLine();
88         }
89         input.close();
90
91         String JavaDoc rV[] = new String JavaDoc[vec.size()];
92         //debug(2, ""+count + " lines in " + filename);
93
debug(2, ""+vec.size() + " lines in input");
94         vec.copyInto(rV);
95
96         return rV;
97     }
98
99     public void printFile(String JavaDoc file[])
100     {
101         for (int i = 0; i < file.length; i++)
102         {
103             pw.println(i + ": " + file[i]);
104             System.out.println(i + ": " + file[i]);
105         }
106     }
107
108     public static String JavaDoc usage = "java SimpleDiff <file1> <file2>";
109
110     /**
111         @param filename Name of file to be read
112         @return String array of file contents
113         @exception IOException thrown by underlying calls
114             to the file system
115     */

116
117     public String JavaDoc[] diffFiles( DiffBuffer file1,
118                                 DiffBuffer file2)
119                                 throws IOException JavaDoc
120     {
121
122         int currentLine1 = 0;
123         int currentLine2 = 0;
124         Vector JavaDoc returnVec = new Vector JavaDoc();
125
126         while ( file1.isValidOffset(currentLine1) &&
127                 file2.isValidOffset(currentLine2))
128         {
129             String JavaDoc f1 = file1.lineAt(currentLine1);
130             String JavaDoc f2 = file2.lineAt(currentLine2);
131
132             if (f1.equals(f2))
133             {
134                 debug(1, currentLine1 + ": match");
135                 currentLine1++;
136                 currentLine2++;
137                 file1.setLowWater(currentLine1);
138                 file2.setLowWater(currentLine2);
139             }
140             else
141             {
142                 boolean foundMatch = false;
143                 int checkLine2 = currentLine2;
144                 int checkCount = 1;
145 // while ( (currentLine2 + checkCount) < (file2Length - 1) &&
146
while ( file2.isValidOffset(currentLine2 + checkCount) &&
147                         checkCount < lookAhead)
148                 {
149                     debug(1, "currentLine1 " + currentLine1 + " currentLine2 " + (currentLine2 +checkCount));
150                     debug(1, "about to reference file2[" + (currentLine2 + checkCount) + "]");
151                     f2 = file2.lineAt(currentLine2 + checkCount);
152                     debug(2, "did");
153                     if (f1.equals(f2))
154                     {
155                         foundMatch = true;
156                         if (checkCount > 1)
157                         {
158                             returnVec.addElement(currentLine1 + "a" + (currentLine2 + 1) + "," + (currentLine2 + checkCount));
159                         }
160                         else
161                         {
162                             returnVec.addElement(currentLine1 + "a" + (currentLine2 + 1));
163                         }
164
165                         for (int j = 0; j < checkCount; j++)
166                         {
167                             returnVec.addElement("> " +
168                                   file2.lineAt(currentLine2 + j) );
169                         }
170                         currentLine2 = currentLine2 + checkCount;
171                         checkCount = 0;
172
173                         // This break statement was commented out, which
174
// caused problems in the diff output. I don't
175
// know why it was commented out, and uncommenting
176
// it fixed the problem.
177
//
178
// - Jeff Lichtman
179
// March 24, 1999
180
break;
181                     }
182                     checkCount ++;
183                 }
184                 if (!foundMatch && file2.isValidOffset(currentLine2))
185                 {
186                     int checkLine1 = currentLine1;
187                     checkCount = 1;
188                     f2 = file2.lineAt(currentLine2);
189                     while ( file1.isValidOffset(currentLine1 + checkCount) &&
190                             checkCount < lookAhead)
191                     {
192                         debug(1, "currentLine2 " + currentLine2 + " currentLine1 " + (currentLine1 + checkCount));
193                         f1 = file1.lineAt(currentLine1 + checkCount);
194                         if ( f2.equals(f1))
195                         {
196                             foundMatch = true;
197                             if (checkCount > 1)
198                             {
199                                 returnVec.addElement((currentLine1 + 1) + "," + (currentLine1 + checkCount) + "d" + currentLine2);
200                             }
201                             else
202                             {
203                                 returnVec.addElement((currentLine1 + 1) + "d" + currentLine2);
204                             }
205
206                             for (int j = 0; j < checkCount; j++)
207                             {
208                                 returnVec.addElement("< " +
209                                       file1.lineAt(currentLine1 + j) );
210
211                             }
212                             currentLine1 = currentLine1 + checkCount;
213                             checkCount = 0;
214                             debug(1, "continuing");
215                             break;
216                         }
217                         checkCount ++;
218                     }
219
220                 }
221                 if (!foundMatch)
222                 {
223                     debug(1, currentLine1 + ": NOMATCH");
224                     returnVec.addElement((currentLine1 + 1) +" del");// + (currentLine2 + 1));
225
returnVec.addElement("< " + file1.lineAt(currentLine1));
226                     currentLine1++;
227                 }
228                 else
229                 {
230                     currentLine1++;
231                     currentLine2++;
232                     file1.setLowWater(currentLine1);
233                     file2.setLowWater(currentLine2);
234                 }
235             }
236         }
237
238         if (file1.isValidOffset(currentLine1))
239         {
240             returnVec.addElement((currentLine2) + " del");
241             for (int i = currentLine1; file1.isValidOffset(i); i++)
242             {
243                 returnVec.addElement("< " + file1.lineAt(i));
244             }
245         }
246         if (file2.isValidOffset(currentLine2))
247         {
248             returnVec.addElement((currentLine1) + " add");
249             for (int i = currentLine2; file2.isValidOffset(i); i++)
250             {
251                 returnVec.addElement("> " + file2.lineAt(i));
252             }
253         }
254
255         file1.close();
256         file2.close();
257
258         if (returnVec.size() == 0)
259         {
260             return null;
261         }
262
263
264         String JavaDoc [] returnArray = new String JavaDoc[returnVec.size()];
265         returnVec.copyInto(returnArray);
266         return returnArray;
267
268
269     }
270
271     private void reportMemory()
272     {
273         reportMemory(null);
274     }
275
276     private void reportMemory(String JavaDoc header)
277     {
278         if (header != null)
279         {
280             System.out.println(header);
281         }
282         long free = Runtime.getRuntime().freeMemory();
283         long total = Runtime.getRuntime().totalMemory();
284
285         System.out.println("total: " + total );
286         System.out.println("free: " + free );
287         System.out.println("used: " + (total - free));
288         System.gc();
289         System.out.println("used: <postgc> " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
290         System.out.println(" ");
291     }
292
293     public boolean doWork(BufferedReader JavaDoc in1, BufferedReader JavaDoc in2, PrintWriter JavaDoc localPW) throws IOException JavaDoc
294     {
295         this.pw=(localPW==null?new PrintWriter JavaDoc(System.out):localPW);
296         try
297         {
298             DiffBuffer db1 = new DiffBuffer(in1, "1");
299             DiffBuffer db2 = new DiffBuffer(in2, "2");
300
301             String JavaDoc diffs[] = diffFiles(db1, db2);
302
303             if (diffs == null)
304             {
305                 debug(1, "no diff");
306                 return false;
307             }
308             else
309             {
310                 for (int i = 0; i < diffs.length; i++)
311                 {
312                     this.pw.println(diffs[i]);
313                     System.out.println(diffs[i]);
314                 }
315             }
316
317         }
318         catch (IOException JavaDoc ioe)
319         {
320             System.err.println("IOException comparing <" + in1 +
321                 "> and <" + in2 + ">");
322             System.err.println(ioe);
323
324             this.pw.println("IOException comparing <" + in1 +
325                 "> and <" + in2 + ">");
326             this.pw.println(ioe);
327         }
328         return true;
329     }
330     public static void main(String JavaDoc args[]) throws IOException JavaDoc
331     {
332
333         if (args.length < 2)
334         {
335             System.err.println("Invalid number of arguments");
336             System.err.println("Usage: " + usage);
337             System.exit(1);
338         }
339
340         SimpleDiff me = new SimpleDiff();
341
342         try
343         {
344             BufferedReader JavaDoc br1 = new BufferedReader JavaDoc(new FileReader JavaDoc(args[0]));
345             BufferedReader JavaDoc br2 = new BufferedReader JavaDoc(new FileReader JavaDoc(args[1]));
346             me.doWork(br1, br2, null);
347         }
348         catch (IOException JavaDoc ioe)
349         {
350             System.out.println("IOExeption: " + ioe);
351         }
352     }
353
354     public void pause()
355     {
356         BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(System.in);
357         try
358         {
359             bis.read();
360         }
361         catch (IOException JavaDoc ioe)
362         {
363             pw.println("Error trying to pause...");
364             System.out.println("Error trying to pause...");
365         }
366     }
367
368     class DiffBuffer extends Vector JavaDoc
369     {
370
371
372         public boolean atEOF()
373         {
374             return atEnd;
375         }
376
377         public DiffBuffer(BufferedReader JavaDoc rb)
378         {
379             this(rb, "");
380         }
381
382         public DiffBuffer(BufferedReader JavaDoc rb, String JavaDoc name)
383         {
384             this (rb, name, 1024);
385         }
386
387         public DiffBuffer(BufferedReader JavaDoc rb, String JavaDoc name, int size)
388         {
389             super(size);
390             readBuffer = rb;
391             currentLowWater = 0;
392             currentHighWater = -1;
393             oldLow = 0;
394             myName = name;
395             atEnd = false;
396         }
397
398         public boolean isValidOffset(int lineNumber) throws IOException JavaDoc
399         {
400             if (atEnd)
401             {
402                 return lineNumber <= actualEndOfFile;
403             }
404
405             if (lineAt(lineNumber) == null)
406             {
407                 return false;
408             }
409             return true;
410         }
411
412         public String JavaDoc lineAt(int offset) throws IOException JavaDoc
413         {
414 /*
415 System.out.println("offset: " + offset);
416 System.out.println("currentHighWater: " + currentHighWater);
417 System.out.println("");
418 */

419             if (offset > currentHighWater)
420             {
421                 for (int i = 0; i < offset - currentHighWater; i++)
422                 {
423                     String JavaDoc aLine = readBuffer.readLine();
424                     addElement(aLine);
425 /*
426 System.out.println("aLine: " + aLine);
427 */

428                     if (aLine == null)
429                     {
430                         if (!atEnd)
431                         {
432                             //first time we've tried to read past the EOF
433
actualEndOfFile = currentHighWater + i;
434 //System.out.println(myName + ": length " + actualEndOfFile);
435
atEnd = true;
436                         }
437                     }
438                 }
439                 currentHighWater = offset;
440             }
441             return (String JavaDoc) elementAt(offset);
442         }
443
444
445         public final String JavaDoc EMPTY = null;
446         protected BufferedReader JavaDoc readBuffer;
447         protected int currentLowWater;
448         protected int currentHighWater;
449         private int oldLow;
450         protected String JavaDoc myName;
451         protected boolean atEnd;
452         protected int actualEndOfFile;
453         /**
454             Useful to keep memory requirements low
455          */

456         public void setLowWater(int newLow)
457         {
458
459             for (int i = oldLow; i < newLow; i++)
460             {
461                 setElementAt(EMPTY, i);
462             }
463             currentLowWater = newLow;
464             oldLow = newLow -1;
465         }
466
467
468     public void iterate(boolean verbose)
469     {
470         int nulls = 0;
471         int nonnulls = 0;
472
473
474         for (int i = 0; i < this.size(); i++)
475         {
476             if (elementAt(i) == null)
477             {
478                 if (verbose)
479                 {
480                     System.out.print("[" + i + "] ");
481                     System.out.println("null");
482                 }
483                 nulls++;
484
485             }
486             else
487             {
488                 if (verbose)
489                 {
490                     System.out.print("[" + i + "] ");
491                     System.out.println("NotNULL");
492                 }
493                 nonnulls++;
494             }
495         }
496         System.out.println("nulls: " + nulls + " nonNull: " + nonnulls);
497     }
498
499     public void close() throws IOException JavaDoc
500     {
501 // System.out.println("Closing BufferedReader");
502
readBuffer.close();
503         readBuffer = null;
504     }
505     }
506
507 }
508
Popular Tags