1 11 package org.eclipse.compare.internal.merge; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 import java.io.UnsupportedEncodingException ; 17 18 import org.eclipse.compare.*; 19 import org.eclipse.compare.rangedifferencer.RangeDifference; 20 import org.eclipse.compare.rangedifferencer.RangeDifferencer; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Status; 24 25 28 public class TextStreamMerger implements IStreamMerger { 29 30 39 public IStatus merge(OutputStream output, String outputEncoding, InputStream ancestor, String ancestorEncoding, InputStream target, String targetEncoding, InputStream other, String otherEncoding, IProgressMonitor monitor) { 40 41 LineComparator a, t, o; 42 43 try { 44 a= new LineComparator(ancestor, ancestorEncoding); 45 t= new LineComparator(target, targetEncoding); 46 o= new LineComparator(other, otherEncoding); 47 } catch (UnsupportedEncodingException e) { 48 return new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.TextAutoMerge_inputEncodingError, e); 49 } 50 51 try { 52 String lineSeparator= System.getProperty("line.separator"); if (lineSeparator == null) 54 lineSeparator= "\n"; 56 RangeDifference[] diffs= RangeDifferencer.findRanges(monitor, a, t, o); 57 58 for (int i= 0; i < diffs.length; i++) { 59 RangeDifference rd= diffs[i]; 60 switch (rd.kind()) { 61 case RangeDifference.ANCESTOR: case RangeDifference.NOCHANGE: 63 case RangeDifference.RIGHT: 64 for (int j= rd.rightStart(); j < rd.rightEnd(); j++) { 65 String s= o.getLine(j); 66 output.write(s.getBytes(outputEncoding)); 67 output.write(lineSeparator.getBytes(outputEncoding)); 68 } 69 break; 70 71 case RangeDifference.LEFT: 72 for (int j= rd.leftStart(); j < rd.leftEnd(); j++) { 73 String s= t.getLine(j); 74 output.write(s.getBytes(outputEncoding)); 75 output.write(lineSeparator.getBytes(outputEncoding)); 76 } 77 break; 78 79 case RangeDifference.CONFLICT: 80 return new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, CONFLICT, MergeMessages.TextAutoMerge_conflict, null); 81 82 default: 83 break; 84 } 85 } 86 87 } catch (UnsupportedEncodingException e) { 88 return new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.TextAutoMerge_outputEncodingError, e); 89 } catch (IOException e) { 90 return new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.TextAutoMerge_outputIOError, e); 91 } 92 93 return Status.OK_STATUS; 94 } 95 } 96 | Popular Tags |