1 11 package org.eclipse.ltk.internal.ui.refactoring.model; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 import java.io.UnsupportedEncodingException ; 17 import java.util.HashSet ; 18 import java.util.Set ; 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 42 public final class RefactoringIndexMerger implements IStreamMerger, IStorageMerger { 43 44 47 public RefactoringIndexMerger() { 48 } 50 51 54 public boolean canMergeWithoutAncestor() { 55 return true; 56 } 57 58 61 public IStatus merge(final OutputStream output, final String encoding, final InputStream ancestor, final String ancestorEncoding, final InputStream target, final String targetEncoding, final InputStream source, final String sourceEncoding, final IProgressMonitor monitor) { 62 try { 63 performMerge(output, encoding, target, source); 64 } catch (IOException 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 73 public IStatus merge(final OutputStream output, final String encoding, final IStorage ancestor, final IStorage target, final IStorage source, final IProgressMonitor monitor) throws CoreException { 74 InputStream targetStream= null; 75 InputStream sourceStream= null; 76 try { 77 targetStream= target.getContents(); 78 sourceStream= target.getContents(); 79 performMerge(output, encoding, targetStream, sourceStream); 80 } catch (IOException 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 exception) { 89 } 91 } 92 if (sourceStream != null) { 93 try { 94 sourceStream.close(); 95 } catch (IOException exception) { 96 } 98 } 99 } 100 return Status.OK_STATUS; 101 } 102 103 119 private void performMerge(final OutputStream output, final String encoding, final InputStream target, final InputStream source) throws IOException , UnsupportedEncodingException { 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 set= new HashSet (); 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 |