1 19 20 25 26 package org.netbeans.test.editor; 27 28 import org.netbeans.junit.diff.Diff; 29 import java.io.File ; 30 import java.io.FileOutputStream ; 31 import java.io.LineNumberReader ; 32 import java.io.FileReader ; 33 import java.io.FileWriter ; 34 import java.io.PrintStream ; 35 import java.io.PrintWriter ; 36 import java.util.ArrayList ; 37 38 42 public class LineDiff implements Diff { 43 44 private boolean ignoreCase; 45 46 47 public LineDiff(boolean ignoreCase) { 48 this.ignoreCase = ignoreCase; 49 } 50 51 public boolean getIgnoreCase() { 52 return ignoreCase; 53 } 54 55 60 private boolean compareLines(String l1,String 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 77 public boolean diff(String first, String second, String diff) throws java.io.IOException { 78 File fFirst = new File (first); 79 File fSecond = new File (second); 80 File fDiff = null != diff ? new File (diff) : null; 81 return diff(fFirst, fSecond, fDiff); 82 } 83 84 90 public boolean diff(java.io.File firstFile, java.io.File secondFile, java.io.File diffFile) throws java.io.IOException { 91 LineNumberReader first = new LineNumberReader (new FileReader (firstFile)); 92 LineNumberReader second = new LineNumberReader (new FileReader (secondFile)); 93 String firstLine; 94 String 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 a1,a2,newLines,missingLines; 112 113 a1=new ArrayList (); 114 while ((firstLine = first.readLine()) != null) { 115 a1.add(firstLine); 116 } 117 a2=new ArrayList (); 118 while ((secondLine = second.readLine()) != null) { 119 a2.add(secondLine); 120 } 121 first.close(); 122 second.close(); 123 newLines=new ArrayList (); 124 missingLines=new ArrayList (); 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 )(a1.get(i)); 137 secondLine=(String )(a2.get(j)); 138 if (!compareLines(firstLine,secondLine)) { 139 found=false; 140 for (int k=j;k < a2.size();k++) { 141 secondLine = (String )(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 pw=null; 166 pw=new PrintStream (new FileOutputStream (diffFile)); 167 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 [] 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 ex) { 192 ex.printStackTrace(); 193 } 194 } 195 } 196 | Popular Tags |