KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > merge > TextStreamMerger


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal.merge;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
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 /**
26  * A simple merger for streams containing text lines.
27  */

28 public class TextStreamMerger implements IStreamMerger {
29
30     /*
31      * (non-Javadoc)
32      *
33      * @see org.eclipse.compare.internal.merge.IAutoMerger#automerge(java.io.OutputStream,
34      * org.eclipse.core.resources.IEncodedStorage,
35      * org.eclipse.core.resources.IEncodedStorage,
36      * org.eclipse.core.resources.IEncodedStorage,
37      * org.eclipse.core.runtime.IProgressMonitor)
38      */

39     public IStatus merge(OutputStream JavaDoc output, String JavaDoc outputEncoding, InputStream JavaDoc ancestor, String JavaDoc ancestorEncoding, InputStream JavaDoc target, String JavaDoc targetEncoding, InputStream JavaDoc other, String JavaDoc 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 JavaDoc e) {
48             return new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.TextAutoMerge_inputEncodingError, e);
49         }
50
51         try {
52             String JavaDoc lineSeparator= System.getProperty("line.separator"); //$NON-NLS-1$
53
if (lineSeparator == null)
54                 lineSeparator= "\n"; //$NON-NLS-1$
55

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: // pseudo conflict
62
case RangeDifference.NOCHANGE:
63                 case RangeDifference.RIGHT:
64                     for (int j= rd.rightStart(); j < rd.rightEnd(); j++) {
65                         String JavaDoc 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 JavaDoc 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 JavaDoc e) {
88             return new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, 1, MergeMessages.TextAutoMerge_outputEncodingError, e);
89         } catch (IOException JavaDoc 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