1 16 package org.outerj.daisy.diff; 17 18 import org.eclipse.compare.rangedifferencer.IRangeComparator; 19 20 import java.io.BufferedReader ; 21 import java.io.StringReader ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 25 28 public class TextComparator implements IRangeComparator { 29 private String [] lines; 30 31 34 public TextComparator(String text) { 35 BufferedReader reader = new BufferedReader (new StringReader (text)); 36 37 ArrayList lines = new ArrayList (50); 38 try { 39 String line = reader.readLine(); 40 while (line != null) { 41 lines.add(line); 42 line = reader.readLine(); 43 } 44 } catch (IOException e) { 45 throw new RuntimeException ("Unexpected: got exception while reading from String object.", e); 46 } 47 this.lines = (String [])lines.toArray(new String [lines.size()]); 48 } 49 50 public int getRangeCount() { 51 return lines.length; 52 } 53 54 public boolean rangesEqual(int thisIndex, IRangeComparator other, int otherIndex) { 55 String thisLine = getLine(thisIndex); 56 String otherLine = ((TextComparator)other).getLine(otherIndex); 57 return thisLine.equals(otherLine); 58 } 59 60 public boolean skipRangeComparison(int length, int maxLength, IRangeComparator other) { 61 return false; 62 } 63 64 public String getLine(int index) { 65 return index < lines.length ? lines[index] : ""; 66 } 67 } 68 | Popular Tags |