1 11 package org.eclipse.compare.internal; 12 13 import org.eclipse.jface.text.*; 14 import org.eclipse.compare.contentmergeviewer.ITokenComparator; 15 import org.eclipse.compare.rangedifferencer.IRangeComparator; 16 17 25 public class DocLineComparator implements ITokenComparator { 26 27 private IDocument fDocument; 28 private int fLineOffset; 29 private int fLineCount; 30 private int fLength; 31 private boolean fIgnoreWhiteSpace; 32 33 42 public DocLineComparator(IDocument document, IRegion region, boolean ignoreWhiteSpace) { 43 44 fDocument= document; 45 fIgnoreWhiteSpace= ignoreWhiteSpace; 46 47 fLineOffset= 0; 48 if (region != null) { 49 fLength= region.getLength(); 50 int start= region.getOffset(); 51 try { 52 fLineOffset= fDocument.getLineOfOffset(start); 53 } catch (BadLocationException ex) { 54 } 56 57 if (fLength == 0) 58 fLineCount= 0; 59 else { 60 int endLine= fDocument.getNumberOfLines(); 61 try { 62 endLine= fDocument.getLineOfOffset(start + fLength); 63 } catch (BadLocationException ex) { 64 } 66 fLineCount= endLine - fLineOffset + 1; 67 } 68 69 } else { 70 fLength= document.getLength(); 71 fLineCount= fDocument.getNumberOfLines(); 72 } 73 } 74 75 80 public int getRangeCount() { 81 return fLineCount; 82 } 83 84 87 public int getTokenStart(int line) { 88 try { 89 IRegion r= fDocument.getLineInformation(fLineOffset + line); 90 return r.getOffset(); 91 } catch (BadLocationException ex) { 92 return fDocument.getLength(); 93 } 94 } 95 96 100 public int getTokenLength(int line) { 101 return getTokenStart(line+1) - getTokenStart(line); 102 } 103 104 113 public boolean rangesEqual(int thisIndex, IRangeComparator otherComparator, int otherIndex) { 114 115 if (otherComparator != null && otherComparator.getClass() == getClass()) { 116 DocLineComparator other= (DocLineComparator) otherComparator; 117 118 if (fIgnoreWhiteSpace) { 119 String s1= extract(thisIndex); 120 String s2= other.extract(otherIndex); 121 return compare(s1, s2); 123 } 124 125 int tlen= getTokenLength(thisIndex); 126 int olen= other.getTokenLength(otherIndex); 127 if (tlen == olen) { 128 String s1= extract(thisIndex); 129 String s2= other.extract(otherIndex); 130 return s1.equals(s2); 131 } 132 } 133 return false; 134 } 135 136 146 public boolean skipRangeComparison(int length, int maxLength, IRangeComparator other) { 147 return false; 148 } 149 150 152 158 private String extract(int line) { 159 if (line < fLineCount) { 160 try { 161 IRegion r= fDocument.getLineInformation(fLineOffset + line); 162 return fDocument.get(r.getOffset(), r.getLength()); 163 } catch(BadLocationException e) { 164 } 166 } 167 return ""; } 169 170 private boolean compare(String s1, String s2) { 171 int l1= s1.length(); 172 int l2= s2.length(); 173 int c1= 0, c2= 0; 174 int i1= 0, i2= 0; 175 176 while (c1 != -1) { 177 178 c1= -1; 179 while (i1 < l1) { 180 char c= s1.charAt(i1++); 181 if (! Character.isWhitespace(c)) { 182 c1= c; 183 break; 184 } 185 } 186 187 c2= -1; 188 while (i2 < l2) { 189 char c= s2.charAt(i2++); 190 if (! Character.isWhitespace(c)) { 191 c2= c; 192 break; 193 } 194 } 195 196 if (c1 != c2) 197 return false; 198 } 199 return true; 200 } 201 } 202 203 | Popular Tags |