1 11 package org.eclipse.compare.internal.patch; 12 13 import java.io.InputStream ; 14 import java.util.List ; 15 16 import org.eclipse.compare.patch.IHunk; 17 import org.eclipse.compare.patch.PatchConfiguration; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 21 public class HunkResult implements IHunk { 22 23 private static final boolean DEBUG= false; 24 25 private Hunk fHunk; 26 private boolean fMatches; 27 private int fShift; 28 29 private final FileDiffResult fDiffResult; 30 31 36 public HunkResult(FileDiffResult diffResult, Hunk hunk) { 37 fDiffResult = diffResult; 38 fHunk = hunk; 39 } 40 41 48 public boolean patch(List lines) { 49 fMatches = false; 50 PatchConfiguration configuration = getConfiguration(); 51 if (isEnabled(configuration)) { 52 if (fHunk.tryPatch(configuration, lines, fShift)) { 53 fShift+= fHunk.doPatch(configuration, lines, fShift); 54 fMatches = true; 55 } else { 56 boolean found= false; 57 int oldShift= fShift; 58 59 for (int i= 1; i <= fDiffResult.getFuzz(); i++) { 60 if (fHunk.tryPatch(configuration, lines, fShift-i)) { 61 if (isAdjustShift()) 62 fShift-= i; 63 found= true; 64 break; 65 } 66 } 67 68 if (! found) { 69 for (int i= 1; i <= fDiffResult.getFuzz(); i++) { 70 if (fHunk.tryPatch(configuration, lines, fShift+i)) { 71 if (isAdjustShift()) 72 fShift+= i; 73 found= true; 74 break; 75 } 76 } 77 } 78 79 if (found) { 80 if (DEBUG) System.out.println("patched hunk at offset: " + (fShift-oldShift)); fShift+= fHunk.doPatch(configuration, lines, fShift); 82 fMatches = true; 83 } 84 } 85 } 86 return fMatches; 87 } 88 89 private boolean isAdjustShift() { 90 return true; 91 } 92 93 private PatchConfiguration getConfiguration() { 94 return getDiffResult().getConfiguration(); 95 } 96 97 103 public int calculateFuzz(List lines, IProgressMonitor monitor) { 104 105 fMatches= false; 106 int fuzz = 0; 107 PatchConfiguration configuration = getConfiguration(); 108 if (fHunk.tryPatch(configuration, lines, fShift)) { 109 fShift+= fHunk.doPatch(configuration, lines, fShift); 110 fMatches = true; 111 } else { 112 int hugeFuzz= lines.size(); fuzz= -1; 115 for (int i= 1; i <= hugeFuzz; i++) { 116 if (monitor.isCanceled()) { 117 throw new OperationCanceledException(); 118 } 119 if (fHunk.tryPatch(configuration, lines, fShift-i)) { 120 fuzz= i; 121 if (isAdjustShift()) 122 fShift-= i; 123 fMatches= true; 124 break; 125 } 126 } 127 128 if (! fMatches) { 129 for (int i= 1; i <= hugeFuzz; i++) { 130 if (monitor.isCanceled()) { 131 throw new OperationCanceledException(); 132 } 133 if (fHunk.tryPatch(configuration, lines, fShift+i)) { 134 fuzz= i; 135 if (isAdjustShift()) 136 fShift+= i; 137 fMatches= true; 138 break; 139 } 140 } 141 } 142 143 if (fMatches) 144 fShift+= fHunk.doPatch(configuration, lines, fShift); 145 } 146 return fuzz; 147 } 148 149 155 public int getShift() { 156 return fShift; 157 } 158 159 165 public void setShift(int shift) { 166 fShift = shift; 167 } 168 169 173 public Hunk getHunk() { 174 return fHunk; 175 } 176 177 181 public FileDiffResult getDiffResult() { 182 return fDiffResult; 183 } 184 185 189 public boolean isOK() { 190 return fMatches; 191 } 192 193 200 public String getContents(boolean afterState, boolean fullContext) { 201 if (fullContext) { 202 boolean problemFound = false; 203 List lines = getDiffResult().getBeforeLines(); 204 if (afterState) { 205 if (isOK()) { 206 int oldShift = fShift; 207 try { 208 fShift = 0; 209 problemFound = !patch(lines); 210 } finally { 211 fShift = oldShift; 212 } 213 } else { 214 problemFound = true; 215 } 216 } 217 if (!problemFound) 219 return Patcher.createString(fDiffResult.isPreserveLineDelimeters(), lines); 220 } 221 return getHunk().getContents(afterState, getConfiguration().isReversed()); 222 } 223 224 private boolean isEnabled(PatchConfiguration configuration) { 225 Patcher patcher = Patcher.getPatcher(configuration); 226 if (patcher != null) 227 return patcher.isEnabled(fHunk); 228 return true; 229 } 230 231 public void setMatches(boolean matches) { 232 fMatches = matches; 233 } 234 235 public int getStartPosition() { 236 return fHunk.getStart(getConfiguration().isReversed()) + fShift; 237 } 238 239 public String getLabel() { 240 return getHunk().getDescription(); 241 } 242 243 public InputStream getOriginalContents() { 244 String contents = getContents(false, false); 245 return asInputStream(contents); 246 } 247 248 protected InputStream asInputStream(String contents) { 249 String charSet = getCharset(); 250 return FileDiffResult.asInputStream(contents, charSet); 251 } 252 253 public InputStream getPatchedContents() { 254 String contents = getContents(true, false); 255 return asInputStream(contents); 256 } 257 258 public String getCharset() { 259 return fDiffResult.getCharset(); 260 } 261 } 262
| Popular Tags
|