Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c0de0d6b396026b35a967a5f81b6006460bafaf5 (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
/*******************************************************************************
 * Copyright (c) 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.team.internal.core.subscribers;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.internal.core.Policy;
import org.eclipse.team.internal.core.TeamPlugin;

/**
 * Compare local and remote contents.
 *
 * This comparator makes use of the <code>IStorage</code> provided by
 * an <code>IResourceVariant</code> or an <code>IFileRevision</code>to obtain the remote contents.
 * This means that the comparison may contact the server unless the contents
 * were cached locally by a previous operation. The caching of remote
 * contents is subscriber specific.
 */
public abstract class AbstractContentComparator {
	private boolean ignoreWhitespace = false;

	public AbstractContentComparator(boolean ignoreWhitespace) {
		this.ignoreWhitespace = ignoreWhitespace;
	}

	public boolean compare(IResource e1, IResourceVariant e2, IProgressMonitor monitor) {
		return compareObjects(e1, e2, monitor);
	}

	public boolean compare(IResource e1, IFileRevision e2, IProgressMonitor monitor) {
		return compareObjects(e1, e2, monitor);
	}

	private boolean compareObjects(Object e1, Object e2, IProgressMonitor monitor) {
		InputStream is1 = null;
		InputStream is2 = null;
		try {
			monitor.beginTask(null, 100);
			is1 = getContents(e1, Policy.subMonitorFor(monitor, 30));
			is2 = getContents(e2, Policy.subMonitorFor(monitor, 30));
			return contentsEqual(Policy.subMonitorFor(monitor, 40), is1, is2, shouldIgnoreWhitespace());
		} catch (TeamException e) {
			TeamPlugin.log(e);
			return false;
		} finally {
			try {
				try {
					if (is1 != null) {
						is1.close();
					}
				} finally {
					if (is2 != null) {
						is2.close();
					}
				}
			} catch (IOException e) {
				// Ignore
			}
			monitor.done();
		}
	}

	protected boolean shouldIgnoreWhitespace() {
		return ignoreWhitespace;
	}

	abstract protected boolean contentsEqual(IProgressMonitor monitor, InputStream is1, InputStream is2,
			boolean ignoreWhitespace);

	private InputStream getContents(Object resource, IProgressMonitor monitor)
			throws TeamException {
		try {
			if (resource instanceof IFile) {
				return new BufferedInputStream(((IFile) resource).getContents());
			} else if (resource instanceof IResourceVariant) {
				IResourceVariant remote = (IResourceVariant) resource;
				if (!remote.isContainer()) {
					return new BufferedInputStream(remote.getStorage(monitor)
							.getContents());
				}
			} else if (resource instanceof IFileRevision) {
				IFileRevision remote = (IFileRevision) resource;
				return new BufferedInputStream(remote.getStorage(monitor)
						.getContents());
			}
			return null;
		} catch (CoreException e) {
			throw TeamException.asTeamException(e);
		}
	}
}

Back to the top