Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 001e39603c3b0cd3ecc477961f1d12e111f80d8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*******************************************************************************
 * Copyright (c) 2008, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal.win32;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.IResourceProvider;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;

/**
 * Abstract class that caches any remote contents in local so that external 
 * tools can be used to show a comparison.
 */
public abstract class AbstractMergeViewer extends Viewer {

	private Object input;
	private File leftFile;
	private File rightFile;
	private File resultFile;
	private final CompareConfiguration configuration;

	public AbstractMergeViewer(CompareConfiguration configuration) {
		this.configuration = configuration;
	}

	@Override
	public Object getInput() {
		return input;
	}

	@Override
	public ISelection getSelection() {
		return StructuredSelection.EMPTY;
	}

	@Override
	public void refresh() {
		// Nothing to do
	}

	@Override
	public void setInput(Object input) {
		this.input = input;
		reset();
	}

	protected void reset() {
		if (leftFile != null && leftFile.exists()) {
			leftFile.delete();
		}
		if (rightFile != null && rightFile.exists()) {
			rightFile.delete();
		}
		if (resultFile != null && resultFile.exists()) {
			resultFile.delete();
		}
		leftFile = null;
		rightFile = null;
		resultFile = null;
	}

	@Override
	public void setSelection(ISelection selection, boolean reveal) {
		// Nothing to do
	}

	protected boolean isOneSided() {
		if (input instanceof ICompareInput) {
			ICompareInput ci = (ICompareInput) input;
			int type = ci.getKind() & Differencer.CHANGE_TYPE_MASK;
			return type != Differencer.CHANGE;
		}
		return false;
	}
	
	protected File getFileForSingleSide() throws CoreException {
		File file = getFileForLeft();
		if (file != null && file.exists())
			return file;
		return getFileForRight();
	}
	
	protected File getFileForRight() throws CoreException {
		if (rightFile != null)
			return rightFile;
		ICompareInput ci = getCompareInput();
		if (ci != null) {
			ITypedElement right = ci.getRight();
			File file = getLocalFile(right);
			if (file != null) {
				return file;
			}
			rightFile = cacheContents(right);
			return rightFile;
		}
		return null;
	}

	protected File getFileForLeft() throws CoreException {
		if (leftFile != null)
			return leftFile;
		ICompareInput ci = getCompareInput();
		if (ci != null) {
			ITypedElement left = ci.getLeft();
			File file = getLocalFile(left);
			if (file != null) {
				return file;
			}
			leftFile = cacheContents(left);
			return leftFile;
		}
		return null;
	}
	
	protected File getResultFile() throws IOException {
		if (resultFile != null)
			return resultFile;
		resultFile = File.createTempFile("merge", ".doc"); //$NON-NLS-1$ //$NON-NLS-2$
		resultFile.deleteOnExit();
		// Need to delete the file so that clients will know that the files doesn't exist yet
		resultFile.delete();
		return resultFile;
	}
	
	protected boolean hasResultFile() {
		return resultFile != null;
	}

	private File cacheContents(ITypedElement element) throws CoreException {
		if (element instanceof IStreamContentAccessor) {
			IStreamContentAccessor sca = (IStreamContentAccessor) element;
			InputStream contents = sca.getContents();
			if (contents != null) {
				try {
					return createTempFile(contents);
				} catch (IOException e) {
					throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, e.getMessage(), e));
				} finally {
					try {
						contents.close();
					} catch (IOException e) {
						// Ignore
					}
				}
			}
		}
		return null;
	}

	private File createTempFile(InputStream contents) throws IOException {
		File file = File.createTempFile("compare", ".doc"); //$NON-NLS-1$ //$NON-NLS-2$
		file.deleteOnExit();
		try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
			byte[] buffer = new byte[1024];
			int length;
			while ((length = contents.read(buffer)) != -1) {
				out.write(buffer, 0, length);
			}
			return file;
		}
	}

	protected ICompareInput getCompareInput() {
		if (input instanceof ICompareInput) {
			return (ICompareInput) input;	
		}
		return null;
	}

	protected File getLocalFile(ITypedElement left) throws CoreException {
		IFile file = getEclipseFile(left);
		if (file != null) {
			URI uri = file.getLocationURI();
			IFileStore store = EFS.getStore(uri);
			if (store != null) {
				return store.toLocalFile(EFS.NONE, null);
			}
		}
		return null;
	}
	
	protected IFile getEclipseFile(Object element) {
		if (element instanceof IResourceProvider) {
			IResourceProvider rp = (IResourceProvider) element;
			IResource resource = rp.getResource();
			if (resource.getType() == IResource.FILE) {
				return (IFile)resource;
			}
		}
		if (element instanceof IAdaptable) {
			IAdaptable a = (IAdaptable) element;
			Object result = a.getAdapter(IResource.class);
			if (result == null) {
				result = a.getAdapter(IFile.class);
			}
			if (result instanceof IFile) {
				return (IFile) result;
			}
		}
		return null;
	}
	
	protected IEditableContent getSaveTarget() {
		IEditableContent left = getEditableLeft();
		IEditableContent right = getEditableRight();
		if (left != null && right == null) {
			return left;
		}
		if (left == null && right != null) {
			return right;
		}
		return null;
	}
	
	private IEditableContent getEditableLeft() {
		ICompareInput compareInput = getCompareInput();
		if (compareInput != null) {
			ITypedElement left = compareInput.getLeft();
			if (left instanceof IEditableContent && configuration.isLeftEditable()) {
				return (IEditableContent) left;
			}	
		}	
		return null;
	}
	
	private IEditableContent getEditableRight() {
		ICompareInput compareInput = getCompareInput();
		if (compareInput != null) {
			ITypedElement right = compareInput.getRight();
			if (right instanceof IEditableContent && configuration.isRightEditable()) {
				return (IEditableContent) right;
				
			}	
		}	
		return null;
	}
	
	protected byte[] asBytes(File file) throws IOException {
		try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int length;
			while ((length = in.read(buffer)) != -1) {
				out.write(buffer, 0, length);
			}
			out.close();
			return out.toByteArray();
		}
	}

	public CompareConfiguration getConfiguration() {
		return configuration;
	}
}

Back to the top