KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.IStreamMerger;
16 import org.eclipse.core.resources.IEncodedStorage;
17 import org.eclipse.core.resources.IStorage;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.team.core.mapping.IStorageMerger;
20 import org.eclipse.team.internal.ui.TeamUIPlugin;
21
22 /**
23  * A merge context that performs three-way merges using the {@link IStreamMerger}
24  * interface.
25  * @since 3.2
26  */

27 public class StorageStreamMerger implements IStorageMerger {
28
29     private IStreamMerger merger;
30     
31     public StorageStreamMerger(IStreamMerger merger) {
32         this.merger = merger;
33     }
34     
35     public IStatus merge(OutputStream output, String JavaDoc outputEncoding, IStorage ancestorStorage, IStorage targetStorage, IStorage otherStorage, IProgressMonitor monitor) throws CoreException {
36         InputStream ancestorStream = null;
37         InputStream remoteStream = null;
38         InputStream targetStream = null;
39         try {
40             ancestorStream = new BufferedInputStream(ancestorStorage.getContents());
41             remoteStream = new BufferedInputStream(otherStorage.getContents());
42             targetStream = new BufferedInputStream(targetStorage.getContents());
43             IStatus status = merger.merge(output, outputEncoding,
44                     ancestorStream, getEncoding(ancestorStorage, outputEncoding),
45                     targetStream, getEncoding(targetStorage, outputEncoding),
46                     remoteStream, getEncoding(otherStorage, outputEncoding),
47                     monitor);
48             if (status.isOK())
49                 return status;
50             if (status.getCode() == IStreamMerger.CONFLICT)
51                 return new Status(status.getSeverity(), status.getPlugin(), CONFLICT, status.getMessage(), status.getException());
52             return status;
53         } finally {
54             try {
55                 if (ancestorStream != null)
56                     ancestorStream.close();
57             } catch (IOException e) {
58                 // Ignore
59
}
60             try {
61                 if (remoteStream != null)
62                     remoteStream.close();
63             } catch (IOException e) {
64                 // Ignore
65
}
66             try {
67                 if (targetStream != null)
68                     targetStream.close();
69             } catch (IOException e) {
70                 // Ignore
71
}
72         }
73     }
74
75     private String JavaDoc getEncoding(IStorage ancestorStorage, String JavaDoc outputEncoding) {
76         if (ancestorStorage instanceof IEncodedStorage) {
77             IEncodedStorage es = (IEncodedStorage) ancestorStorage;
78             try {
79                 String JavaDoc charSet = es.getCharset();
80                 if (charSet != null)
81                     return charSet;
82             } catch (CoreException e) {
83                 TeamUIPlugin.log(e);
84             }
85         }
86         return outputEncoding;
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