KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.team.core.mapping.IStorageMerger;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25
26 import org.eclipse.core.resources.IStorage;
27
28 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
29 import org.eclipse.ltk.core.refactoring.RefactoringSessionDescriptor;
30
31 import org.eclipse.ltk.internal.core.refactoring.IRefactoringSerializationConstants;
32 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryManager;
33 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
34 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
35
36 import org.eclipse.compare.IStreamMerger;
37
38 /**
39  * Stream merger for refactoring history files.
40  *
41  * @since 3.2
42  */

43 public final class RefactoringHistoryMerger implements IStreamMerger, IStorageMerger {
44
45     /**
46      * Creates a new refactoring history merger.
47      */

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

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

62     public IStatus merge(final OutputStream JavaDoc output, final String JavaDoc outputEncoding, 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) {
63         try {
64             performMerge(output, target, source);
65         } catch (CoreException exception) {
66             return new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 1, RefactoringUIMessages.RefactoringHistoryMerger_error_auto_merge, exception);
67         }
68         return Status.OK_STATUS;
69     }
70
71     /**
72      * {@inheritDoc}
73      */

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

114     private void performMerge(final OutputStream JavaDoc output, final InputStream JavaDoc target, final InputStream JavaDoc source) throws CoreException {
115         final RefactoringDescriptor[] sourceDescriptors= RefactoringHistoryManager.readRefactoringDescriptors(source);
116         final RefactoringDescriptor[] targetDescriptors= RefactoringHistoryManager.readRefactoringDescriptors(target);
117         final Set JavaDoc set= new HashSet JavaDoc();
118         for (int index= 0; index < sourceDescriptors.length; index++)
119             set.add(sourceDescriptors[index]);
120         for (int index= 0; index < targetDescriptors.length; index++)
121             set.add(targetDescriptors[index]);
122         final RefactoringDescriptor[] outputDescriptors= new RefactoringDescriptor[set.size()];
123         set.toArray(outputDescriptors);
124         RefactoringHistoryManager.sortRefactoringDescriptorsAscending(outputDescriptors);
125         RefactoringHistoryManager.writeRefactoringSession(output, new RefactoringSessionDescriptor(outputDescriptors, IRefactoringSerializationConstants.CURRENT_VERSION, null), true);
126     }
127 }
128
Popular Tags