KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > TextStorageMerger


1 /*******************************************************************************
2  * Copyright (c) 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.team.internal.ui.mapping;
12
13 import java.io.*;
14
15 import org.eclipse.compare.CompareUI;
16 import org.eclipse.compare.rangedifferencer.RangeDifference;
17 import org.eclipse.compare.rangedifferencer.RangeDifferencer;
18 import org.eclipse.core.resources.IStorage;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.team.core.mapping.IStorageMerger;
21 import org.eclipse.team.internal.ui.TeamUIMessages;
22
23 public class TextStorageMerger implements IStorageMerger {
24
25     public IStatus merge(OutputStream output, String JavaDoc outputEncoding,
26             IStorage ancestor, IStorage target, IStorage other,
27             IProgressMonitor monitor) throws CoreException {
28
29         LineComparator a, t, o;
30
31         try {
32             a= LineComparator.create(ancestor, outputEncoding);
33             t= LineComparator.create(target, outputEncoding);
34             o= LineComparator.create(other,outputEncoding);
35         } catch (UnsupportedEncodingException e) {
36             throw new CoreException (new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, UNSUPPORTED_ENCODING, TeamUIMessages.TextAutoMerge_inputEncodingError, e));
37         }
38
39         try {
40             boolean firstLine = true;
41             String JavaDoc lineSeparator= System.getProperty("line.separator"); //$NON-NLS-1$
42
if (lineSeparator == null)
43                 lineSeparator= "\n"; //$NON-NLS-1$
44

45             RangeDifference[] diffs= RangeDifferencer.findRanges(monitor, a, t, o);
46
47             for (int i= 0; i < diffs.length; i++) {
48                 RangeDifference rd= diffs[i];
49                 switch (rd.kind()) {
50                 case RangeDifference.ANCESTOR: // pseudo conflict
51
case RangeDifference.NOCHANGE:
52                 case RangeDifference.RIGHT:
53                     for (int j= rd.rightStart(); j < rd.rightEnd(); j++) {
54                         String JavaDoc s= o.getLine(j);
55                         if (!firstLine)
56                             output.write(lineSeparator.getBytes(outputEncoding));
57                         output.write(s.getBytes(outputEncoding));
58                         firstLine = false;
59                     }
60                     break;
61
62                 case RangeDifference.LEFT:
63                     for (int j= rd.leftStart(); j < rd.leftEnd(); j++) {
64                         String JavaDoc s= t.getLine(j);
65                         if (!firstLine)
66                             output.write(lineSeparator.getBytes(outputEncoding));
67                         output.write(s.getBytes(outputEncoding));
68                         firstLine = false;
69                     }
70                     break;
71
72                 case RangeDifference.CONFLICT:
73                     return new Status(IStatus.WARNING, CompareUI.PLUGIN_ID, CONFLICT, TeamUIMessages.TextAutoMerge_conflict, null);
74
75                 default:
76                     break;
77                 }
78             }
79
80         } catch (UnsupportedEncodingException e) {
81             throw new CoreException (new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, UNSUPPORTED_ENCODING, TeamUIMessages.TextAutoMerge_outputEncodingError, e));
82         } catch (IOException e) {
83             return new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, INTERNAL_ERROR, TeamUIMessages.TextAutoMerge_outputIOError, e);
84         }
85
86         return Status.OK_STATUS;
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.team.core.mapping.IStorageMerger#canMergeWithoutAncestor()
91      */

92     public boolean canMergeWithoutAncestor() {
93         return false;
94     }
95
96 }
97
Popular Tags