Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9c57dbe2c1c684774485516853f933b3cd419731 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.eclipse.compare.internal.merge;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.eclipse.compare.*;
import org.eclipse.compare.rangedifferencer.RangeDifference;
import org.eclipse.compare.rangedifferencer.RangeDifferencer;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

/**
 * A simple merger for streams containing text lines.
 */
public class TextStreamMerger implements IStreamMerger {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.compare.internal.merge.IAutoMerger#automerge(java.io.OutputStream,
     *      org.eclipse.core.resources.IEncodedStorage,
     *      org.eclipse.core.resources.IEncodedStorage,
     *      org.eclipse.core.resources.IEncodedStorage,
     *      org.eclipse.core.runtime.IProgressMonitor)
     */
    public IStatus merge(OutputStream output, String outputEncoding,
			InputStream ancestor, String ancestorEncoding,
			InputStream target, String targetEncoding,
			InputStream other, String otherEncoding,
			IProgressMonitor monitor) {
        
        LineComparator a, t, o;

        try {
            a= new LineComparator(ancestor, ancestorEncoding);
            t= new LineComparator(target, targetEncoding);
            o= new LineComparator(other, otherEncoding);
        } catch (UnsupportedEncodingException e) {
            return new Status(Status.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.getString("TextAutoMerge.inputEncodingError"), e); //$NON-NLS-1$
        }

        try {
            char lineSeparator= '\n';
            
            RangeDifference[] diffs = RangeDifferencer.findRanges(monitor, a, t, o);

            for (int i = 0; i < diffs.length; i++) {
                RangeDifference rd = diffs[i];
                switch (rd.kind()) {
                case RangeDifference.ANCESTOR:	// pseudo conflict
                case RangeDifference.NOCHANGE:
                case RangeDifference.RIGHT:
                    for (int j = rd.rightStart(); j < rd.rightEnd(); j++) {
                        String s= o.getLine(j);
                        output.write(s.getBytes(outputEncoding));
                        output.write(lineSeparator);
                    }
                    break;

                case RangeDifference.LEFT:
                    for (int j = rd.leftStart(); j < rd.leftEnd(); j++) {
                        String s= t.getLine(j);
                        output.write(s.getBytes(outputEncoding));
                        output.write(lineSeparator);
                    }
                    break;

                case RangeDifference.CONFLICT:
                    return new Status(Status.ERROR, CompareUI.PLUGIN_ID, CONFLICT, MergeMessages.getString("TextAutoMerge.conflict"), null); //$NON-NLS-1$

                default:
                    break;
                }
            }
 
        } catch (UnsupportedEncodingException e) {
            return new Status(Status.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.getString("TextAutoMerge.outputEncodingError"), e); //$NON-NLS-1$
        } catch (IOException e) {
            return new Status(Status.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.getString("TextAutoMerge.outputIOError"), e); //$NON-NLS-1$
        }

        return Status.OK_STATUS;
    }
}

Back to the top