KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > model > RefactoringIndexMerger


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ltk.internal.ui.refactoring.model;
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 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.team.core.mapping.IStorageMerger;
21
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26
27 import org.eclipse.core.resources.IStorage;
28
29 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
30
31 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryManager;
32 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
33 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
34
35 import org.eclipse.compare.IStreamMerger;
36
37 /**
38  * Combined storage and stream merger for refactoring history index files.
39  *
40  * @since 3.2
41  */

42 public final class RefactoringIndexMerger implements IStreamMerger, IStorageMerger {
43
44     /**
45      * Creates a new refactoring index merger.
46      */

47     public RefactoringIndexMerger() {
48         // Do nothing
49
}
50
51     /**
52      * {@inheritDoc}
53      */

54     public boolean canMergeWithoutAncestor() {
55         return true;
56     }
57
58     /**
59      * {@inheritDoc}
60      */

61     public IStatus merge(final OutputStream JavaDoc output, final String JavaDoc encoding, final InputStream JavaDoc ancestor, final String JavaDoc ancestorEncoding, final InputStream JavaDoc target, final String JavaDoc targetEncoding, final InputStream JavaDoc source, final String JavaDoc sourceEncoding, final IProgressMonitor monitor) {
62         try {
63             performMerge(output, encoding, target, source);
64         } catch (IOException JavaDoc exception) {
65             return new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 1, RefactoringUIMessages.RefactoringHistoryMerger_error_auto_merge, exception);
66         }
67         return Status.OK_STATUS;
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public IStatus merge(final OutputStream JavaDoc output, final String JavaDoc encoding, final IStorage ancestor, final IStorage target, final IStorage source, final IProgressMonitor monitor) throws CoreException {
74         InputStream JavaDoc targetStream= null;
75         InputStream JavaDoc sourceStream= null;
76         try {
77             targetStream= target.getContents();
78             sourceStream= target.getContents();
79             performMerge(output, encoding, targetStream, sourceStream);
80         } catch (IOException JavaDoc exception) {
81             return new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 1, RefactoringUIMessages.RefactoringHistoryMerger_error_auto_merge, exception);
82         } catch (CoreException exception) {
83             return new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 1, RefactoringUIMessages.RefactoringHistoryMerger_error_auto_merge, exception);
84         } finally {
85             if (targetStream != null) {
86                 try {
87                     targetStream.close();
88                 } catch (IOException JavaDoc exception) {
89                     // Do nothing
90
}
91             }
92             if (sourceStream != null) {
93                 try {
94                     sourceStream.close();
95                 } catch (IOException JavaDoc exception) {
96                     // Do nothing
97
}
98             }
99         }
100         return Status.OK_STATUS;
101     }
102
103     /**
104      * Performs the actual merge operation.
105      *
106      * @param output
107      * the output stream
108      * @param encoding
109      * the output stream encoding
110      * @param target
111      * the target input stream
112      * @param source
113      * the source input stream
114      * @throws IOException
115      * if an input/output error occurs
116      * @throws UnsupportedEncodingException
117      * if the encoding is not supported
118      */

119     private void performMerge(final OutputStream JavaDoc output, final String JavaDoc encoding, final InputStream JavaDoc target, final InputStream JavaDoc source) throws IOException JavaDoc, UnsupportedEncodingException JavaDoc {
120         final RefactoringDescriptorProxy[] sourceProxies= RefactoringHistoryManager.readRefactoringDescriptorProxies(source, null, 0, Long.MAX_VALUE);
121         final RefactoringDescriptorProxy[] targetProxies= RefactoringHistoryManager.readRefactoringDescriptorProxies(target, null, 0, Long.MAX_VALUE);
122         final Set JavaDoc set= new HashSet JavaDoc();
123         for (int index= 0; index < sourceProxies.length; index++)
124             set.add(sourceProxies[index]);
125         for (int index= 0; index < targetProxies.length; index++)
126             set.add(targetProxies[index]);
127         RefactoringHistoryManager.writeRefactoringDescriptorProxies(output, (RefactoringDescriptorProxy[]) set.toArray(new RefactoringDescriptorProxy[set.size()]));
128     }
129 }
130
Popular Tags